2013-06-03 28 views
1

我有一個數據的XML輸出文件(引文),其重複作者在文章有多個作者,所以像這樣的:串聯重複XML領域到1

<Affiliation>a School of Architecture and Urban Planning , Nanjing University , Nanjing , China.</Affiliation> 
     <AuthorList CompleteYN="Y"> 
      <Author ValidYN="Y"> 
       <LastName>Gao</LastName> 
       <ForeName>Zhi</ForeName> 
       <Initials>Z</Initials> 
      </Author> 
      <Author ValidYN="Y"> 
       <LastName>Zhang</LastName> 
       <ForeName>J S</ForeName> 
       <Initials>JS</Initials> 
      </Author> 
      <Author ValidYN="Y"> 
       <LastName>Byington</LastName> 
       <ForeName>Jerry G A</ForeName> 
       <Initials>JG</Initials> 
      </Author> 
     </AuthorList> 
     <Language>eng</Language> 

我想什麼做的是結束了,這樣你最終

<Authors>Gao, Z // Zhang, JS // Byington, JG</Authors> 

這樣加入了作者的文件時,使用的姓氏和縮寫,增加他們之間的分隔成一個字段

這是我第一次看到這個和xsl,所以我希望有人可以建議如何做到這一點

回答

0

這個樣式表會做你要求的。它複製除了任何AuthorList元素之外的整個文檔,這些元素按照您的描述進行轉換。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/> 

    <xsl:template match="/"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="AuthorList"> 
    <Authors> 
     <xsl:apply-templates select="Author"/> 
    </Authors> 
    </xsl:template> 

    <xsl:template match="Author"> 
    <xsl:if test="preceding-sibling::Author"> 
     <xsl:text> // </xsl:text> 
    </xsl:if> 
    <xsl:value-of select="concat(LastName, ', ', Initials)"/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <Affiliation>a School of Architecture and Urban Planning , Nanjing University , Nanjing , China.</Affiliation> 
    <Authors>Gao, Z // Zhang, JS // Byington, JG</Authors> 
    <Language>eng</Language> 
</root> 
+0

非常好,非常感謝。 – user2448544

+0

請問downvoter解釋問題? – Borodin

+0

你的解決方案對我來說是一個完全可以接受的解決方案,但是在某些領域有點浪費(例如,不需要使用'preceding-sibling ::'軸)。 – ABach

1

您可能會感興趣的略短的替代品。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output omit-xml-declaration="no" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="AuthorList"> 
    <Authors> 
     <xsl:apply-templates/> 
    </Authors> 
    </xsl:template> 

    <xsl:template match="Author"> 
    <xsl:if test="position() &gt; 1"> // </xsl:if> 
    <xsl:value-of select="concat(LastName, ', ', Initials)"/> 
    </xsl:template> 

</xsl:stylesheet>