【问题标题】:XSLT rename element with attribute valueXSLT 用属性值重命名元素
【发布时间】:2016-06-19 05:19:44
【问题描述】:

我是 xslt 的新手。我试图用属性名称重命名我的 .xml 的元素名称,并删除该属性。

这是我要转换的 XML 示例:

<configdata>  
 <element xsi:type="AAA">
  <attributes>
     <att1>0</att1>
     <att2>1</att2>
     <att3>25</att3>
  </attributes>
 </element>
 <element xsi:type="BBB">
  <attributes>
     <att4>23</att4>
     <att5>44</att5>
     <att6>12</att6>
  </attributes>
 </element>
</configdata>

想要的输出:

<configdata> 
 <AAA>
  <attributes>
     <att1>0</att1>
     <att2>1</att2>
     <att3>25</att3>
  </attributes>
 </AAA>
 <BBB>
  <attributes>
     <att4>23</att4>
     <att5>44</att5>
     <att6>12</att6>
  </attributes>
 </BBB>
</configdata>

xml 包含数百个元素(AAA、BBB、CCC、DDD...),因此,任何通用解决方案都会很棒。

我已尝试使用以下 xslt 代码,但在输出中我保持输入 xml 不变。

<?xml version="1.0"?>
<xsl:stylesheet 
version="1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="element">
 <xsl:element name="{@xsi:type}">
  <xsl:value-of select="."/>
 </xsl:element>
</xsl:template>

</xsl:stylesheet>

我将不胜感激。谢谢

【问题讨论】:

    标签: xml xslt


    【解决方案1】:
    <xsl:template match="element">
     <xsl:element name="{@xsi:type}">
      <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
    </xsl:template>
    

    您想要处理&lt;element&gt; 的所有子代。但是,&lt;xsl:value-of select="."/&gt; 并没有这样做。它所做的只是输出元素的文本内容。

    【讨论】:

    • 嗨,Tomalak,感谢您的回答。我试过: w3.org/2001/XMLSchema-instance" xmlns:xsl="w3.org/1999/XSL/Transform"> 但在输出中我只得到属性的 de 值.. 0125 234412
    • 我没有说你应该放弃身份模板。 ;)
    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2010-10-27
    • 2020-04-04
    相关资源
    最近更新 更多