XML文件结构更改/转换

时间:2014-04-10 10:03:56

标签: xml xslt translation

我认为这应该相当容易,但似乎我在我面前有更多的文档只是为了理解简单转换XML的原则。我想改变XML的结构:

   <Feature>
        <Property>
            <Name>ID</Name>
                <Value>761153</Value>
        </Property>
        <Property>
            <Name>TITLE</Name>
                <Value>The Title</Value>
        </Property>
   </Feature>

到这个

   <Feature>
        <ID>761153</ID>
        <TITLE>The Title</TITLE>
   </Feature>

我相信我可以用XSLT做到这一点,我只是不知道从哪里开始。我将非常感谢能够帮助我解释的解决方案或指针。

2 个答案:

答案 0 :(得分:2)

请参阅http://www.xmlplease.com/xsltidentity,您需要两个模板

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

<xsl:template match="Property">
  <xsl:element name="{Name}"><xsl:value-of select="Value"/></xsl:element>
</xsl:template>

答案 1 :(得分:0)

你可以使用以下样式表(在评论中有解释)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <Feature>
            <!-- loop through each Property element -->
            <xsl:for-each select="Feature/Property">
                <!-- element's name is the value of child node Name -->
                <xsl:element name="{Name}">
                    <!-- the value is the content of the child Value-->
                    <xsl:value-of select="Value"/>
                </xsl:element>
            </xsl:for-each>
        </Feature>
    </xsl:template>

</xsl:stylesheet>