Xml 使用XSLT为元素插入ID
我需要将下面的xml转换为另一种xml格式,其中我需要按照父元素journalcontent的顺序为所有章节和章节提供idXml 使用XSLT为元素插入ID,xml,xslt,Xml,Xslt,我需要将下面的xml转换为另一种xml格式,其中我需要按照父元素journalcontent的顺序为所有章节和章节提供id <?xml version="1.0" encoding="UTF-8"?> <book> <footnote> <journal> <section>Fir</section> sum <chapter>sec</chapter> </journ
<?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>
杉木和秒
三加四
ff总和66
我尝试使用下面的xslt,但输出不正确
<?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>
冷杉
秒
蒂尔
四
ff
66
我希望输出为下面提到的唯一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>
冷杉
秒
蒂尔
四
ff
66
任何人都可以帮我你的编号是基于
脚注
元素,其中只有3个。当您试图对日记帐
元素中的部分
和章节
进行编号时,最好更改模板以匹配日记帐
元素,然后在此元素中选择章节
或章节
元素
<xsl:template match="journal">
<xsl:for-each select="section|chapter">
试试这个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>
<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>