【问题标题】:Split XML data using multiple delimiters使用多个分隔符拆分 XML 数据
【发布时间】:2013-07-08 10:47:32
【问题描述】:

我在 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 个值。

我猜 tokenize() 是最有效的方法,但不知道怎么做。谁能帮帮我?

【问题讨论】:

    标签: xml parsing xslt tokenize delimiter


    【解决方案1】:

    这是一个示例:

    <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>
    

    它首先用逗号标记,然后用分号标记。

    【讨论】:

    • 如果我想声明这个模板,并从一个对象中调用它,我该怎么做?
    • 好吧,您不会在 XSLT 中调用此类模板,而是使用 xsl:apply-templates 将它们推送到 XSLT 处理器。但是内置模板会为您执行此操作,因此上述代码是完整的。而且我不知道在 XSLT 的上下文中“从对象中调用它”应该是什么意思。你需要解释一下。
    • 好的,我删除了模板的东西,它工作了。我也在使用其他属性。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多