[英]How do I get different values from the same variable in XML using XSL
XML: XML:
<message code="100" description="checkSnr">
<string name="id"/>
<string name="serialNr"/>
</message>
XSL: XSL:
<xsl:template match="/">
<xsl:for-each select="machine/events">
<xsl:text>;</xsl:text>
<xsl:value-of select="message/@code"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="message/string/@name" />
<xsl:text>;</xsl:text>
<xsl:value-of select="message/string/@name" />
</xsl:for-each>
</xsl:template>
So here's my Problem. 所以这是我的问题。 My XSL-File is supposed to detect the value "id" and the value "serialNr", but my Output is "id" x2.
我的XSL文件应该检测到值“ id”和值“ serialNr”,但我的输出是“ id” x2。 I know it is only Logical but how can I actually Change my XSL so it gives me both of the values?
我知道这只是逻辑上的问题,但是如何才能实际更改XSL以便给我两个值呢? (by the way, in the XML there are parts, where the variable "string Name" is used like 4 times) I thought of rewriting the XML with a Loop so that the Name variables get renamed into name1, name2, name3 and so on, but I can't figure out how to do that.
(顺便说一下,在XML中有一些部分,其中变量“ string Name”使用了大约4次)我想到了用Loop重写XML,以便将Name变量重命名为name1,name2,name3等。 ,但我不知道该怎么做。 I use Java as a converter if you can think of any Solutions in Java.
如果您能想到Java中的任何解决方案,我都将Java用作转换器。 My Output Format is .CSV if that matters.
如果重要的话,我的输出格式为.CSV。
(any other tips on improving the XSL are appreciated aswell) (对改进XSL的任何其他技巧也表示赞赏)
Thanks! 谢谢!
It is strange that you have 2 empty string
tags. 奇怪的是,您有2个空
string
标签。
I suppose, your XML should have the following shape: 我想,您的XML应该具有以下形状:
<message code="100" description="checkSnr">
<string name="id">1234</string>
<string name="serialNr">5678</string>
</message>
Then you shoud read both string
tags using name
attribute with particular value as a predicate : 然后你读768,16两个
string
使用标签name
与特定值作为谓语 ,定语:
<xsl:template match="/">
<xsl:for-each select="machine/events">
<xsl:text>;</xsl:text>
<xsl:value-of select="message/@code"/>
<xsl:text>;</xsl:text>
<xsl:value-of select="message/string[@name='id']" />
<xsl:text>;</xsl:text>
<xsl:value-of select="message/string[@name='serialNr']" />
</xsl:for-each>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.