" /> " /> "/>

XPath : Attribute Compare

449 views Asked by At

I am looking for a solution to frame xpath logic to compare attributes.

<menucontainer id="menu1" op="o3" indicator="highlight">
<menubox>
<menutitle>TITLE</menutitle>
<menuoptionlist>
<menuoption id="o1">Select a path from below :</menuoption>
<menuoption id="o2">IMAGE1</menuoption>
<menuoption id="o3">IMAGE2</menuoption>
<menuoption id="o4">IMAGE3</menuoption>
<menuoption id="o5">IMAGE4</menuoption>
<menuoption id="o6">IMAGE5</menuoption>
</menuoptionlist>
</menubox>
</menucontainer>

Here I want to compare menucontainers op value (op="03") against the menuoption id's.So here the ideal match would be IMAGE2. And I would be highlighting the IMAGE2 in red colour.

I tried //menublock/menucontainer/@op = //menublock/menucontainer/menuoptionlist/menuoption/@id

but it is not dynamic.It checks first result and applies the same result everywhere.pls help in framing the xpath .

2

There are 2 answers

1
DaveStSomeWhere On

To return the node menuoption where the value is IMAGE2, based on the menuoption attribute id being equal to the menucontainer attribute op you can use the following XPath Selector //menucontainer/menubox/menuoptionlist/menuoption[@id=../../../@op]

The ../ goes up a level. W3schools XPath Syntax

XPathFiddle

0
uL1 On

With the help of my crystal ball, i present you a solution.

XPATH: //menuoption[@id = ancestor::menucontainer/@op] [you can restrict the //menuoption as you want/needed.]

XSLT 1.0:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml"/>

    <xsl:template match="menuoption[@id = ancestor::menucontainer/@op]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <!-- add a specific attribute -->
            <xsl:attribute name="highlight">
                <xsl:text>red</xsl:text>
            </xsl:attribute>
            <xsl:apply-templates />
        </xsl:copy>        
    </xsl:template>

    <!-- identity copy -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Change the attribute highlight to whatever you want. Fit these templates in your environment. [Crystal ball got a crack]