我试图弄清楚如何在分配给密钥时获得节点的多位置(1.1)。以下是该问题的简要复制品:
输入XML:
<doc>
<chapter>
<title>Chapter</title>
<section>
<para0>
<title>section</title>
</para0>
</section>
<section>
<para0>
<title>section</title>
</para0>
</section>
<procedure>
<title>procedure</title>
</procedure>
<section>
<para0>
<title>section</title>
</para0>
</section>
<section>
<para0>
<title>section</title>
</para0>
</section>
<procedure>
<title>procedure</title>
</procedure>
</chapter>
</doc>
所需的输出
<output>
<chapter>Chapter 1
<section>section 1.1</section>
<section>section 1.2</section>
<section>procedure 1.3</section>
<section>section 1.4</section>
<section>section 1.5</section>
<section>procedure 1.6</section>
</chapter>
</output>
当前输出
<output>
<chapter>Chapter 1
<section>section 1</section>
<section>section 1</section>
<section>procedure 1</section>
<section>section 1</section>
<section>section 1</section>
<section>procedure 1</section>
</chapter>
</output>
我的XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="numbering" match="chapter/procedure" use="generate-id()"/>
<xsl:key name="numbering" match="chapter/section/para0" use="generate-id()" />
<xsl:template match="/">
<output>
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="doc">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter">
<chapter>
<xsl:value-of select="title"/>
<xsl:text> </xsl:text>
<xsl:number level="multiple" count="chapter" format="1"/>
<xsl:for-each select="section/para0 | procedure ">
<section>
<xsl:value-of select="title"/>
<xsl:text> </xsl:text>
<xsl:number level="any" select="key('numbering', generate-id())/title" from="chapter" format="1.1"/>
</section>
</xsl:for-each>
</chapter>
</xsl:template>
答案 0 :(得分:3)
使用Saxon 9.5,以下样式表可以生成所需的输出:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="doc">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="chapter">
<chapter>
<xsl:value-of select="title"/>
<xsl:text> </xsl:text>
<xsl:number level="multiple" count="chapter" format="1"/>
<xsl:apply-templates/>
</chapter>
</xsl:template>
<xsl:template match="section | procedure ">
<section>
<xsl:value-of select="(title, para0/title)[1]"/>
<xsl:text> </xsl:text>
<xsl:number level="multiple" count="chapter | section | procedure" format="1.1"/>
</section>
</xsl:template>
</xsl:stylesheet>