How to access XML *attributes* using XPath

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
Folks,
Let's say I have a XML fragment like this:

<Requester>
<ApplicationID application="OMS_SERVICE" bo="Client" />

How can I access the value of the "application" attribute in TakeCommand??

From what I can see, the XMLXPATH function in TCMD 9 (and from the looks of it - in TCMD 10, too) can only read and write element values - but not attributes.

Really?!?!? This seems like a severe limitation, really - XML documents typically are mixed collections of elements and attributes.

Or am I missing something here??

Cheers! Marc
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#2
mscheuner wrote:


> Let's say I have a XML fragment like this:
>
> <Requester>
> <ApplicationID application="*OMS_SERVICE*" bo="*Client*" />
>
> How can I access the value of the "application" attribute in TakeCommand??
You cannot; Take Command does not currently support retrieving XML
attributes.


>>From what I can see, the XMLXPATH function in TCMD 9 (and from the looks of it - in TCMD 10, too) can only read and write element values - but not attributes.
>
> Really?!?!? This seems like a severe limitation, really - XML documents typically are mixed collections of elements and attributes.
>
> Or am I missing something here??
I'll add it to the suggestion list for v11, but so far you're the only
user who's asked for it.

Rex Conn
JP Software
 
May 29, 2008
517
3
Groton, CT
#3
Folks,
Let's say I have a XML fragment like this:

<requester>
<applicationid application="<b>OMS_SERVICE</b>" bo="<b>Client</b>">

How can I access the value of the "application" attribute in TakeCommand??

From what I can see, the XMLXPATH function in TCMD 9 (and from the looks of it - in TCMD 10, too) can only read and write element values - but not attributes.

Really?!?!? This seems like a severe limitation, really - XML documents typically are mixed collections of elements and attributes.

Or am I missing something here??

Cheers! Marc

Here's a clue:

File X.XML :

<foo>
<bar>
<baz whatzit='frob'>
</baz>
</bar>
</foo>


> echo answer=%@xmlxpath[".\x.xml",foo/bar/baz/@whatzit]
answer=frob


You can access an attribute with the same syntax an item by prefixing the attribute name with an '@'.

Rex has disclaimed support for this, but I've been using it consistently without a problem.


</applicationid></requester>
 
#4
Here's a clue:
> echo answer=%@xmlxpath[".\x.xml",foo/bar/baz/@whatzit]
answer=frob
You can access an attribute with the same syntax an item by prefixing the attribute name with an '@'.
Ah, yes - look at that - that's the standard XPath syntax, and I had tried it before (but that was still in TCMD v8 and it hadn't worked back then).

But now in TCMD v9, this works like a charm - thanks!

Rex: no need to consider for v11 - it's already in there :-)

Thanks guys
Marc