xsl-list
[Top] [All Lists]

RE: Re: Re: Converting specific child elements into attriut es of parent

2003-10-30 13:16:44
Dimitre,
I tried with org.apache.xalan.xslt.Process class that is shipped along with
JDK1.4.0_01.  Is there any alternative way to resolve this ?

I used same code which is as below:

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


-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev(_at_)yahoo(_dot_)com]
Sent: Thursday, October 30, 2003 12:20 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Re: Re: Converting specific child elements into attriutes
of parent


Sorry, I cannot reproduce the problem -- the transformation runs OK and
produces the expected results -- tried with Saxon 6.5.3, Xalan J 2.4.1,
MSXML4.

The result is:

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


The transformation processes "id" elements before any other elements and
does not depend on any order of the elements in the source xml document.

Which XSLT processor/version are you using?

Are you sure that you copied/pated well (exactly) the xslt code ?


=====
Cheers,

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

"Sindigi, Ganesh K" <SindiGK(_at_)LOUISVILLE(_dot_)STORTEK(_dot_)COM> wrote in 
message
news:0809DC8497D26748A7B1F36D36BC80A8AB3BD6(_at_)hqemailc(_dot_)louisville(_dot_)stortek(_dot_)com(_dot_)
..
Sorry I pasted the wrong error.  I had put some comments in xsl
transformation.  The error is coming at the line where an attribute is
created i.e., <xsl:attribute name="{name()}">.  This time I used the exact
transformation suggested by Dimitre.  The output i got is below, only
change
is in line number.

<customerList>
<customer>
file:/C:/working/XML/posting/../elmToAtt.xsl;  Line 59; Column -1; id has
an
illegal attribute: {1}
<field id="customerId">cust1</field>
file:/C:/working/XML/posting/../elmToAtt.xsl;  Line 59; Column -1; id has
an
illegal attribute: {1}
<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>


-----Original Message-----
From: Sindigi, Ganesh K
Sent: Thursday, October 30, 2003 10:37 AM
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: RE: [xsl] Re: Converting specific child elements into attriutes
of parent


Thanks to all.

One thing I noticed is the order of elements in the source.xml is also a
reason for this error :-(
For e.g., if the <id> is coming after <value>, then this error is still
coming.
As the source xml is coming from different interfaces, i do not have any
control on the order of elements :-(.  How do i resolve this?

When i ran the command "java org.apache.xalan.xslt.Process -in source.xml
-xsl ..\elmToAtt.xsl"

Dimitre I used transformation provided by you and applied on this
source.xml.
(Notice that the order <id> and <value> elements in the first two <field>s
has been interchanged)
<customerList>
  <customer>
    <field>
      <value>cust1</value>
      <id>customerId</id>
    </field>
    <field>
      <value>Customer  1</value>
      <id>customerName</id>
    </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>

which produced the following result (With two errors)

<customerList>
<customer>
file:/C:/working/XML/posting/../elmToAtt.xsl; Line 66; Column -1; id has
an
illegal attribute: {1}
<field id="customerId">cust1</field>
file:/C:/working/XML/posting/../elmToAtt.xsl; Line 66; Column -1; id has
an
illegal attribute: {1}
<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>


-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev(_at_)yahoo(_dot_)com]
Sent: Thursday, October 30, 2003 2:01 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Re: Converting specific child elements into attriutes of
parent


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

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






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

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



<Prev in Thread] Current Thread [Next in Thread>