【问题标题】:XSL Name Value Pair transformationXSL 名称值对转换
【发布时间】:2013-08-12 14:58:42
【问题描述】:

我什至不确定这是否可能,但它就是这样。

从此 XML:

<?xml version="1.0" encoding="UTF-8"?>
<AttributesCollection>
    <Attributes>
        <AttributeName>AAA</AttributeName>
        <AttributeValue>Value1</AttributeValue>
    </Attributes>
    <Attributes>
        <AttributeName>BBB</AttributeName>
        <AttributeValue>Value2</AttributeValue>
    </Attributes>
</AttributesCollection>

我希望使用 XSL 转换将其转换为以下内容:

<Attributes>
   <AAA>Value1</AAA>
   <BBB>Value2</BBB>
</Attributes>

我可以获取属性名称,但不确定如何形成 XML。这是我尝试过的。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="./AttributesCollection/Attributes/AttributeName">
            Name:<xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

哪个给我:

<?xml version="1.0" encoding="UTF-8"?>
            Name:AAA
            Name:BBB

那么,有可能做我正在寻找的东西吗?有什么帮助吗?谢谢

【问题讨论】:

    标签: xml xslt transformation


    【解决方案1】:

    应该这样做:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/*">
        <Attributes>
          <xsl:apply-templates select="Attributes" />
        </Attributes>
      </xsl:template>
    
      <xsl:template match="Attributes">
        <xsl:element name="{AttributeName}">
          <xsl:value-of select="AttributeValue" />
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    在您的示例数据上运行时,结果是:

    <Attributes>
      <AAA>Value1</AAA>
      <BBB>Value2</BBB>
    </Attributes>
    

    【讨论】:

    • 非常感谢。这正是我一直在寻找的。我不确定这是否可能,您已经提供了正确的解决方案。做得好。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多