0

Im trying to get the parent node based of child element value

I want to do something like that get TransactionDetail element where SequenceNumber = 2

I tried to do it like that but im missing something...

 ancestor:://*[local-name()='SequenceNumber'][text()=2]

 //TransactionDetail[//SequenceNumber = 2]

this is the xml

<Root>
<biztalk_1>

    <body>
        <ActiveStore_SalesTransaction_1.70>

            <TransactionDetail>
                <SequenceNumber>2</SequenceNumber>

                <TransactionDetailGroup>
                    <Item>
                        <ItemID>22</ItemID>

                        <NonMerchandiseFlag>1</NonMerchandiseFlag>
                    </Item>
                </TransactionDetailGroup>
            </TransactionDetail>




        </ActiveStore_SalesTransaction_1.70>
    </body>
</biztalk_1>

2 Answers 2

1

Using name() is not a good practice. Instead, use the element name in the xpath as below:

//TransactionDetail[SequenceNumber = 2]

The xpath selects all the TransactionDetail elements with a child SequenceNumber with value 2 in the whole document.

And if you want TransactionDetail with any descendant SequenceNumber with value 2, use:

//TransactionDetail[.//SequenceNumber = 2]

Here, the .//SequenceNumber makes sure that only the current TransactionDetail's descendant SequenceNumber elements are checked.

Sign up to request clarification or add additional context in comments.

1 Comment

@MIkCode There could be many reasons for this. I'm pointing some here. 1. The processing is much faster(depending of the processor). Some processors may list all the descendants first (because of *), and then filter based on name(). 2. The Xpath is easy to write without name(). Moreover, there is no necessity for using name() in such xpaths unless you want to escape the namespaces.
1

Try this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Root">
    <Root>
        <xsl:copy-of select="descendant::*[name()='TransactionDetail']
                                                        [descendant::*[name()='SequenceNumber']='2']"/>
    </Root>
</xsl:template>

</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.