1
votes

I am trying to get a child node value based on a specific parent node value, but I am struggling to get the syntax right…

This is my XML document:

    <result>
        <heading>A</heading>
        <messages>
            <message>
                <heading>D</heading>
                <ingress>ABCD</ingress>
            </message>
        </messages>
    </result>

I am trying to get the ingress “ABCD” when the heading value is A.

This is my code.

<xsl:if test="result">  
    <xsl:variable name="parent" select="heading"/>  
    <xsl:value-of select="//message[heading=$parent]"/>  
</xsl:if>  
1

1 Answers

1
votes

I am trying to get the ingress “ABCD” when the heading value is A.

Try:

<xsl:value-of select="/result[heading='A']/messages/message/ingress" />

Note: The above uses the absolute path to the ingress element, starting at the root node. Depending on where you are when you need this, you can use a relative (shorter) path to the same node.