I am thinking this should be fairly easy, but it seems that I have a lot more documentation in front of me to read just to understand the principles od doing the simple transfromation of XML. I would like to change the structure of XML from this:

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

to this

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

I believe I could do this with XSLT, I just don't know where to start. A solution or pointer to an explanation that would help me out would be greatly appreciated.

有帮助吗?

解决方案 2

you could use the following stylesheet (with explanation in comments)

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

其他提示

See http://www.xmlplease.com/xsltidentity, you need two templates

<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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top