Solved

Run an XPath against an XSLT document

Posted on 2006-10-25
4
388 Views
Last Modified: 2013-11-23
Why is xpath assert broken when I use it on XSLT stylesheets? I have an XSLT stylesheet that I'm unit testing. My stylesheet generates XSLT stylesheets. I am trying to perform xpath asserts on the resulting generated stylesheets and it looks like XMLUnit is of no help to me. I use a valid Xpath that should result in a single hit and I get zero hits with the XMLUnit Xpath assert. So then I changed to use my own implementation of an XpathAssert. In my implementation I delegate to the JDK5 XPath API to check if an xpath exists. That throws errors. Can somebody show me how to execute this XPath, "/xsl:stylesheet/xsl:template[@name = 'foo']/xsl:call-template[@name = 'foo']" against the following XSLT?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="foo">
<xsl:call-template name="foo"/>
</xsl:template>
</xsl:stylesheet>

I realize this is a particularly involved and unique question but I can't find an answer. I've also cross posted on the yahoo Junit group where I've included more detail about my actual code. I forgot that it takes a moment to moderate so now I'm posting here. Any insight is highly appreciated.
0
Comment
Question by:DizzyDiz
4 Comments
 
LVL 5

Expert Comment

by:kannan_ekanath
ID: 17819115
I am not sure if you can evaluate the complete XPath with attribute filters. I did use XPathAPI a while ago, so you mite find that there might be updates on the code. Here is a sample code,

String xslt = //your xslt string ..../stream
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setValidating(false);
Document d = dfactory.newDocumentBuilder().parse(input);
..you got the org.w3c.dom.Document now...

XPathAPI.selectSingleNode(d, "/stylesheet/template/call-template")

The above will give you the node from there you can say node.getAttribute() and do an assert.

1) The last i used this API it does not support namespaces ("stylesheet" and not "xsl:stylesheet") and also i am not sure if it does any attribute matching... (you can try though)

2) The above is using org.apache.xpath.XPathAPI however i suggest you to try Dom4J also once. I think xpath matching is slightly better there
0
 
LVL 5

Author Comment

by:DizzyDiz
ID: 17819720
Thanks Kannan,

I found a workaround to my problem thanks to a third party API and it's sole author. (Juxy/Pavel Sher) There's two issues with my example I believe. The first issue involves allowing the XPath engine to select based on Namespaces. The second issue (which may have led to my errors, though I'm not sure) is setting the DOM to be namespace aware. That second issues was pointed out to me by Pavel. I also had another issue in code that I did not post involving multiple prefixes pointing to the same namespace. It turns out that that's not a particularly good idea when working with XPath. I fixed that issue and used my work around and now all is well. I actually believe that setting the Document to be namespace aware was causing the majority of my problems. I don't have time to double back and test for this. However if anyone else wishes to comment I'd appreciate the additional insight.

Cliff
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18023939
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 43
hibernate insert example 13 34
null output 3 35
by zero exception 10 52
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question