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

RE: variable in xpath

Subject: RE: variable in xpath
From: "Américo Albuquerque" <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Fri, 15 Nov 2002 15:22:14 -0000
xsl variable in xpath
Hi Kalyan.

Try this:
 <xsl:variable name="path" select="'/element/child/a'"/> <!-- have to be
full path (ie. With root node) -->

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

 <xsl:template match="*"> <!-- a simply identity template with a twist
-->
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <xsl:call-template name="copy"/>
   <xsl:apply-templates/>
  </xsl:copy>
 </xsl:template>

 <xsl:template name="copy">
  <xsl:variable name="ancestors">
  <xsl:call-template name="build-tree"/> <!-- get path string -->
  </xsl:variable>
  <xsl:if test="$ancestors=concat($path,'/')">
   <xsl:variable name="path" select="ancestor::* | self::*"/>
   <xsl:for-each select="$doc2">
    <xsl:call-template name="copy-of">
     <xsl:with-param name="node" select="$path"/>
    </xsl:call-template>
   </xsl:for-each>
  </xsl:if>
 </xsl:template>

 <xsl:template name="copy-of">
  <xsl:param name="node" select="."/>
  <xsl:param name="name" select="name($node[1])"/>
  <xsl:param name="pos"
select="count($node[1]/preceding-sibling::*)+1"/>

  <xsl:choose>
   <xsl:when test="count($node)>0">
   <xsl:for-each select="*[name()=name($node[1])][position()=$pos]">
<!-- this will change the current node -->
    <xsl:call-template name="copy-of"> <!-- procede to the next node in
the list -->
     <xsl:with-param name="node" select="$node[position()&gt;1]"/>
     <xsl:with-param name="name" select="name($node[1])"/>
    </xsl:call-template>
   </xsl:for-each>
   </xsl:when>
   <xsl:otherwise>
   <xsl:if test="name()=$name"> <!-- if we stop at the right node then
copy all children -->
   <xsl:copy-of select="./*"/>
   </xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="build-tree"> <!-- concat the ancestor's names so we
can compare them with the one suplied -->
  <xsl:param name="node" select="."/>
  <xsl:param name="path" select="''"/>
  <xsl:choose>
   <xsl:when test="$node/..">
   <xsl:call-template name="build-tree">
    <xsl:with-param name="node" select="$node/.."/>
    <xsl:with-param name="path" select="concat(name($node),'/',$path)"/>
   </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="concat('/',$path)"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

Hope that this helps you.


-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Kalyan Kumar
Mudumbai
Sent: Friday, November 15, 2002 5:34 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  variable in xpath


I have two xml files with exactly similar hierarchy, but for a few
different element nodes. Something like the following:

File1.xml
<element>
    <child>
       <a>
       <b>
    </child>
</element>

File2.xml
<element>
    <child>
      <a>
       <adesc>
      </a>
    </child>
</element>

I have a variable $path that holds an element's (in File1) xpath
as if it was assinged by one the following:

<xsl:variable name="path" select="'element/child/a'"/>

How do I obtain and copy the node <adesc> of File2 using the above
$path variable? Something like the below one:

<xsl:template match="node()">
    <xsl:copy-of select="document(File2.xml)/$path.."/>
</xsl:template>

Output has to be smth like this:
<element>
    <child>
      <a>
       <adesc>
      </a>
      <b>
    </child>
</element>

Thanks,
Kalyan

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



 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.