2017-04-10 69 views
0

我需要將xml轉換爲另一種xml格式,其中我需要使用父元素journalcontent按順序爲所有章節和章節提供id。使用XSLT爲元素插入ID

<?xml version="1.0" encoding="UTF-8"?> 
<book> 
    <footnote> 
<journal> 
    <section>Fir</section> sum <chapter>sec</chapter> 
    </journal> 
</footnote> 

<footnote> 
    <journal> 
     <section>thir</section> sum <chapter>four</chapter> 
    </journal> 
</footnote> 

<footnote> 
    <journal> 
     <section>ff</section> sum <chapter>66</chapter> 
    </journal> 
    </footnote> 
</book> 

我試圖通過以下XSLT,但輸出中不corect

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:template match="book"> 
     <book> 
      <xsl:apply-templates/> 
     </book> 
    </xsl:template> 
    <xsl:template match="//footnote"> 
      <xsl:variable name="seq"> 
      <xsl:number format="001" level="any"/> 
     </xsl:variable> 
     <xsl:for-each select="journal"> 
      <xsl:if test="section"> 
       <journalcontent> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="$seq"/> 
        </xsl:attribute> 
        <xsl:copy-of select="section"/> 
       </journalcontent> 
      </xsl:if> 
      <xsl:if test="chapter"> 
       <journalcontent> 
        <xsl:attribute name="id"> 
         <xsl:value-of select="$seq"/> 
        </xsl:attribute> 
        <xsl:copy-of select="chapter"/> 
       </journalcontent> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我得到了下面的輸出

<?xml version="1.0" encoding="UTF-8"?><book> 
    <journalcontent id="001"><section>Fir</section></journalcontent> 
    <journalcontent id="001"><chapter>sec</chapter></journalcontent> 
    <journalcontent id="002"><section>thir</section></journalcontent> 
    <journalcontent id="002"><chapter>four</chapter></journalcontent> 
    <journalcontent id="003"><section>ff</section></journalcontent> 
    <journalcontent id="003"><chapter>66</chapter></journalcontent> 
</book> 

我期待下面

所提到的唯一ID輸出
<?xml version="1.0" encoding="UTF-8"?> 
    <book> 
     <journalcontent id="001"><section>Fir</section></journalcontent> 
     <journalcontent id="002"><chapter>sec</chapter></journalcontent> 
     <journalcontent id="003"><section>thir</section></journalcontent> 
     <journalcontent id="004"><chapter>four</chapter></journalcontent> 
     <journalcontent id="005"><section>ff</section></journalcontent> 
    <journalcontent id="006"><chapter>66</chapter></journalcontent> 
    </book> 

任何人都試圖幫助我

回答

0

你是立足編號的footnote元素,其中只有3當你試圖號碼在journal元素sectionchapter,如果你改變了你的模板來匹配journal元素可能會更好,然後選擇這個內

<xsl:template match="journal"> 
    <xsl:for-each select="section|chapter"> 

chaptersection元件。然後,得到的序列號,在任何水平

計數的 sectionchapter元件的數量

試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:strip-space elements="*" /> 
    <xsl:output method="xml" indent="yes" /> 

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

    <xsl:template match="journal"> 
     <xsl:for-each select="section|chapter"> 
      <journalcontent> 
       <xsl:attribute name="id"> 
        <xsl:number format="001" count="section|chapter" level="any"/> 
       </xsl:attribute> 
       <xsl:copy-of select="."/> 
      </journalcontent> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet>