English 中文(简体)
从 str: tokenize () 绕过多个序列
原标题:Looping through multiple sequences from str:tokenize()

我也有类似的XML 来自于一些硬件:

<invoice>
  <field name="item">Item 1;Item 2;Item 3</field>
  <field name="price">32.0;192.2;12.0</field>
  <field name="quantity">1;4;2</field>
</invoice>

我需要转换 类似这个:

<invoice>
  <item>
    <desc>Item 1</desc>
    <price>32.0</price>
    <quantity>1</quantity>
  </item>
  <item>
    <desc>Item 1</desc>
    <price>192.0</price>
    <quantity>4</quantity>
  </item>
  <item>
    <desc>Item 3</desc>
    <price>12.0</price>
    <quantity>2</quantity>
  </item>     
</invoice>

此时,我尝试了斯特勒:tokenize (),但主要问题是构建一个简单的循环。我对XSLT的了解非常基本,我的工作进展就非常深入:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:str="http://exslt.org/strings">
<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:template match="inovice">
    <xsl:param name="separator" select=" ; "/>
    <xsl:param name="desc" select="str:tokenize(field[@name= item ],$separator)"/>
    <xsl:param name="price" select="str:tokenize(field[@name= price ],$separator)"/>
    <xsl:param name="quantity" select="str:tokenize(field[@name= quantity ],$separator)"/>
    <xsl:param name="count" select="count($desc)"/>
    <!-- some loop goes here -->
</xsl:template>
</xsl:stylesheet>
最佳回答

一个简单的 XSLT 2. 0 样式表, 对所有项目进行循环, 根据当前位置选择相应的价格/数量, 可以像这样看 :

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="invoice">
    <xsl:variable name="fields" select="field"/>
    <invoice>
      <xsl:for-each select="tokenize(field[@name= item ],  ; )">
        <xsl:variable name="pos" select="position()"/>
        <item>
          <desc>
            <xsl:value-of select="."/>
          </desc>
          <price>
            <xsl:value-of select="tokenize($fields[@name= price ],  ; )[position()=$pos]"/>
          </price>
          <quantity>
            <xsl:value-of select="tokenize($fields[@name= quantity ],  ; )[position()=$pos]"/>
          </quantity>          
        </item>
      </xsl:for-each>
    </invoice>
  </xsl:template>
</xsl:stylesheet>

如果您想要使用 XSLT 1.0 与 EXSLT 扩展模块 < em>strings 一起使用, 样式表必须只稍稍修改 :

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="str">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="invoice">
    <xsl:variable name="fields" select="field"/>
    <invoice>
      <xsl:for-each select="str:tokenize(field[@name= item ],  ; )">
        <xsl:variable name="pos" select="position()"/>
        <item>
          <desc>
            <xsl:value-of select="."/>
          </desc>
          <price>
            <xsl:value-of select="str:tokenize($fields[@name= price ],  ; )[position()=$pos]"/>
          </price>
          <quantity>
            <xsl:value-of select="str:tokenize($fields[@name= quantity ],  ; )[position()=$pos]"/>
          </quantity>          
        </item>
      </xsl:for-each>
    </invoice>
  </xsl:template>
</xsl:stylesheet>
问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签