2016-03-05 79 views
0

我是新的xslt。并试图使用属性名称重命名我的.xml的元素名称,并删除atttribute。具有属性值的XSLT重命名元素

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

我将不胜感激任何帮助。谢谢

回答

0
<xsl:template match="element"> 
<xsl:element name="{@xsi:type}"> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:element> 
</xsl:template> 

您想要处理<element>的所有子项。但是,<xsl:value-of select="."/>不这样做。它所做的只是输出元素的文本内容。

+0

嗨,Tomalak,谢谢你的回答。我试过了:<?xml version =“1.0”?> \t但是在输出中我只能得到de的值atributes .. 0125 234412 –

+0

我没有说你应该放弃身份模板。 ;) – Tomalak