xsl-list
[Top] [All Lists]

Re: [xsl] Merging attributes in one XML file with node values in another (identical structure)

2006-06-22 21:37:15
Here is a Saxon (8.7.3J) specific solution:

file1.xml
------------
<a>
 <b>
    <node>1</node>
 </b>
 <c>
    <d>
       <node>2</node>
    </d>
 </c>
</a>

file2.xml
------------
<a>
 <b val="x">
    <node width="10"/>
 </b>
 <c>
    <d val="z">
       <node height="15"/>
    </d>
 </c>
</a>

merge.xsl
--------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                                          xmlns:saxon="http://saxon.sf.net/";>

   <xsl:output method="xml" indent="yes" />

   <xsl:variable name="file2" select="document('file2.xml')" />

   <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="$file2//*[saxon:path() =
current()/saxon:path()]/@*" />
        <xsl:apply-templates />
            </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

java net.sf.saxon.Transform file1.xml merge.xsl
Warning: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
<?xml version="1.0" encoding="UTF-8"?>
<a>
 <b val="x">
    <node width="10">1</node>
 </b>
 <c>
    <d val="z">
       <node height="15">2</node>
    </d>
 </c>
</a>

Regards,
Mukul

On 6/22/06, Minervini, Chris 
<christopher(_dot_)minervini(_at_)lehman(_dot_)com> wrote:
O.K. here's my question.  It seems like it should be simple but I can't
figure it out.
I have two XML files. One with data in the node values, and one with
data in attributes. They have the same exact structure.

<a>
  <b>
     <node>1</node>
  </b>
  <c>
     <d>
        <node>2</node>
     </d>
  </c>
</a>

... and ...

<a>
  <b val="x">
     <node width="10"/>
  </b>
  <c>
     <d val="z">
        <node height="15"/>
     </d>
  </c>
</a>

As you can see the word "node" node appears at /a/b/node and a/c/d/node
but has two distinct values and attributes.
Is there a way to transform the first one with XSLT to produce the
following results:

<a>
  <b val="x">
     <node width="10">1</node>
  </b>
  <c>
     <d val="z">
        <node height="15">2</node>
     </d>
  </c>
</a>

...essentially combining the node values of one file with the attributes
in the other.
I've really been struggling with this. I was able to do it with
recursion, but I'm looking for a way to do it without using recursive
procedures. I don't think it's possible. Is it?

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--