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

Subject: [xsl] Re: Re: Converting specific child elements into att riutes of parent
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 1 Nov 2003 12:04:52 +0100
> I would guess that the stylesheet was run on a document where <id>
> wasn't a first child, that would generate the error quoted.
>

Not really. I rearranged the source xml document like this:

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


and using the same stylesheet:

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

 I get the correct output:

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

I used Saxon 6.5.3, Msxml3/4, and  .Net xsltTransform without any problem.

Xalan J 2.4.1 and Xalan C 1.5 also produce this output, but issue several of
the following messages:

"Cannot add attribute id after child nodes or before an element is produced.
Attribute will be ignored."


To prove that the reason is elesewhere I modified the code as recommended by
you:

replaced:

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

with:

    <xsl:copy>
      <xsl:apply-templates select="*[name()=$elNames]"/>
      <xsl:apply-templates select="@* | node()[not(name()=$elNames)]"/>
    </xsl:copy>

There was no effect. XalanJ 2.4.1 still produces the correct result issuing
the same several error messages as before the code modification.

Therefore the question about the strange error messages issued by XalanJ
remains open.


Could somebody, please, explain them?



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