Xslt 如何对结果集中的已筛选元素重新编号

Xslt 如何对结果集中的已筛选元素重新编号,xslt,Xslt,XML: <?xml version="1.0" encoding="utf-8"?> <book> <chapter> <section name="a">...</section> <section name="b">...</section> <section name="c">...</section> <

XML:

<?xml version="1.0" encoding="utf-8"?>
<book>
    <chapter>
        <section name="a">...</section>
        <section name="b">...</section>
        <section name="c">...</section>
        <section name="d">...</section>
        <section name="e">...</section>
        ...
    </chapter>
    <appendix>
        <section name="reference">...</section>
    </appendix>
</book>
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://example.com/namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:param name="validChapters" />

    <xsl:function name="ex:isValidChapter" as="xs:boolean">
        <xsl:param name="str-in" as="xs:string"/>
        <xsl:choose>
            <xsl:when test="contains($validChapters, $str-in)">
                <xsl:sequence select="true()" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:sequence select="false()" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>

    <xsl:template match="/">
        <xsl:apply-templates select="chapter" />
        <xsl:apply-templates select="appendix" />
    </xsl:template>

    <xsl:template match="chapter">
        ...
        <xsl:apply-templates select="section" />
    </xsl:template>

    <xsl:template match="appendix">
        ...
        <xsl:apply-templates select="section" />
    </xsl:template>

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

    <xsl:template match="//chapter/section">
        <xsl:if test="ex:isValidChapter(@name)">
            <fo:block>
                <xsl:number format="1."/>
                <xsl:value-of select="@name" />
            </fo:block>
            <xsl:apply-templates />
        </xsl:if>
    </xsl:template>

    ...
</xsl:stylesheet>

答案只是为您的
指令提供一个
count=
属性:

        <xsl:number format="1." count="section[ex:isValidChapter(@name)]"/>

因此,在对节进行编号时,只计算“有效”的节,因此编号是正确的

还请注意,您需要修改模板,其中说明:

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

因为
不是根节点的子节点。(它是文档元素的子元素,


此外,您的函数不需要选择/何时/以其他方式将布尔结果转换为布尔结果。可缩短为:

<xsl:function name="ex:isValidChapter" as="xs:boolean">
   <xsl:param name="str-in" as="xs:string"/>
   <xsl:sequence select="contains($validChapters, $str-in)"/>
</xsl:function>

答案只是为您的
指令提供一个
count=
属性:

        <xsl:number format="1." count="section[ex:isValidChapter(@name)]"/>

因此,在对节进行编号时,只计算“有效”的节,因此编号是正确的

还请注意,您需要修改模板,其中说明:

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

因为
不是根节点的子节点。(它是文档元素的子元素,


此外,您的函数不需要选择/何时/以其他方式将布尔结果转换为布尔结果。可缩短为:

<xsl:function name="ex:isValidChapter" as="xs:boolean">
   <xsl:param name="str-in" as="xs:string"/>
   <xsl:sequence select="contains($validChapters, $str-in)"/>
</xsl:function>


Explain更清晰,带有示例XML数据和所需输出。“允许列表”存储在哪里?允许列表以逗号分隔字符串的形式从java应用程序传入,以便使用contains函数Explain更清晰,带有示例XML数据和所需输出。“允许列表”存储在哪里存储?允许列表从java应用程序传入,以逗号分隔字符串的形式,以便使用contains函数