XSLT foreach重复节点平坦 [英] XSLT foreach repeating nodes to flat
问题描述
我有一个XML模式,我需要从中提取值,但是运气不太好.
I have an XML schema that I need to extract values from but I am not having much luck.
来源模式为:
<Reading>
<State>
<StateValues>
<Name>
<Value>
</StateValues>
</State>
<Reading>
XML的示例为:
<Reading>
<State>
<StateValues>
<Name>Reading1</Name>
<Value>1234</Value>
</StateValues>
<StateValues>
<Name>Reading2</Name>
<Value>2345</Value>
</StateValues>
</State>
<State>
<StateValues>
<Name>Reading3</Name>
<Value>4321</Value>
</StateValues>
</State>
<Reading>
我需要的结果是:
<Readings>
<Reading1>1234</Reading1>
<Reading2>2345</Reading2>
<Reading3>4321</Reading3>
</Readings>
我无法控制架构.
我一直在尝试使用xsl:foreach
和xsl:if
来检查名称,但是我只是将第一个值重复3次.有小费吗?我已经尝试了2天,但我不是XSLT专家...
I have been trying to use a xsl:foreach
with an xsl:if
that checks the name but I just get the first value repeated 3 times. Any tips? I've been trying to figure this out for 2 days and I am not an XSLT expert...
谢谢. 对不起,您没有发布我的XSLT.我尝试了多个版本,但认为它没有用.在这里:
Thanks. EDIT : Sorry for not posting my XSLT. I had tried several versions and didn't think it would be useful. Here it is:
Blockquote
Blockquote
<xsl:element name="ns0:Readings">
<xsl:element name="ns0:Current">
<xsl:for-each select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']">
<xsl:choose>
<xsl:when test="Name='TOTAL'">
<xsl:element name="ns0:Total">
<xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Total'" />
</xsl:element>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()='Reading1'">
<xsl:element name="ns0:Reading1">
<xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
</xsl:element>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading2'">
<xsl:element name="ns0:Reading2">
<xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
</xsl:element>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading3'">
<xsl:element name="ns0:Reading3">
<xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
</xsl:element>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text() = 'Reading4'">
<xsl:element name="ns0:Reading4">
<xsl:value-of select="/*[local-name()='gatewayInterface' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Reading' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='State' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='StateValues' and namespace-uri()='http://biztalk.gateway.com']/*[local-name()='Name' and namespace-uri()='http://biztalk.gateway.com']/text()" />
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:element>
推荐答案
foreach
和if
在XSLT中通常是错误的方法.将模板应用于模式要有效得多.在这种情况下,您可以使用两种方法:第一种设置根元素并匹配您要使用的元素:
foreach
and if
are usually the wrong approach in XSLT. It is far more effective to apply templates to patterns. In this case you can use two: the first sets up the root element and matches the elements you want to work with:
<xsl:template match='/'>
<Readings>
<xsl:apply-templates select='Reading/State/StateValues'/>
</Readings>
</xsl:template>
,当找到匹配项并完成核心工作时,上述触发的秒数.这里的复杂性是动态创建一个元素,这意味着使用{XPath}
语法从输入文档中获取名称:
and a second that the above triggers when a match is found that will do the core work. The complexity here is creating an element dynamically which means using the {XPath}
syntax to get the name from the input document:
<xsl:template match='StateValues'>
<xsl:element name='{Name}'>
<xsl:value-of select='Value' />
</xsl:element>
</xsl:template>
这篇关于XSLT foreach重复节点平坦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!