如何使用模板遍历嵌套 XML 结构
我是 XSL 新手,正在从头开始解决问题。
我有一个包含以下结构的源 XML 文件:-
<root>
<Header>
</Header>
<DetailRecord>
<CustomerNumber>1</CustomerNumber>
<DetailSubRecord>
<Address>London</Address>
</DetailSubRecord>
<DetailSubRecord>
<Address>Hull</Address>
</DetailSubRecord>
</DetailRecord>
<DetailRecord>
<CustomerNumber>2</CustomerNumber>
<DetailSubRecord>
<Address>Birmingham</Address>
</DetailSubRecord>
<DetailSubRecord>
<Address>Manchester</Address>
</DetailSubRecord>
</DetailRecord>
<Footer>
</Footer>
</root>
其中有多个
,每个
都有多个
。
我已经设法组合一个 XSL,将多个 DetailRecords 的单个嵌套集合输出到平面文件,但我无法弄清楚如何引用 XSL 中的第二个嵌套级别的地址记录...
这是到目前为止我的 XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:variable name="spaces" select="' '"/>
<xsl:variable name="newline">
<xsl:text> </xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="root/Header/HeaderField"/>
<xsl:copy-of select="$newline"/>
<xsl:for-each select="root/DetailRecord">
<xsl:value-of select="CustomerNumber"/>
<xsl:copy-of select="$newline"/>
</xsl:for-each>
Trailer - recordCount - <xsl:value-of select="count(root/DetailRecord)"/>
</xsl:template>
</xsl:stylesheet>
I am new to XSL and in the process of picking this up from scratch to solve a problem.
I have a source XML file that contains the following structure:-
<root>
<Header>
</Header>
<DetailRecord>
<CustomerNumber>1</CustomerNumber>
<DetailSubRecord>
<Address>London</Address>
</DetailSubRecord>
<DetailSubRecord>
<Address>Hull</Address>
</DetailSubRecord>
</DetailRecord>
<DetailRecord>
<CustomerNumber>2</CustomerNumber>
<DetailSubRecord>
<Address>Birmingham</Address>
</DetailSubRecord>
<DetailSubRecord>
<Address>Manchester</Address>
</DetailSubRecord>
</DetailRecord>
<Footer>
</Footer>
</root>
where there are mutiple <DetailRecord>
s each with multiple <DetailSubRecord>
s.
I have managed to put together an XSL that outputs a single nested set of multiple DetailRecords to a flat file but I haven't been able to puzzle out how to refer to the 2nd nested level of address records in the XSL...
Here is my XSL so far:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:variable name="spaces" select="' '"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="root/Header/HeaderField"/>
<xsl:copy-of select="$newline"/>
<xsl:for-each select="root/DetailRecord">
<xsl:value-of select="CustomerNumber"/>
<xsl:copy-of select="$newline"/>
</xsl:for-each>
Trailer - recordCount - <xsl:value-of select="count(root/DetailRecord)"/>
</xsl:template>
</xsl:stylesheet>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XSLT 是一种函数式语言,而不是一种过程式语言;大多数 XSLT 新手没有意识到 XSLT 处理器会按照它们在源代码中出现的顺序自动处理树中的每个节点。然而,如果没有模板来定义如何处理每个节点,则不会输出任何内容。
在大多数情况下,您不需要仅使用
来处理子元素,这已经为您完成了,您只需要定义一个模板来描述您希望如何输出每个元素。像这样:第一个模板中的
只是告诉 XSLT 处理器处理子元素,然后添加记录计数。第二个模板处理其
match
属性中具有三个名称的任何元素,并在每种情况下输出与新行连接的内容 (.
)。当前形式的第三个模板实际上是多余的,处理器无论如何都会这样做,但您可以用更有用的东西替换该注释。
您会注意到,这并没有提供有关如何处理
DetailRecord
元素的任何信息;因为您想要做的就是处理它的子级,所以您不需要指定任何内容,因为这是给定的。XSLT is a functional language, not a procedural; what most newcomers to XSLT don't realise is that the XSLT processor automatically handles each node in the tree, in the order they appear in the source. Without a template to define what to do with each node however, nothing is output.
In most cases, you don't need to use
<xsl:for-each>
just to get the child elements processed, this is already done for you, you just need to define a template that describes how you want each element to be output. Like this:The
<xsl:apply-templates />
in the first template just tells the XSLT processor to deal with the child elements, after which it adds in the record count.The second template handles any element with the three names in it's
match
atrtibute, and in each case outputs the content (.
) concatenated with a new line.The third template in it's current form is actually superfluous, the processor will do that anyway, but you can replace that comment with something more useful.
You'll notice this doesn't give any information on how to handle a
DetailRecord
element; because all you want to do is process it's children, you don't need to specify anything, as that's taken as a given.这里有一个简单的示例,说明如何(字面意思)将模板应用到您的情况。因为你不太清楚所需的输出,所以我发明了一个。
XSLT 1.0 在 Saxon 6.5.5 下测试
应用于您的输入会得到:
Here you have a simple example on how to (literally) apply templates to your situation. Because you wasnt so clear about the required output, I've invented one.
XSLT 1.0 tested under Saxon 6.5.5
Applied on your input gets: