0

following is my XML where i need to find Changeset attribute value of (Id) using <id> value of <task>/id value using Xpath.

i have tried the followin query "ReleaseNotes/ChangeSets/ChangeSet[Comments/checkintext/task/id ='" + jiraId + "']" where jiraId is <task>id value

<?xml version="1.0" encoding="utf-8"?>
<ReleaseNotes>
    <AxVersion>6.2.1000.1437</AxVersion>
    <FromDate>2/1/2015 12:00:00 AM</FromDate>
    <ToDate>2/26/2015 12:00:00 AM</ToDate>
    <ChangeSets>
        <ChangeSet Id="3600">
            <CheckInUserId>XXX\yy</CheckInUserId>
            <Comments>
                <checkintext>
                    <bpcheckdeviated>true</bpcheckdeviated>
                    <task>
                        <id>CS-2215</id>
                        <name>Small performance improvements in hedge qty lookup.</name>
                        <version>V6.0</version>
                        <sprint>17.8</sprint>
                        <project>Cs2215_Mpv_HedgeQtyLookupPerformance</project>
                        <area>Others</area>
                        <taskstatus>Closed</taskstatus>
                    </task>
                    <text>
                        Few small performance improvements in queries.
                    </text>
                    <notes>
                        <note>
                            <section>Info</section>
                            <component></component>
                            <text></text>
                        </note>
                    </notes>
                </checkintext>
            </Comments>
            <Components>
                <Component>
                    <Name>CsMpvCalcMatQtyHedgeBase.xpo</Name>
                    <Type>Classes</Type>
                    <Action>Edit</Action>
                    <Models>
                        <Model Id="I4C" Layer="ISV" />
                    </Models>
                </Component>
                <Component>
                    <Name>CsMpvInsertMatQtyDetails_Agreement.xpo</Name>
                    <Type>Classes</Type>
                    <Action>Edit</Action>
                    <Models>
                        <Model Id="I4C" Layer="ISV" />
                    </Models>
                </Component>
                <Component>
                    <Name>Cs2215_Mpv_HedgeQtyLookupPerformance.xpo</Name>
                    <Type>Shared</Type>
                    <Action>Add</Action>
                    <Models />
                </Component>
            </Components>
        </ChangeSet>
    </ChangeSets>
</ReleaseNotes>

HOw to find the attribute value Thanks in advance..

4
  • Do you have to use XPath? Can you just use a LINQ to XML query? Personally I find those easier to understand, though some don't... Commented Mar 5, 2015 at 13:44
  • @JonSkeet That's entirely a matter of taste I'd say :-). Commented Mar 5, 2015 at 13:52
  • @MathiasMüller: I don't think it's entirely a matter of taste. For example, consider the XPath expression the OP is using - that's potentially vulnerable to an "XPath injection attack" just like including values directly in SQL. It's much harder for that to be a problem in LINQ to XML. I also suspect that anyone with general XML and LINQ experience would be able to understand the LINQ to XML equivalent - no XPath-specific knowledge required. Commented Mar 5, 2015 at 13:55
  • @JonSkeet I did not mention using XPath expressions directly vs. preventing injections, but you're right of course, that could be an issue. And yes, people with LINQ experience understand LINQ to XML without XPath-specific knowledge - and people with XPath experience understand XPath without LINQ-specific knowledge. Anyway, I didn't mean to be argumentative... Commented Mar 5, 2015 at 14:01

2 Answers 2

1

This should also work.

//ChangeSet[.//id[text()='CS-2215']]

Gets all changesets who has a child id with a specific text

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

1 Comment

Yes, but you should avoid // (descendant-or-self::) if the structure of the document is statically known. There is no reason to have brevity but possibly sacrifice efficiency.
1

You're almost there:

ReleaseNotes/ChangeSets/ChangeSet[Comments/checkintext/task/id[text()='CS-2215']]

(Note: I used an online XPath fiddle tool to find this.)

1 Comment

Sorry for being nit-picking, change id[text()='CS-2215'] to id ='CS-2215', since the string value of id is evaluated anyway. And I think the OP is looking for the ID value of that ChangeSet, so add /@Id at the end.

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.