使用多个分隔符拆分XML数据

时间:2013-07-08 10:47:32

标签: xml parsing xslt tokenize delimiter

我有一个XML标签,其中包含一个项目列表,使用多个分隔符,例如:

<List>1;Black;Colour;Smart,2;White;Colour;TV,3;Yellow;Pillow;Home</List>

我需要使用XSLT(首选2.0)将值拆分为以下格式:

<LIST>
 <LIST_ITEM id="1" value="Black" type="Colour" usedIn="Smart"/>
 <LIST_ITEM id="2" value="White" type="Colour" usedIn="TV"/>
 <LIST_ITEM id="3" value="Yellow" type="Pillow" usedIn="Home"/>
</LIST>

对于单独的列表项,分隔符为,,对于单独的单独条目,分隔符为;。每个列表项中只有4个值。

我猜missize()是最有效的方法,但不知道怎么做。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

以下是一个示例:

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output indent="yes"/>

<xsl:template match="List">
  <LIST>
    <xsl:for-each select="tokenize(., ',')">
      <xsl:variable name="items" select="tokenize(., ';')"/>
      <LIST_ITEM id="{$items[1]}" value="{$items[2]}" type="{$items[3]}" usedIn="{$items[4]}"/>
    </xsl:for-each>
  </LIST>
</xsl:template>

</xsl:stylesheet>

它首先在逗号上标记,分号为分号。