XSLT:选择跟随兄弟直到达到指定的标签 [英] XSLT: Select following-sibling until reaching a specified tag

查看:28
本文介绍了XSLT:选择跟随兄弟直到达到指定的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写 XSLT,它将在选定的后续兄弟姐妹上运行 for-each,但在到达另一个标记 (h1) 时停止.

I am attempting to write XSLT that will run a for-each on the selected following-siblings but stop when another tag (h1) is reached.

这是源 XML:

<?xml version="1.0"?>
<html>
    <h1>Test</h1>
    <p>Test: p 1</p>
    <p>Test: p 2</p>
    <h1>Test 2</h1>
    <p>Test2: p 1</p>
    <p>Test2: p 2</p>
    <p>Test2: p 3</p>
</html>

这是 XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="h1">
        <section>
            <sectionHeading>
                <xsl:apply-templates/>
            </sectionHeading>
            <sectionContent>
                <xsl:for-each select="following-sibling::p">
                    <paragraph>
                        <xsl:value-of select="."/>
                    </paragraph>
                </xsl:for-each>
            </sectionContent>
        </section>
    </xsl:template>

    <xsl:template match="p"/>
</xsl:stylesheet>

这是当前的结果:

<?xml version="1.0" encoding="UTF-8"?>
<content>
    <section>
        <sectionHeading>Test</sectionHeading>
        <sectionContent>
            <paragraph>Test: p 1</paragraph>
            <paragraph>Test: p 2</paragraph>
            <paragraph>Test: p 3</paragraph>
            <paragraph>Test2: p 1</paragraph>
            <paragraph>Test2: p 2</paragraph>
        </sectionContent>
    </section>
    <section>
        <sectionHeading>Test 2</sectionHeading>
        <sectionContent>
            <paragraph>Test2: p 1</paragraph>
            <paragraph>Test2: p 2</paragraph>
        </sectionContent>
    </section>
</content>

这是预期的结果:

<?xml version="1.0" encoding="UTF-8"?>
<content>
<section>
    <sectionHeading>Test</sectionHeading>
    <sectionContent>
        <paragraph>Test: p 1</paragraph>
        <paragraph>Test: p 2</paragraph>
        <paragraph>Test: p 3</paragraph>
    </sectionContent>
</section>
<section>
    <sectionHeading>Test 2</sectionHeading>
    <sectionContent>
        <paragraph>Test2: p 1</paragraph>
        <paragraph>Test2: p 2</paragraph>
    </sectionContent>
</section>
</content>

推荐答案

试试这个:(而不是要求所有的 p,我们要求所有最近在 h1 之前的 p 是当前的.)

Try this: (Instead of asking for all the p's we ask for all the p's whose most recently preceding h1 is current.)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="h1">
        <xsl:variable name="header-id" select="generate-id(.)"/>
        <section>
            <sectionHeading>
                <xsl:apply-templates/>
            </sectionHeading>
            <sectionContent>
                <xsl:for-each select="following-sibling::p[generate-id(preceding-sibling::h1[1]) = $header-id]">
                    <paragraph>
                        <xsl:value-of select="."/>
                    </paragraph>
                </xsl:for-each>
            </sectionContent>
        </section>
    </xsl:template>

    <xsl:template match="p"/>
</xsl:stylesheet>

这篇关于XSLT:选择跟随兄弟直到达到指定的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆