我正在尝试编写XSLT,它将在选定的后续兄弟上运行for-each,但在到达另一个标记(h1)时停止。XSLT:选择以下兄弟直到达到指定的标记
这里的源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>
@添,虽然我们可以得到XSLT一个解决方案,我宁愿建议你到XML格式更改为 哪个更合理,可读性强,易于在xslt中查询。 –
Ravisha
2010-01-30 04:05:16
测试
测试:第1页
测试:第2页
试验2
Test2的:p 1
Test2的:p 2
Test2:p 3