Search code examples
c++xmlxpathmsxml

XPATH backwards compatibility with XSLPattern


It is my understanding that all XSLPattern queries will work identically as XPath queries, but not necessarily the other way around. Can anyone confirm this, or does anyone have a link to some Microsoft documentation that confirms this?

I have changed the selection language of MSXML documents to XPath in various parts of some code that I manage, and I just want to make sure that i'm not going to break the queries that were originally written as XSLPattern queries.


Solution

  • In my understanding there are expressions that are valid as XSLPattern and are executable with selectNodes/selectSingleNode but that throw an error when being executed as an XPath 1.0 expression. For instance the example

    var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
    doc.loadXML('<root><item>1</item><item>2</item></root>');
    var item = doc.selectSingleNode('root/item[end()]');
    WScript.Echo(item.xml);
    
    doc.setProperty('SelectionLanguage', 'XPath');
    item = doc.selectSingleNode('root/item[end()]');
    

    makes use of the end() function defined in the XSLPattern language and as such works fine as long as the selection language is XSLPattern but then in the last line throws an exception as there is no such function in XPath 1.0, there you would write it as root/item[position() = last()].

    So there are XSLPattern expressions that don't work as XPath 1.0 expressions. There are also expressions that lead different result, for instance

    var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
    doc.loadXML('<root><item>1</item><item>2</item><item>12</item></root>');
    var expression = 'root/item[. > "10"]';
    var item = doc.selectSingleNode(expression);
    WScript.Echo(item.xml);
    
    doc.setProperty('SelectionLanguage', 'XPath');
    item = doc.selectSingleNode(expression);
    WScript.echo(item.xml);
    

    which outputs

    <item>2</item>
    <item>12</item>
    

    as it seems that XSLPattern make a string based > greater than comparison while XPath 1.0 only supports number comparison and automatically converts arguments to that type.