XSL 名称值对转换 [英] XSL Name Value Pair transformation

查看:26
本文介绍了XSL 名称值对转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是否可能,但就是这样.

I am not sure it's even possible but here it goes.

来自这个 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 转换将其转换为以下内容:

I am looking to convert it to the following using XSL transformation:

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

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

I can get the attribute names but not sure how to form the XML. Here's what I tried.

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

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

So, is it possible to do what I am looking for? Any help? Thanks

推荐答案

应该这样做:

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

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

When run on your sample data, the result is:

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

这篇关于XSL 名称值对转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆