i'm trying create , xpath expression returns me value of node found self unable element within element has different namespace root.
for example i'm trying retrieve internalsystemid following xml traditional xml won't work
xpath i'm trying use
/somemessage[1] /messagedetails[1] /payload[1] /call-name[1] /body[1] /actiontotake[1] /fields[1] /internalsystemid[1]
xml i'm have
<?xml version="1.0" encoding="utf-8"?> <somemessage> <messagedetails> <from/> <to/> <payload> <call-name xmlns:ns0="http://www.mysite.com/interface/genericnamespace" xmlns="http://www.mysite.com/interface/genericnamespace"> <header xmlns=""> <transaction> <mandatory> <transactionid>111111</transactionid> </mandatory> <system-use-only> <name>receiver</name> <someone>customer</someone> <something>out</something> </system-use-only> </transaction> </header> <body xmlns=""> <actiontotake> <transactionname>actiontotake</transactionname> <fields> <interfaceid>w00tie</interfaceid> <customersystemid>555555</customersystemid> <internalsystemid>4444444</internalsystemid> <submitteddate >2011-04-14t12:00:00-00:00</submitteddate> <eventtype>a type</eventtype> </fields> </actiontotake> </body> </call-name> </payload> </messagedetails> </somemessage>
the call-name
element in namespace uri "http://www.mysite.com/interface/genericnamespace". therefore need either
select name using namespace, or
use namespace-agnostic method access it.
the fact element in different namespace "root" (element) not directly relevant.
to #1, have declare prefix namespace in xpath execution environment; e.g. in xslt stylesheet put xmnls:mysite="http://www.mysite.com/interface/genericnamespace"
. select element using prefix , element name, e.g.
/somemessage[1]/messagedetails[1]/payload[1]/mysite:call-name[1]/body[1]/actiontotake[1]/fields[1]/internalsystemid[1]
of course use whatever prefix like.
to #2, there several options. if call-name
element has no siblings, or comes in stable order among siblings, substitute *
name. way, xpath select element child of payload[1]
, regardless of name or namespace:
/somemessage[1]/messagedetails[1]/payload[1]/*[1]/...
another option, if you're brave , confident structure of input xml, use //
skip on element:
/somemessage[1]/messagedetails[1]/payload[1]//body[1]/...
if still need test name, not namespace, can use local-name() =
:
/somemessage[1]/messagedetails[1]/payload[1]/*[local-name()='call-name']/body[1]/...
Comments
Post a Comment