187
Views
3
Comments
Solved
xml namespaces & prefixes causing problems selecting nodes
Question

Hello,

In my app I am querying a service, which sends an xml response, and I am trying to parse it using the xml extension provided by OutSystems. I have run into a couple difficulties related to namespaces in the xml:

1. The beginning of the xml is shown below. I use XmlDocument_Load_v2() to load my xml into a DOM, and that completes without error. However, I then use XmlDocument_SelectNodes() and specify the xpath "feed/entry", but it returns an XmlNodeList with a length of 0. I have found that if I delete <xmlns="http://www.w3.org/2005/Atom"> from my xml before loading it, the list has 2 nodes, which is correct, and I can further query those nodes as expected. My first question is: Is there a way to load the xml without first modifying the namespace declaration?

2. When I try to specify an xpath to an element that contains the prefix <d:> or <m:>, the element is undefined, and then I can't query that object. For example, once I have selected the first entry element in the xpath feed/entry, I would like to specify an xpath similar to the one I specify below, and then get the value from the FieldOne element using XmlElement_GetInnerText(). Could someone help me figure out how to get the text contained by the FieldOne element?

entry/link[@title='RoutedContainer']/m:inline/entry/content/m:properties/d:FieldOne


Thank you for any help you can offer. By the way, I have been looking through the forums, and I found a similar question here. The only answer was to use the XML Records forge component, but I would prefer to not use that component because it requires putting the output into a structure that has the same hierarchy as the data you are trying to extract, but I only need a couple pieces of information from each feed/entry element, and I would like to return the data in a flat structure.

I am also attaching a text file that contains a full xml response.

SampleXML.txt
UserImage.jpg
Ryan Rigby
Solution

I ultimately figured out how to solve my problem of not being able to create an xpath that references elements containing a prefix (such as m: or d: in my example above) that correspond to a namespace. When you reference one of these elements, instead of creating an xpath like this: 

entry/link/m:inline 

the xpath needs to be structured like this:

entry/link/*[name()='m:inline']

Hope this helps someone else.

2018-10-29 08-31-03
João Marques
 
MVP

Hi Ryan,


Perhaps, you want to consider using Regex? After all you have a text and a pattern.

This component allows you to test and make sure you have the right syntax as well as to see how to call it from OutSystems both server and client side.


I was able to extract what you want by using this pattern (?<=d:FieldOne (.*)>).*(?=</d:FieldOne>) but you can tune more to your use case.


Hope it helps.


Cheers,

João

UserImage.jpg
Ryan Rigby
Solution

I ultimately figured out how to solve my problem of not being able to create an xpath that references elements containing a prefix (such as m: or d: in my example above) that correspond to a namespace. When you reference one of these elements, instead of creating an xpath like this: 

entry/link/m:inline 

the xpath needs to be structured like this:

entry/link/*[name()='m:inline']

Hope this helps someone else.

UserImage.jpg
SM Wang

Have you solved the first one yet? How was it resolved?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.