xsl-list
[Top] [All Lists]

RE: variable in xpath

2002-11-15 08:22:14
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(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Kalyan Kumar
Mudumbai
Sent: Friday, November 15, 2002 5:34 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] 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



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