2016-03-05 76 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