XPath error with namespaces

Oxygen general issues.
clintbergman
Posts: 1
Joined: Tue Mar 02, 2010 8:12 pm

XPath error with namespaces

Post by clintbergman »

Given the following XML that validates as well formed:

Code: Select all

<?xml version="1.0" encoding="utf-16"?>
<GPO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.microsoft.com/GroupPolicy/Settings">
<Computer>
<VersionDirectory>35</VersionDirectory>
<VersionSysvol>35</VersionSysvol>
<Enabled>true</Enabled>
<ExtensionData>
<Extension xmlns:q2="http://www.microsoft.com/GroupPolicy/Settings/SoftwareInstallation" xsi:type="q2:SoftwareInstallationSettings">
<q2:MsiApplication>
</q2:MsiApplication>
</Extension>
</ExtensionData>
</Computer>
</GPO>
Using 'Xpath 2.0' in Oxygen 11.1 I get the following results:
Query: /GPO/Computer/ExtensionData/Extension/*
Result: /GPO[1]/Computer[1]/ExtensionData[1]/Extension[1]/q2:MsiApplication[1]

Query: /GPO/Computer/ExtensionData/Extension/namespace::*[name() = 'q2']
Result: q2 - http://www.microsoft.com/GroupPolicy/Se ... stallation

Query: /GPO/Computer/ExtensionData/Extension/q2:MsiApplication
Error: XPath failed ... Prefix q2 has not been declared

The error doesn't seem right to me, is this a bug, or am I doing something wrong?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XPath error with namespaces

Post by adrian »

Hi,

You have to specify the namespace, not the prefix. For example:

Code: Select all

/GPO/Computer/ExtensionData/Extension/*:MsiApplication[namespace-uri()='http://www.microsoft.com/GroupPolicy/Settings/SoftwareInstallation']
or

Code: Select all

/GPO/Computer/ExtensionData/Extension/*[namespace-uri()='http://www.microsoft.com/GroupPolicy/Settings/SoftwareInstallation' and local-name()='MsiApplication']
If you activate XPath update on caret move(to the right of the XPath combo) and move the caret to q2:MsiApplication you will see in the XPath combo the second variant I've shown you above.

For XPath 2.0 you can also declare default prefix-namespace mappings in Options -> Preferences -> XML -> XSLT-FO-XQuery -> XPath.

So if you declare that q2 is mapped to http://www.microsoft.com/GroupPolicy/Se ... stallation then you can use your original query:

Code: Select all

/GPO/Computer/ExtensionData/Extension/q2:MsiApplication
After declaring the prefix-namespace mapping the 'XPath update on caret move' will also change its XPath to the same original query when the caret is in q2:MsiApplication.

Let me know if you have further questions.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply