[xsl] Re: Converting specific child elements into att riutes of parent

Subject: [xsl] Re: Converting specific child elements into att riutes of parent
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 1 Nov 2003 07:21:27 +0100
> > file:///C:/working/XML/elmToAtt.xsl; Line #31; Column #5; Cannot add
> > attribute id after child nodes or before an element is produced.
> Attribute
> > will be ignored.
>
> You've tried to use <xsl:attribute>, or to copy an attibute node drom some
> other source, after non-attribute content has been added to the element.
> XSLT doesn't permit that; you have to write the stylesheet to prevent it
> from happening.

I don't have XalanJ 2.5.1

The transformation below works without any problem when run with XalanJ
2.4.1, Saxon 6.5.3, MSXML3, etc.

If it produces an error with XalanJ 2.5.1 then this may be a bug  with this
XSLT processor or otherwise all other XSLT processors are non-compliant:

<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 applied on this 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>


So, is issuing an error message by XalanJ 2.5.1 a bug?



=====
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