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>
任何人都試圖幫助我