Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

XSLT 2.0 match attribute of parent to attribute somewhere else

I have some XML data that looks something like this:
<A>
    <B>
        <C href="#hello"/>
    </B>
    ...
    <X id="hello">
        <Y>
        ...

Open in new window

I'm running an XSLT 2.0 transformation, and I'm on node <Y>. I know there's a parent node <X> above me with an id= attribute, and I suspect there's a section <B> above which has  a bunch of child nodes, not necessarily <C> nodes, they may be anything, but they may have an href= attribute that points to the <X id=...> above me.

I now want to test if the id= above me matches any href= in section <B> above. (To make it simple there's only one level below <B>.)

[Earlier when I was on node <X>, I was able to write the following test:]
<xsl:when test="/A/B/*[./@href = current()/concat('#',@id)]

Open in new window

Pretty close to what I now need, except I need to replace current() with parent(), or parent::, and that's where I'm losing the syntax.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

you need the current() to maintain your current context
Try
<xsl:when test="/A/B/*[./@href = current()/parent::*/concat('#',@id)]">

but I would use a key
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NodeList nodelist=doc.geyElementByTagName(A);
for(int i=0;i<nodelist.getLenght();i++)
{
Node node=nodelist.item(i);
NodeList nl=node.getchildNode();
for(int j=0;j<nl.getlenght();j++)
{
Node nodes=nl.item(j);
if(nodes.getnodename().equals("B")
{
//do Operation for B
}
if(nodes.getchildnode().equals("X))
{
//Do Opration for X
}

Open in new window

@manoj kumar
interesting XSLT 2.0 ;-)
Avatar of deleyd

ASKER

Works like a charm! Thank you!