Author |
Message
|
jcraw62 |
Posted: Fri Mar 17, 2006 12:11 pm Post subject: Use of evaluateXPath() with namespaces |
|
|
Newbie
Joined: 17 Mar 2006 Posts: 6
|
The following code works with the message below, i.e 2 chapters are returned:
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("/pub:document/chapter");
MbElement chapter = (MbElement)chapters.get(0);
<pub:document xmlns:pub="http://broker.www.njsp.org/services/brokerPubSub/"><chapter title="Introduction"> Some text.<para><sent>sentence1</sent></para><para><sent>sentence2</sent></para> </chapter> <chapter title="Message Flows"/> </pub:document>
However, with the following message change zero chapters are returned:
<pub:document xmlns="http://broker.www.njsp.org/services/junk" xmlns:literary="http://broker.www.njsp.org/services/literary/" xmlns:pub="http://broker.www.njsp.org/services/brokerPubSub/"><chapter title="Introduction"> Some text.<literary:para><sent>sentence1</sent></literary:para><literary:para><sent>sentence2</sent></literary:para> </chapter> <chapter title="Message Flows"/> </pub:document>
I don’t understand why ?
Does anyone have good examples of using the evaluateXPath() with / without MbXPath objects ?
Many thanks. |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Mar 17, 2006 3:40 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
This is probably really a Broker question, and not a Java question.
You should take your input XML and use the Trace node to write it out to a file.
Then you should examine the output of the trace node to understand how WMB has parsed the XML and what elements it has created with what names.
I bet you will find that the namespace has been applied to things you didn't expect. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
PeterPotkay |
Posted: Sat Mar 18, 2006 8:20 am Post subject: |
|
|
 Poobah
Joined: 15 May 2001 Posts: 7722
|
Moved to correct forum. _________________ Peter Potkay
Keep Calm and MQ On |
|
Back to top |
|
 |
dirac |
Posted: Mon Mar 20, 2006 5:19 am Post subject: |
|
|
Novice
Joined: 31 Mar 2005 Posts: 23
|
I was feeling exceptionally helpful this morning so I ran the second example through a trace node.
This gives:
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:document = (
(0x07000012):xmlns= 'http://broker.www.njsp.org/services/junk'
(0x07000012)xmlns:literary= 'http://broker.www.njsp.org/services/literary/'
(0x07000012)xmlns:pub= 'http://broker.www.njsp.org/services/brokerPubSub/'
(0x01000000)http://broker.www.njsp.org/services/junk:chapter = (
(0x03000000):title = 'Introduction'
(0x01000000)http://broker.www.njsp.org/services/literary/:para = (
(0x01000000)http://broker.www.njsp.org/services/junk:sent = (
(0x02000000): = 'sentence1'))
(0x01000000)http://broker.www.njsp.org/services/literary/:para = (
(0x01000000)http://broker.www.njsp.org/services/junk:sent = (
(0x02000000): = 'sentence2')))
(0x01000000)http://broker.www.njsp.org/services/junk:chapter = (
(0x03000000):title = 'Message Flows'))
So the chapter elements are associated with the default namespace instead of xmlns:pub - perhaps not what you were expecting? |
|
Back to top |
|
 |
jcraw62 |
Posted: Mon Mar 20, 2006 9:04 am Post subject: Problem revisted |
|
|
Newbie
Joined: 17 Mar 2006 Posts: 6
|
Thanks for all the input. I added Trace nodes to my flow as suggested.
Not sure why but it seems like the 1st example from Friday's posting now produces no "chapters". This is consistent with the results from the testing I did today (see below).
Note all I'm trying to do in these tests is get the "document" node. I've also run the tests below with a different default namespace e.g. xmlns="http://broker.www.njsp.org/services/brokerPubSub/" and get the same results.
Help ! Please ! Thanks !
Additional tests
*************
Using this message MQInput node parses as follows:
<pub:document xmlns="http://broker.www.njsp.org/services/brokerPubSub/" xmlns:pub="http://broker.www.njsp.org/services/brokerPubSub/"><chapter title="Introduction"> Some text.<para><sent>sentence1</sent></para><para><sent>sentence2</sent></para> </chapter> <chapter title="Message Flows"/> </pub:document>
Trace Node output
(0x01000010):XMLNS = (
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:document = (
(0x07000012):xmlns = 'http://broker.www.njsp.org/services/brokerPubSub/'
(0x07000012)xmlns:pub = 'http://broker.www.njsp.org/services/brokerPubSub/'
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:chapter = (
(0x03000000):title = 'Introduction'
(0x02000000): = ' Some text.'
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:para = (
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:sent = (
(0x02000000): = 'sentence1'
)
)
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:para = (
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:sent = (
(0x02000000): = 'sentence2'
)
)
(0x02000000): = ' '
)
(0x02000000): = ' '
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub/:chapter = (
(0x03000000):title = 'Message Flows'
)
(0x02000000): = ' '
)
)
None of the following produce correct results:
1. Produces MbRuntimeJavaComputeNode exception on evaluateXPath()
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("/http://broker.www.njsp.org/services/brokerPubSub/:document");
2. Returns zero documents
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("/pub:document");
3. Returns zero documents
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("/document");
4. Returns zero documents
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("document");
5. Returns zero documents
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("pub:document");
6. Produces MbRuntimeJavaComputeNode exception on evaluateXPath()
MbMessage msg = assembly.getMessage();
List chapters= (List)msg.evaluateXPath("http://broker.www.njsp.org/services/brokerPubSub/:document");
Please advise how to refer to these elements since using either none of the above methods works.
Thanks. |
|
Back to top |
|
 |
jefflowrey |
Posted: Mon Mar 20, 2006 9:17 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
I'm not quite set up yet to test this myself, so forgive me a bit if this doesn't work.
You're probably now running into difficulties with the /'s in the namespace.
Try either putting the namespace in single quotes
Code: |
List chapters= (List)msg.evaluateXPath("'/http://broker.www.njsp.org/services/brokerPubSub/':document"); |
or escaping the /'s
Code: |
List chapters= (List)msg.evaluateXPath("/http:\/\/broker.www.njsp.org\/services\/brokerPubSub\/:document"); |
_________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
jcraw62 |
Posted: Mon Mar 20, 2006 9:41 am Post subject: Results posted |
|
|
Newbie
Joined: 17 Mar 2006 Posts: 6
|
Jeff,
thanks but no success.
The 1st produces zero chapters. The 2nd,3rd,4th producre MB exceptions.
List chapters1= (List)msg.evaluateXPath("'/http://broker.www.njsp.org/services/brokerPubSub/':document");
List chapters2= (List)msg.evaluateXPath("/http://broker.www.njsp.org/services/brokerPubSub/:document");
List chapters3= (List)msg.evaluateXPath("//http:///broker.www.njsp.org//services//brokerPubSub//:document");
List chapters4= (List)msg.evaluateXPath("//http:///broker.www.njsp.org//services//brokerPubSub//:document");
BTW One of the examples you provided (below) contains invalid ecape characters...
List chaptersz= (List)msg.evaluateXPath("/http:\//broker.www.njsp.org\/services\/brokerPubSub\/:document");
The following variations also are invalid...
List chaptersz= (List)msg.evaluateXPath("\/http:\//broker.www.njsp.org\/services\/brokerPubSub\/:document");
List chaptersz= (List)msg.evaluateXPath("\/http:\/\/broker.www.njsp.org\/services\/brokerPubSub\/:document"); |
|
Back to top |
|
 |
jefflowrey |
Posted: Mon Mar 20, 2006 9:54 am Post subject: Re: Results posted |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
jcraw62 wrote: |
Jeff,
thanks but no success. |
I think it's time to look at the XPath standards to see how namespaces should be handled.
jcraw62 wrote: |
The 1st produces zero chapters. The 2nd,3rd,4th producre MB exceptions.
List chapters1= (List)msg.evaluateXPath("'/http://broker.www.njsp.org/services/brokerPubSub/':document");
List chapters2= (List)msg.evaluateXPath("/http://broker.www.njsp.org/services/brokerPubSub/:document");
List chapters3= (List)msg.evaluateXPath("//http:///broker.www.njsp.org//services//brokerPubSub//:document");
List chapters4= (List)msg.evaluateXPath("//http:///broker.www.njsp.org//services//brokerPubSub//:document"); |
I would only expect the first one to be vaguely valid.
jcraw62 wrote: |
BTW One of the examples you provided (below) contains invalid ecape characters...
List chaptersz= (List)msg.evaluateXPath("/http:\//broker.www.njsp.org\/services\/brokerPubSub\/:document");
|
Which doesn't look like what I posted...
jcraw62 wrote: |
The following variations also are invalid...
List chaptersz= (List)msg.evaluateXPath("\/http:\//broker.www.njsp.org\/services\/brokerPubSub\/:document");
List chaptersz= (List)msg.evaluateXPath("\/http:\/\/broker.www.njsp.org\/services\/brokerPubSub\/:document"); |
The last one is what I meant to post (and what shows as what I posted).
Ah! http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.mft.doc/com/ibm/broker/plugin/MbXPath.html
Quote: |
MbXPath xp = new MbXPath("/aaa/other:aaa/bbb");
xp.addNamespacePrefix("other", "http://mydomain.com/namespace2");
xp.setDefaultNamespace("http://mydomain.com/namespace2");
List nodeset = (List)message.evaluateXPath(xp); |
_________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
jcraw62 |
Posted: Mon Mar 27, 2006 11:17 am Post subject: Sample from red book works (sort of) but... |
|
|
Newbie
Joined: 17 Mar 2006 Posts: 6
|
Jeff,
here is the example which after modifying it I was able to get working.
<aaa xmlns='http://mydomain.com/namespace1'
xmlns:other='http://mydomain.com/namespace2'>
<other:aaa>
<bbb/>
</other:aaa>
<aaa>
MbXPath xp = new MbXPath("/aaa/other:aaa/bbb");
xp.addNamespacePrefix("other", "http://mydomain.com/namespace2");
xp.setDefaultNamespace("http://mydomain.com/namespace2");
List nodeset = (List)message.evaluateXPath(xp);
Out of the box it doesn't work...need to change:
xp.setDefaultNamespace("http://mydomain.com/namespace2");
to...
xp.setDefaultNamespace("http://mydomain.com/namespace1");
Also...need to close <aaa> tag, i.e. </aaa>
************
However, tried applying the same technique to the following message...
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p464="http://broker.www.njsp.org/services/brokerPubSub"><soapenv:Header/><soapenv:Body><p464:subscriptionRequestMessage><subscriptionRequest><subscriptionDetails><p464:subscriptionDetails><topic>IncidentReport</topic><destination>sims_IncidentReport_Queue</destination><destinationType>SOAPHTTP</destinationType></p464:subscriptionDetails></subscriptionDetails></subscriptionRequest></p464:subscriptionRequestMessage></soapenv:Body></soapenv:Envelope>
(0x01000010):XMLNS = (
(0x01000000)http://schemas.xmlsoap.org/soap/envelope/:Envelope = (
(0x07000012)xmlns:soapenv = 'http://schemas.xmlsoap.org/soap/envelope/'
(0x07000012)xmlns:soapenc = 'http://schemas.xmlsoap.org/soap/encoding/'
(0x07000012)xmlns:xsd = 'http://www.w3.org/2001/XMLSchema'
(0x07000012)xmlns:xsi = 'http://www.w3.org/2001/XMLSchema-instance'
(0x07000012)xmlns:p464 = 'http://broker.www.njsp.org/services/brokerPubSub'
(0x01000000)http://schemas.xmlsoap.org/soap/envelope/:Header =
(0x01000000)http://schemas.xmlsoap.org/soap/envelope/:Body = (
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub:subscriptionRequestMessage = (
(0x01000000):subscriptionRequest = (
(0x01000000):subscriptionDetails = (
(0x01000000)http://broker.www.njsp.org/services/brokerPubSub:subscriptionDetails = (
(0x01000000):topic = (
(0x02000000): = 'IncidentReport'
)
(0x01000000):destination = (
(0x02000000): = 'sims_IncidentReport_Queue'
)
(0x01000000):destinationType = (
(0x02000000): = 'SOAPHTTP'
)
)
)
)
)
)
)
)
on the <Body> element evaluateXPath("p464:subscriptionRequestMessage") returns a subscriptionRequestMessage element.
However, evaluateXPath("p464:subscriptionRequestMessage/subscriptionRequest") returns an empty array.
I don't understand why. Any ideas ? Thanks. |
|
Back to top |
|
 |
jefflowrey |
Posted: Mon Mar 27, 2006 11:33 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
the subscriptionRequest element is not in any namespace. So I think you need to indicate that in some way.
I would guess that this is done by using ""p464:subscriptionRequestMessage/:subscriptionRequest".
But I'm not an expert on this. You'll have to keep looking at your traces and keep mapping the namespaces to namespace prefixes and build your XPath that way. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
jcraw62 |
Posted: Mon Mar 27, 2006 2:35 pm Post subject: |
|
|
Newbie
Joined: 17 Mar 2006 Posts: 6
|
Thanks. This really needs to be better documented.
Just as ...
MbXPath xp = new MbXPath("/aaa/other:aaa/bbb");
allows me to reference elements in the default namespace, i.e. /aaa or /bbb I should be able to specify
p464:subscriptionRequestMessage/subscriptionRequest
(where subscriptionRequest is in the default namespace).
Help ! IBM ! |
|
Back to top |
|
 |
|