2

Given an XML fragment that I want to parse with XPath I first need to extract the namespaces to add to the namespace manager. I've been trying to figure out the Regex pattern needed to extract xml attributes that define a namepspace. For example I want to get all the namespaces which I can do some more basic string manipulation on to separate out the namespace name and the url.

xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-02-12T12:41:45"

The attribute name will always begin with xmlns: and I need the regex to read to the end of the value, so include the last "

Alternatively a more generic pattern would do the job to just extract ALL attributes in the form name="value" and I can just do some string compares to see if each attribute is a namespace.

<my:StationLookupValues xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-02-12T12:41:45"><my:StationLookupValue>Hull Inspectors</my:StationLookupValue></my:StationLookupValues><my:StationLookupValues xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-02-12T12:41:45"><my:StationLookupValue>Barnsley Inspectors</my:StationLookupValue></my:StationLookupValues><my:StationValue xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-02-12T12:41:45">Hull Inspectors</my:StationValue>

I've not been able to find an example of something like this, nor work it out for myself. Any assistance on this would be very much appriciated.

[EDIT] I understand that XML parsers should be used and this is what I am going to do. But all I have is an XML fragment to pass so I must first build a namespace manager and in order to do that I need to extract the namespaces used.

2

4 Answers 4

4

Try this pattern: 'xmlns:(.*?)=(".*?")'

It means

  • the literal string xmlns:
  • shortest string up to an =
  • a quote, followed by shortest string up to the following quote

The parenthesis means the first group contains the namespace name, the second group is the value. Adjust according to whether you want it all in one, and whether you want or don't want the quotes in the group.

As Tomalak pointed out in his answer, this is fraught with peril. It could potentially match patterns that are parts of comments or embedded in strings as data, etc. This is why regular expressions aren't good for parsing xml data -- since you aren't actually parsing, you're just looking for patterns without regard to context).

2

Be aware that such a things are possible:

<elem>
  <x:elem xmlns:x="http://some/namespace" />
  <x:elem xmlns:x="http://some/other/namespace" />
  <elem xmlns="http://some/third/namespace" />
  <elem>
     XML Namespaces look like xmlns:foo="http://some/foo/namespace"!
  </elem>
  <!-- remember to put xmlns:x="http://some/namespace" back in! -->
  <elem />
</elem>

Just extracting namespaces and prefixes with a regex will get it wrong at some point.

1
  • +1 for good thinking. I didn't think of the different ways of expressing these. Although the namespaces themselfs are dynamic fortunatly the way that InfoPath builds the xml isn't so I'm safe looking for this specific way. Feb 19, 2010 at 14:26
1

I think that processing XML that contains namespaces without knowing what those namespaces are is a sign that someone, somewhere, is doing something wrong.

I'm trying to figure out how, if you don't know what namespace you're looking for, you could get any benefit out of creating a namespace manager. The weirdest requirements often actually turn out to be requirements, so I dunno, but it really seems to me like there's something else going on here.

4
  • Unfortunately, since the OP apparently doesn't understand XML Namespaces, he also doesn't understand why it's so unusual for the namespace to be unknown. Feb 25, 2010 at 4:01
  • Hm, now that I've read his second sentence more closely, I think I see what you mean. I don't like assuming that anyone asking a question about XML namespaces doesn't understand them, but I can't really think of a time when that hasn't proven to be true, and a quick perusal of SO questions isn't finding any counterexamples. Feb 25, 2010 at 4:44
  • Your correct that I don't fully understand XML namespaces. I'm asking this question specifically because I'm trying to get values out of the XML produced when an infopath form is saved. The namespace is dynamic. Each form template creates it's own namespace based on the form name and date of creation. My code doesn't know which form the xml comes from so I have to pull out the namespace somehow. Feb 27, 2010 at 0:22
  • But if you don't know the namespace, how do you know that the data is what you're looking for? If you get a document that has data in the foo namespace, data in the bar namespace, and data in the baz namespace, which namespace should you be looking in? How do you know? Feb 28, 2010 at 10:24
1

The regex mentioned by Bryan Oakley will work (with the caveats he mentions).

Others who have railed against the idea of not knowing the namespaces involved in an XML document to parse are forgetting about the XSD specification for wildcards (see section 3.10 of the XML Schema Part 1 specification).

You may be in a scenario, like I currently am, where you only have a base XSD defined but which intentionally has defined <any namespace="##other" .../> elements to allow for arbitrary XML extensions from other namespaces. In this scenario, you'll have to use XPaths to parse any XML from other namespaces which are making use of the XSD wildcard elements. For my parser, I need to first figure out what namespaces are being used. Then, based on that, grab the appropriate pre-defined XPaths for those namespaces before I can parse the document.

Using XSD wildcards are nice when you just want a base structure but also want the flexibility that allows others to append their own information independent of one another so you don't have to constantly revise XSDs for any random request from another group and risk breaking those currently using the schema.

I haven't settled on a final solution for this myself. But leaning towards using regex to grab XML prefixes (which will likely have false positives) and then validate those matches against the JAXP org.w3c.dom.Document.lookupNamespaceURI(String prefix) to remove the false positives.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.