[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Converting specific child elements into attriutes

Subject: Re: Converting specific child elements into attriutes of parent
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Thu, 30 Oct 2003 10:01:01 +0100
xsl strip space elements
> I have been trying to convert specific child elements into attributes of
the
> parent node.  I looked through archive, there was a topic of converting
all
> children to attributes of the root.  I followed the same thing and tried
to
> convert a specific child element, but i am getting following error.
> elmToAtt.xsl; Line 18; Column -1; name() has an illegal attribute: {1}
> I would like to copy the xml document and convert only specific elements
> into attributes.
> I am not able to make out why this error is, but to no avail.  Hoping that
> anybody in the list would help to resolve .

The specific error can be corrected by using AVT like this:

name="{name()}"

Here's a transformation, which converts any element, whose name is in a list
of names, to an identically named attribute of its parent:


<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my"
 exclude-result-prefixes="my"
 >

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:elNames>
   <name>id</name>
 </my:elNames>

 <xsl:variable name="elNames"
      select="document('')/*/my:elNames/name"/>

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

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test="not(name() = $elNames)">
        <xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates select="*[name() = $elNames]"/>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="value">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on your original source.xml:

<customerList>
  <customer>
    <field>
      <id>customerId</id>
      <value>cust1</value>
    </field>
    <field>
      <id>customerName</id>
      <value>Customer  1</value>
    </field>
    <fieldGroup>
      <id>homeAddress</id>
      <fieldList>
        <field>
          <id>street</id>
          <value>98th  Street </value>
        </field>
        <field>
          <id>city</id>
          <value>Chicago</value>
        </field>
      </fieldList>
    </fieldGroup>
    <fieldGroup>
      <id>companyAddress</id>
      <fieldList>
        <field>
          <id>street</id>
          <value>128th  Street</value>
        </field>
        <field>
          <id>city</id>
          <value>Chicago</value>
        </field>
      </fieldList>
    </fieldGroup>
  </customer>
</customerList>

the wanted result is produced:

<customerList>
   <customer>
      <field id="customerId">cust1</field>
      <field id="customerName">Customer  1</field>
      <fieldGroup id="homeAddress">
         <fieldList>
            <field id="street">98th  Street </field>
            <field id="city">Chicago</field>
         </fieldList>
      </fieldGroup>
      <fieldGroup id="companyAddress">
         <fieldList>
            <field id="street">128th  Street</field>
            <field id="city">Chicago</field>
         </fieldList>
      </fieldGroup>
   </customer>
</customerList>


Hope this helped.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.