简体   繁体   English

如何使用XSL从XML中的同一变量获取不同的值

[英]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.

相关问题 如何使用 Java 从 MongoDB 中的所有文档中获取相同键的值? - How do I get values of same key from all documents in MongoDB using Java? 如何从同一文本字段中获得两个值? - How do I get two values from same text field? 如何使用XSL排列数据 - How do I arrange data using XSL 如何使用Java在同一方法中使用不同的字符串值更新变量? - How to update a variable with different string values in the same method using java? 如何从 Spring MVC 中同一控制器中不同 JSP 页面上的表单访问值? - How do I access values from forms on different JSP pages in the same controller in Spring MVC? 如何将具有相同键的两个 HashMap 中的值(不同数据类型)放入新的第三个 HashMap? - How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap? 如何在Java中将变量值从Java转换为xml文件? - How to get variable values from java to xml file in android? 为什么我从arrayList的不同对象中获得相同的值? - Why do I get the same value from different objects an an arrayList? 为同一个XSL样式表使用不同的HTML模板 - Using a different HTML template for the same XSL stylesheet 如何从调用同一个变量的两个线程中获得相同的值? - How do I get an identical value from two threads calling on the same variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM