【问题标题】:XSL - transform array elements into a list of independent elements with element_name+index namesXSL - 将数组元素转换为具有 element_name+index 名称的独立元素列表
【发布时间】:2014-09-13 20:10:21
【问题描述】:

我对 XSL 转换有非常基本的了解。 我需要将数组转换为独立元素的列表,如下所示:

输入:

<Fields>
    <Field>
         <Name>One</Name>
         <Value>1</Value>
    </Field>
    <Field>
        <Name>Two</Name>
        <Value>2</Value>
    </Field>
    <Field>
        <Name>Three</Name>
        <Value>3</Value>
    </Field>
    <Field>
        <Name>Four</Name>
        <Value>4</Value>
    </Field>
    </Fields>

期望的输出:

<Fields>
     <Field1>
            <Name>One</Name>
            <Value>1</Value>
        </Field1>
        <Field2>
            <Name>Two</Name>
            <Value>2</Value>
        </Field2>
        <Field3>
            <Name>Three</Name>
            <Value>3</Value>
        </Field3>
        <Field4>
            <Name>Four</Name>
            <Value>4</Value>
        </Field4>
</Fields>

真的可行吗? 感谢任何建议。

【问题讨论】:

  • 可行,是的。然而,在去那里之前你应该三思而后行,因为虽然我看到的变化没有带来任何好处,但它会使结果比原来的更难处理。 -- 另请注意,您的输出缺少每个 XML 文档都必须具有的根元素。
  • 输出不是有效的 XML,因为它有多个根元素。
  • 另外,您的 XML 中的元素实际上是否称为“字段”,还是您正在寻找更通用的解决方案?
  • 很抱歉缺少根元素,确保它应该有一个。它不是生产 xml,只是说明问题的人工示例。这个问题背后的真正原因是,如果字段名称不是由静态 xml 节点表示的,消费者的软件无法映射字段。

标签: arrays xml xslt transformation


【解决方案1】:

来自 michael.hor257k 的回答效果很好,并且完全回答了我原来的问题,但我意识到这个转换的输出更适合我的需要,只要字段名称是唯一的。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/*">
    <Fields>
      <xsl:apply-templates select="Field" />
    </Fields>
  </xsl:template>

  <xsl:template match="Field">
    <xsl:element name="{Name}">

     <xsl:element name="Value">
      <xsl:value-of select="Value" />
      </xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0"?>
<Fields>
    <One>
        <Value>1</Value>
    </One>
    <Two>
        <Value>2</Value>
    </Two>
    <Three>
        <Value>3</Value>
    </Three>
    <Four>
        <Value>4</Value>
    </Four>
</Fields>

【讨论】:

    【解决方案2】:

    这个问题背后的真正原因是消费者的软件 如果字段的名称不是由静态 xml 表示,则无法映射字段 节点。

    我不确定这到底是什么意思,但如果您确定这是您想要的结果,请尝试:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Field">
        <xsl:element name="Field{position()}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 太棒了!奇迹般有效。我还在这里找到了另一个类似的解决方案 - stackoverflow.com/questions/18190322/… 。它将 元素转换为相应的 {Name} 元素。采纳了我的问题并将其作为答案。
    猜你喜欢
    • 2012-03-05
    • 1970-01-01
    • 2018-07-20
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    相关资源
    最近更新 更多