[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Friday challenge: XSLT thats creates XPaths for meani

Subject: Friday challenge: XSLT thats creates XPaths for meaningfully equivalent comparisons of XML files
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 13 Apr 2007 11:48:26 +0100
 Friday challenge: XSLT thats creates XPaths for  meani
This is a slight variation of the "generate xpaths to all elements"
problem.  Here the goals is to create a set of XPaths from a given XML
document, such that when applied to an XML document if all XPaths
return true, the two XML documents are meaningfully equivalent.

Why?  Well before setting about upgrading existing transforms to 2.0,
or say performance tuning, its useful to have tests in place to ensure
the output after the modifications is still the same as before.   One
simple way to do this is use a tool like CheckXML with a set of
generated XPaths.

So for this input:

<root>
	<foo fooatt="att">foo</foo>
	<bar>bar</bar>
	<bar>baz</bar>
</root>

And this transform:

<xsl:template match="/">
	<checkXML>
		<xml src="{document-uri(/)}">
			<xsl:for-each select="//*">
				<xsl:variable name="path" select="concat('/', string-join(for $x
in ancestor-or-self::*
				return concat($x/local-name(), '[',
count(.|$x/preceding-sibling::*[name() = current()/name()]), ']'),
'/'))" as="xs:string"/>
				<xsl:for-each select="text()[normalize-space(.) != '']">
					<check><xsl:value-of select="concat($path, '/text[', position(),
'] = ''', .,'''')"/></check>
				</xsl:for-each>
				<xsl:for-each select="@*">
					<check><xsl:value-of select="concat($path, '/@', name(), ' = ''',
., '''')"/></check>
				</xsl:for-each>
			</xsl:for-each>
		</xml>
	</checkXML>
</xsl:template>

The result is:

<checkXML>
  <xml src="file:/C:/test.xml">
     <check>/root[1]/foo[1]/text[1] = 'foo'</check>
     <check>/root[1]/foo[1]/@fooatt = 'att'</check>
     <check>/root[1]/bar[1]/text[1] = 'bar'</check>
     <check>/root[1]/bar[2]/text[1] = 'baz'</check>
  </xml>
</checkXML>

The above transform is quick attempt to demonstrate the problem, I'm
sure it can be improved on.

Also, if there are any thoughts about this approach it would be good
to hear them.

cheers
andrew

ps. CheckXML is a work in progress, contact me if you'd like to be involved.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.