0

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>
<ComputerType>Windows</ComputerType>
<DatabaseName />
<CPULevel>10</CPULevel>
</entry>
1
  • Could you format your question so that the XML schema can be read and provide what you have already tried Commented Apr 7, 2011 at 11:57

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Using this code: var myXslTrans = new XslCompiledTransform(); myXslTrans.Load("Files/NaughtyXSLT_Transform.xsl"); myXslTrans.Transform("Files/NaughtyXSLT_InputXML.xml","NaughtyXSLT_OutputXML.xml"); Does not work, jeez I cannot edit this comment for some reason!
0
<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.

2 Comments

There is no element named entry in the provided source XML document.
It was <AllFields> ... </entry> so I chose entry. Thanks, updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.