1

我试图弄清楚如何在分配给键时获得节点的多位置(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>

4

1 回答 1

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>
于 2013-11-10T13:39:24.383 回答