[Home] [By Thread] [By Date] [Recent Entries]

Subject: Transform all tags into attributes with some tags omitted
From: "Philipp Kursawe" <phil.kursawe@xxxxxxxxx>
Date: Fri, 16 May 2008 08:46:50 +0200
Hi all!

I need to transform this rather verbose XML Output into a smaller more
compact, attribute based form. Also I do not need all tags, but wanna
be sure that, if in future a new CHECKDIGIT4 field will be added that
my XSLT is still transforming this new tag. I can use XSLT 2.0 or 1.0.
I have marked the fields I do not need from the source XML. Find below
the expected XML output.
<OUTPUT>
 <ES_HEAD>
   <TANUM>0000057779</TANUM>
   <QTY_LINES>0005</QTY_LINES>
   <WEIGHT>43.879</WEIGHT>
   <GEWEI>KG</GEWEI> <!-- NOT NEEDED -->
   <VOLUME>0</VOLUME>
   <VOLEH></VOLEH> <!-- NOT NEEDED -->
   <INUSE></INUSE>
   </ES_HEAD>
   <ET_ITEMS>
     <item>
       <TAPOS>0001</TAPOS>
       <LGPLA>01 02 0</LGPLA>
       <CORRIDOR>01</CORRIDOR> <!-- NOT NEEDED -->
       <PLATZ> 02 0</PLATZ> <!-- NOT NEEDED -->
       <MATERIAL>1993924600</MATERIAL>
       <MATKTXT></MATKTXT>
       <CHECKDIGIT>902</CHECKDIGIT>
       <CHECKDIGIT2>600</CHECKDIGIT2>
       <CHECKDIGIT3></CHECKDIGIT3>
       <VSOLM>9.000</VSOLM>
       <MEINS>ST</MEINS> <!-- NOT NEEDED -->
       <PACKGROESSE>0</PACKGROESSE>
     </item>
     ...more <item/>
   </ET_ITEMS>
</OUTPUT>

The expected output should be looking like this:
<OUTPUT>
  <ES_HEAD tanum="0000057779" qty_lines="0005" ../>
  <ES_ITEMS>
    <item tapos="0001" lgpla="01 02 0" ... />
    <item tapos="0002" lgpla="02 04 0" ... />
  </ES_ITEMS>
</OUTPUT>

I have created this XSTL:

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

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

<xsl:template match="item">
<xsl:copy>
<xsl:for-each select="TAPOS|LGPLA|MATERIAL|MATKTXT|CHECKDIGIT|CHECKDIGIT2|CHECKDIGIT3|VSOLM|MEINS|PACKGROESSE"><xsl:call-template
name="item-element" /></xsl:for-each>
</xsl:copy>
</xsl:template>

<xsl:template name="item-element">
<xsl:attribute name="{name()}" ><xsl:value-of select="." /> </xsl:attribute>
</xsl:template>

</xsl:stylesheet>


However I do not like the select statement for the tags. I would
rather like to describe the exceptions so that tag added in the future
are automatically added as attributes without changing the xslt.

Thanks in advance,
Phil

Current Thread
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member