Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XPATH Nearest Node

From: Soren Kuula <dongfang-remove_this@------_--------------.--->
To: NULL
Date: 4/10/2005 9:50:00 PM
nkunapa wrote:
> Hi:
>  Is there a way in XPATH to find the nearest node of the node in
> context with a certain attribute value. Here is my problem. I have the
> following XML and I am trying to add all the nodes with attribute value
> 
> LNum=1 as child nodes of the nearest node above it with attribute
> LNum=0....and add all the nodes with attribute value LNum=2 as child
> nodes of the nearest node above it with attribute LNum=1 and so on. The

Hi,

Have you thought about selecting all suitable nodes in the 
preceding-sibling axis, and then pick the first of them? That should be 
the nearest above (unless I have messed up something). The FIRST is 
becuase the preceding-sibling is reverse; the first is the nearest to 
the context.

preceding-sibling::*[@LNum=$MyLNumMinus1][1]

I tried it in an XSLT (just because that's all I have working right now 
for evaluating XPath):


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">

<xsl:template match="thing">
   <xsl:variable name="myLNumMinus1" select="@LNum - 1"/>
   <xsl:variable name="catch" 
select="preceding::*[@LNum=$myLNumMinus1][1]/@name"/>
     <xsl:if test="$catch">
     This node had a suitable ancestor:
     <thing myName="{@name}" myLNum="{@LNum}" 
ImLookingFor="{$myLNumMinus1}" 
my-nearest-predecessor-with-that-LNum="{$catch}"/>
     </xsl:if>
   <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>


For input file:

<?xml version="1.0"?>
<thing name="ancestor0" LNum="0">
   <thing name="ancestor1" LNum="0">
     <thing name="ancestor2" LNum="0">
       <thing name="a-predecessor-but-not-an-ancestor-to-the-successor" 
LNum="1"/>
       <thing name="depth3" LNum="1">
         <thing name="depth4" LNum="2"/>
         <thing name="depth4, again" LNum="1"/>
       </thing>
       <thing name="depth3, again" LNum="2"/>
     </thing>
   </thing>
</thing>

I got
[dongfang@granada nearest-ancestor]$ xsltproc nearest-predecessor.xsl 
things.xml
<?xml version="1.0"?>
     This node had a suitable ancestor:
     <thing myName="depth4" myLNum="2" ImLookingFor="1" 
my-nearest-predecessor-with-that-LNum="a-predecessor-but-not-an-ancestor-to-the-successor"/>
     This node had a suitable ancestor:
     <thing myName="depth3, again" myLNum="2" ImLookingFor="1" 
my-nearest-predecessor-with-that-LNum="depth4, again"/>
[dongfang@granada nearest-ancestor]$


With your "flat" documents it doesn't matter whether you use precesing 
or preceding-sibling; it's the same.

Soren



transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent