tags:

views:

1188

answers:

6

We have a current system that outputs an XML file which is in the following format:

<INVENTORY>
   <ITEM>
      <SERIALNUMBER>something</SERIALNUMBER>
      <LOCATION>something</LOCATION>
      <BARCODE>something</BARCODE>
   </ITEM>
</INVENTORY>

I need to use this data to load into the standard .NET 2.0 grid. But the grid needs the XML to be in the following format:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something">
   </ITEM>
</INVENTORY>

i.e. the child nodes of item need to be converted into attributes of the item node.

Does someone know how this can be done using XSLT?

A: 

This ought to do it:

  <xsl:for-each select="//ITEM">
    <xsl:element name="ITEM">
      <xsl:attribute name="serialNumber">
        <xsl:value-of select="SERIALNUMBER"/>
      </xsl:attribute>
      <xsl:attribute name="location">
        <xsl:value-of select="LOCATION"/>
      </xsl:attribute>
      <xsl:attribute name="barcode">
        <xsl:value-of select="BARCODE"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:for-each>

Or using David's shortcut:

<xsl:for-each select="//ITEM">
  <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}"/>
</xsl:for-each>
Welbog
+1  A: 

If your source looks like this:

<row><a>1</a><b>2</b></row>

and you want it to look like this:

<row a="1" b="2" />

then this XSLT should work:

<xsl:template match="row">
    <row a="{a}" b="{b}" />
</xsl:template>
David
+1  A: 

These two templates should do it:-

<xsl:template match="ITEM">
   <ITEM serialNumber="{SERIALNUMBER}" location="{LOCATION}" barcode="{BARCODE}" />
</xsl:template>

<xsl:template math="INVENTORY">
   <INVENTORY>
      <xsl:apply-templates />
   </INVENTORY>
</xsl:template>
AnthonyWJones
A: 
<xsl:template match="INVENTORY | ITEM">
  <xsl:copy>
    <xsl:apply-templates select="*" />
  </xsl:copy>
</xsl:template>

<xsl:template match="ITEM/*">
  <xsl:attribute name="{local-name()}">
    <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>

To enforce lowercase attribute names:

<xsl:template match="ITEM/*">
  <xsl:variable name="uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
  <xsl:variable name="lc" select="'abcdefghijklmnopqrstuvwxyz'" />
  <xsl:attribute name="{translate(local-name(), $uc, $lc)}">
    <xsl:value-of select="." />
  </xsl:attribute>
</xsl:template>
Tomalak
wtf? This reads "something something something". Did you want to get first post without actually posting anything?
phihag
Yeah. I mis-typed and hit enter accidentally without having my post ready. The short moment that it took to correct it was enough to be down-voted already. It's a pity people don't have the *slightest amount* of patience before down-voting something that was clearly an accident.
Tomalak
+3  A: 

That should work:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  <xsl:template match="INVENTORY">
    <INVENTORY>
      <xsl:apply-templates/>
    </INVENTORY>
  </xsl:template>

  <xsl:template match="ITEM">
    <ITEM>
      <xsl:for-each select="*">
        <xsl:attribute name="{name()}">
          <xsl:value-of select="text()"/>
        </xsl:attribute>

      </xsl:for-each>
    </ITEM>
  </xsl:template>
</xsl:stylesheet>

HTH

Johannes Weiß
Thanks for all your suggestions but this one seems to be the simplest and most dynamic solution :) and worked for me :)
eMTeeN
A: 

Here is probably the simplest solution that will convert any children-elements of ITEM to its attributes and will reproduce everything else as is, while converting the element names to any desired attribute names:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                              --> 
  <xsl:strip-space elements="*"/>

  <xsl:variable name="vrtfNameMapping">
    <item name="SERIALNUMBER" newName="serialNumber"/>
    <item name="LOCATION" newName="location"/>
    <item name="BARCODE" newName="barcode"/>
  </xsl:variable>
 <!--                                              --> 
  <xsl:variable name="vNameMapping" select=
  "document('')/*/xsl:variable[@name='vrtfNameMapping']"/>
<!--                                              --> 

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                              --> 
  <xsl:template match="ITEM/*">
    <xsl:attribute name=
     "{$vNameMapping/*[@name=name(current())]/@newName}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

when the above transformation is applied on the provided XML document:

<INVENTORY>
    <ITEM>
     <SERIALNUMBER>something</SERIALNUMBER>
     <LOCATION>something</LOCATION>
     <BARCODE>something</BARCODE>
    </ITEM>
</INVENTORY>

the wanted result is produced:

<INVENTORY>
   <ITEM serialNumber="something" location="something" barcode="something"/>
</INVENTORY>

Do note the following:

  1. The use of the identity rule

  2. The use of <xsl:strip-space elements="*"/>

  3. The use of the variable vrtfNameMapping without any xxx:node-set() extension function.

  4. The fact that we handle any mapping between a name and a newName, not only simple lower-casing.

Dimitre Novatchev