Need XSLT transformation to convert Input XML to Output XML
HI,
I am looking to convert input XML to OUTput XML using XSLT.
Input XML is as below
<ALLFields id="0001">
<field name="ComputerName">ABC</field>
<field name="ComputerType">Windows</field>
<field name="DatabaseName" />
<field name="CPULevel">10</field>
</ALLFields>
OUTPUT XML what i need is
<entry id="0001">
<ComputerName>ABC</ComputerName>
开发者_C百科<ComputerType>Windows</ComputerType>
<DatabaseName />
<CPULevel>10</CPULevel>
</entry>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="ALLFields">
<entry id="{@id}">
<xsl:apply-templates select="node()"/>
</entry>
</xsl:template>
<xsl:template match="field">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (corrected to be well-formed):
<ALLFields id="0001">
<field name="ComputerName">ABC</field>
<field name="ComputerType">Windows</field>
<field name="DatabaseName" />
<field name="CPULevel">10</field>
</ALLFields>
produces the wanted, correct result:
<entry id="0001">
<ComputerName>ABC</ComputerName>
<ComputerType>Windows</ComputerType>
<DatabaseName/>
<CPULevel>10</CPULevel>
</entry>
<xsl:template match="AllFields">
<entry id="{@id}">
<xsl:for-each select="field">
<xsl:element name="{@name}">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:for-each>
</entry>
</xsl:template>
Not tested though.
精彩评论