小编Bru*_*aim的帖子

如何在XSLT的输出中包含通过xpath参数选择的节点的祖先分支

尝试了8个多小时后,我希望有人可以帮助我:

给出一本书的以下(简化)XML:

<book>
    <section name="A">
        <chapter name="I">
            <paragraph name="1"/>
            <paragraph name="2"/>
        </chapter>
        <chapter name="II">
            <paragraph name="1"/>          
        </chapter>
    </section>
    <section name="B">
        <chapter name="III"/>
        <chapter name="IV"/>   
    </section>
</book>
Run Code Online (Sandbox Code Playgroud)

我能够使用以下XSL基于给定参数提取书籍XML的任何部分(部分,章节或段落):

<xsl:param name="subSelectionXPath" required="yes" as="node()"/>

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

<xsl:template match="*">
    <!-- output node with all children -->
    <xsl:copy-of select="."/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

和参数$ subSelectionXPath的值类似

doc(filename)//chapter[@name='II']

产生输出:

<chapter name="II">
    <paragraph name="1"/>          
</chapter>
Run Code Online (Sandbox Code Playgroud)

我想要实现的另一个目标是让祖先XML分支包含所选的XML片段,即:

<book>
    <section name="A">
        <chapter name="II">
            <paragraph name="1"/>          
        </chapter>
    </section>    
</book>
Run Code Online (Sandbox Code Playgroud)

我想(并尝试)遍历XML树并测试当前节点是否是祖先,类似于(伪代码):

<xsl:if test="node() in $subSelectionXPath/ancestor::node()">
    <xsl:copy>
       <xsl:apply-templates/>
    </xsl:copy> …
Run Code Online (Sandbox Code Playgroud)

xslt xpath xslt-2.0

8
推荐指数
1
解决办法
1900
查看次数

标签 统计

xpath ×1

xslt ×1

xslt-2.0 ×1