使用 XSL 迭代 For Each 循环中的元素 [英] Use XSL to Iterate over Elements in For Each Loop
问题描述
所以我在让它工作时遇到了严重的问题(我确信这很简单,我对 XSL 的经验很少).
so I have been having serious issues getting this to work (and I'm sure it is simple, I have little experience with XSL).
我试图在结构如下的 xml 文档中迭代一堆非常不同的子元素:
I am trying to iterate over a bunch of very different child elements in an xml document that is structured like this:
<transaction>
<data_xml>
<document data_type="0">
<element1>value1</element1>
<element2>value2</element2>
<element3>value3</element3>
</document>
</data_xml>
</transaction>
我使用的是 XSL 1.0 版,考虑到我所使用的系统的限制,我很确定我不能为此使用 2.0.
I am using XSL version 1.0, and I'm pretty sure that I cannot use 2.0 for this considering the constraints of the system I am working in.
我想要的输出是:
<foo>
<bar:element1><!CDATA[[value]]></bar:element1>
<bar:element2><!CDATA[[value]]></bar:element2>
<bar:element3><!CDATA[[value]]></bar:element3>
</foo>
我目前使用的可怕的可怕代码是:
The awful hideous code I am currently using is:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<foo>
<xsl:for-each select="/transaction/data_xml/document[@data_type='0']/*">
<bar:<xsl:value-of select="name(name(/transaction/data_xml/document[@data_type='0']/*)"/>>
<![CDATA[<xsl:value-of select="/transaction/data_xml/document[@data_type='0']/*"/>]]>
</bar:<xsl:value-of select="name(/transaction/data_xml/document[@data_type='0']/*)"/>>
</xsl:for-each>
</foo>
</xsl:template>
</xsl:stylesheet>
但是,当我使用它时,它只是多次迭代同一个元素,而不是依次迭代每个元素.
When I use this, though, it just iterates over the same element multiple times, instead of each element in turn.
有什么想法吗?除了因为看起来很糟糕的代码而向我开枪?
Any ideas? Besides shooting me for terribad looking code?
推荐答案
您可能想要执行以下操作:
You probably want to do something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:bar="http://example.com/bar">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<foo>
<xsl:for-each select="transaction/data_xml/document[@data_type='0']/*">
<xsl:element name="bar:{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</foo>
</xsl:template>
</xsl:stylesheet>
应用于您的示例输入,将导致:
which, applied to your example input, would result in:
<?xml version="1.0" encoding="utf-8"?>
<foo xmlns:bar="http://example.com/bar">
<bar:element1>value1</bar:element1>
<bar:element2>value2</bar:element2>
<bar:element3>value3</bar:element3>
</foo>
如果您希望各种 bar:elementX
值成为 CDATA 部分,您应该在
指令中指定:
If you want the various bar:elementX
values to be CDATA sections, you should specify so in the <xsl:output>
instruction so:
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" cdata-section-elements="bar:element1 bar:element2 bar:element3"/>
然而,这需要您提前知道所有可能的元素名称.否则你必须破解它,例如:
However this requires you to know all possible element names in advance. Otherwise you'd have to hack it, for example as:
<xsl:template match="/">
<foo>
<xsl:for-each select="transaction/data_xml/document[@data_type='0']/*">
<xsl:element name="bar:{local-name()}">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</xsl:element>
</xsl:for-each>
</foo>
</xsl:template>
这篇关于使用 XSL 迭代 For Each 循环中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!