INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Need help extracting text with XQuery/XPath

Need help extracting text with XQuery/XPath

(OP)
I've got some XML like this:

CODE

<?xml version="1.0"?>
<BTARNException xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/biztalk/btarn/2004/BTARNException">
  <ExceptionDescription>
    <errorClassification>
      <GlobalMessageExceptionCode>TimeoutOnSignal</GlobalMessageExceptionCode>
    </errorClassification>
    <errorDescription>
      <FreeFormText>Public initiator terminated on time out to receive a signal for the Async action message.</FreeFormText>
    </errorDescription>
    <offendingMessageComponent>
      <GlobalMessageComponentCode>PublicInitiatorProcess</GlobalMessageComponentCode>
    </offendingMessageComponent>
  </ExceptionDescription>
  <GlobalExceptionTypeCode>Exception Message</GlobalExceptionTypeCode>
</BTARNException> 

I want the "FreeFormText" data ("Public initiator terminated ...) I've tried every XQuery/XPath statement I could think of but nothing's been working. It seems like this should work since I do something similar with a different bit of XML:

CODE

@xml.query('BTARNException/ExceptionDescription/errorDescription/FreeFormText/text()') 
The only obvious difference is that the other file doesn't include the XML version tags.

What am I missing? If it helps to know, I'm working with the XML in a SQL stored procedure.

RE: Need help extracting text with XQuery/XPath

Most likely, you need to add the namespace to each of the nodes in the XPath expression. All of the nodes carry the default namespace declare by xmlns="http://schemas.microsoft.com/biztalk/btarn/2004/BT...".

I am not an XQuery expert, so there may be a way to declare a default namespace for the XPath expression within XQuery. (There is not a way to do this in XSLT, so one must place a namespace alias on each node.)

Tom Morrison
Hill Country Software

RE: Need help extracting text with XQuery/XPath

(OP)
Thanks for the suggestion. While investigating it, I found something that will work until I can figure out a better solution or learn more about XQuery/XPath.

CODE

@xml.query('/*/*/*/*[local-name()=''FreeFormText'']/text()') 
If I understand it correctly, it's ignoring the namespace info, going down thee levels in the XML structure, searching for the specified node at the fourth level, then grabbing the text that it contains.

RE: Need help extracting text with XQuery/XPath

(OP)
Looks like XPath 2.0 allows you to get rid of the "local-name()=''" stuff by replacing it with "*:". To me, it makes the code less bloated and easier to follow.

CODE

@xml.query('/*/*/*/*:FreeFormText/text()') 
or even something like this:

CODE

@xml.query('/*:BTARNException/*:ExceptionDescription/*:errorDescription/*:FreeFormText/text()') 

RE: Need help extracting text with XQuery/XPath

The '*' means 'any child'. You could also use the '//' operator, which means 'zero or more elements'. Your XPath expression could simply be:

CODE --> XPath

//*[local-name()='FreeFormText']/text() 
This would be translated to, "Give me the text out of all nodes named 'FreeFormText' without regard to namespace."

If there is a possibility of more than one, and you want only the first (for example), you would use this:

CODE --> XPath

//*[local-name()='FreeFormText'][1]/text() 
This would be translated to, "Give me the text out of the first node (in document order) named 'FreeFormText' without regard to namespace." 'Stacking' XPath predicates in this manner can be really convenient and can be far more complex than this.

Note also that predicates can apply to any node in the expression, not just the leaf node or the penultimate node.

CODE --> XPath

/*/*[1]//*[local-name()='FreeFormText'][1]/text() 
This would be translated to, "From the first child of the document root element, give me the text out of the first node (in document order) named 'FreeFormText' without regard to namespace." 'Stacking' XPath predicates in this manner can be really convenient and can be far more complex than this.

Not on point for this thread, but to complete the story a bit, one can also combine XPath predicates with the use of XPath axes to help identify nodes that have specific ancestor nodes, child nodes, or sibling nodes.

Tom Morrison
Hill Country Software

RE: Need help extracting text with XQuery/XPath

Oops, sorry for failing to trim the copy-and-paste in the previous post. blush

Tom Morrison
Hill Country Software

RE: Need help extracting text with XQuery/XPath

(OP)
Thanks for the additional information. I can see it being useful in the near future.

RE: Need help extracting text with XQuery/XPath

Quote (PPettit)

Looks like XPath 2.0 allows you ...

XPath 2 is not as well supported, so check on your specific platform. Unfortunate, but true.

Tom Morrison
Hill Country Software

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close