XSL 按部分分组
XSL Grouping by Sections
使用 XSL 1.0,我试图获取两个 "Section" 元素之间的数据。我单独获取 "Heading" 信息没有问题,但我也想知道中间是哪个部分,因为他们不是兄弟姐妹。这就是我搞砸的地方。
我在网上和这个论坛上搜索了各种解决方案,主要是尝试生成密钥和使用分组,但我一直未能获得任何有效的解决方案。有时它是一个 "whitespace expected" 错误或什么都没有。
输入:
<Root>
<Content>
<Paragraph Type="New Section">
<Text>Section A</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 1</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 1</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 2</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 2</Text>
</Paragraph>
<Paragraph Type="End Of Section">
<Text>End of Section A</Text>
</Paragraph>
<Paragraph Type="New Section">
<Text>Section B</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 3</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 3</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 4</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 4</Text>
</Paragraph>
<Paragraph Type="End Of Section">
<Text>End of Section B</Text>
</Paragraph>
</Content>
</Root>
期望输出:
"Important information 1"
"Section A"
"Important information 2"
"Section A"
"Important information 3"
"Section B"
"Important information 4"
"Section B"
正如我提到的,使用 choose 和 when 测试 Paragraph/@Type = "Heading" 我能够得到 "Important information" 文本,但我不知道如何区分哪些部分他们倒下了。
提前致谢。
I cannot figure out how to tell between which Sections they fall.
如果您将问题重述为 "which section was the last one to start prior to the current heading",那么它就变得相当微不足道了:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/Root">
<xsl:for-each select="Content/Paragraph[@Type='Heading']">
<xsl:value-of select="Text"/>
<xsl:text> </xsl:text>
<xsl:value-of select="preceding-sibling::Paragraph[@Type='New Section'][1]/Text"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
使用 XSL 1.0,我试图获取两个 "Section" 元素之间的数据。我单独获取 "Heading" 信息没有问题,但我也想知道中间是哪个部分,因为他们不是兄弟姐妹。这就是我搞砸的地方。
我在网上和这个论坛上搜索了各种解决方案,主要是尝试生成密钥和使用分组,但我一直未能获得任何有效的解决方案。有时它是一个 "whitespace expected" 错误或什么都没有。
输入:
<Root>
<Content>
<Paragraph Type="New Section">
<Text>Section A</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 1</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 1</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 2</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 2</Text>
</Paragraph>
<Paragraph Type="End Of Section">
<Text>End of Section A</Text>
</Paragraph>
<Paragraph Type="New Section">
<Text>Section B</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 3</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 3</Text>
</Paragraph>
<Paragraph Type="Stuff">
<Text>Random information 4</Text>
</Paragraph>
<Paragraph Type="Heading">
<Text>Important information 4</Text>
</Paragraph>
<Paragraph Type="End Of Section">
<Text>End of Section B</Text>
</Paragraph>
</Content>
</Root>
期望输出:
"Important information 1"
"Section A"
"Important information 2"
"Section A"
"Important information 3"
"Section B"
"Important information 4"
"Section B"
正如我提到的,使用 choose 和 when 测试 Paragraph/@Type = "Heading" 我能够得到 "Important information" 文本,但我不知道如何区分哪些部分他们倒下了。
提前致谢。
I cannot figure out how to tell between which Sections they fall.
如果您将问题重述为 "which section was the last one to start prior to the current heading",那么它就变得相当微不足道了:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/Root">
<xsl:for-each select="Content/Paragraph[@Type='Heading']">
<xsl:value-of select="Text"/>
<xsl:text> </xsl:text>
<xsl:value-of select="preceding-sibling::Paragraph[@Type='New Section'][1]/Text"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>