Condividi tramite


Quick XPath test

Some time ago a colleague asked for help: he needed to write a XPath query that seemed complex.

I suggested him to use VS and a simple XSLT as a quick debugging environment.

Create a new XSLT file, substitute the template code with the following content.

 <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <queryResult>
      <xsl:value-of select="put your XPath query here"/>
    </queryResult>
  </xsl:template>
</xsl:stylesheet>

From the XML menu in Visual Studio select “Show XSLT Output”: Visual Studio will ask you for a sample source XLM document (“Choose input XML document”) on which the XSLT will be applied, and voilà, you have a simple XML output; in the queryResult element you’ll have the result of your XPath query.

In the end, he needed to find an element independently from its namespace, as it may change according to the schema version of the document; something like this:

 /*/*[local-name(.)='result']/*[local-name(.)='rc']

In the past you needed to write a program for this.

Comments