tags:

views:

50

answers:

3

Again a simple thing. I have a stylesheet that parses XML and XSL files. Basically, it tries to detect if the XML is a stylesheet with:

<xsl:if test="count(//xsl:template)!=0">

It does indeed detect stylesheets. It has problems with XML files, though, which generate an "Undefined namespace prefix - 'xsl'" error. (In XmlSpy. Similar errors in the project I'm working on.)

I'm doing something wrong. Any suggestions on how to improve this stylesheet?


Some additional information: This is a stylesheet that's meant to analyse other XML files, no matter what they contain. It should be able to transform itself even, and does so nicely. It has no problem transforming other (normal) stylesheets either. The problem arrives when I try to transform a regular XML file. Yet not all XML files...


As it turns out, the error is something else. The XML files that I tried to transform contain a processing instruction. This one: <?xml-stylesheet href="..\MyStylesheet.xsl" type="text/xsl"?>
The problem I have now is that when I process an XML file that contains this PI, the XSLT starts reporting the error about the undefined namespace prefix. So, how do I tell the XSLT processor to ignore this processing instruction?

+1  A: 

Double check how you have declared the xsl namespace and what namespace-prefix you have chosen.

You need to ensure that the xsl namespace prefix is defined in your stylesheet if you want to use it in your XPATH expressions. You will get that error when you try to use a namespace prefix that has not been declared.

If it isn't declared anywhere further up in the stylesheet(typically on the document element like this: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;), or if you have chosen a different namespace prefix (e.g. declared it as "xslt" like this: xmlns:xslt="http://www.w3.org/1999/XSL/Transform"), then when you attempt to reference "xsl" it won't know what you are referring to.

You can declare the xsl namespace prefix on your if statement as a quick test:

<xsl:if test="count(//xsl:template)!=0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

Any suggestions on how to improve this stylesheet?

You can simplify your test condition to select the xsl:template elements, rather than evaluating the count() of them. The results of test="//xsl:template" will evaluate as true() if something is selected, and false() if nothing is selected.

In stylesheets xsl:template are top level constructs that are children of the document element. Rather than using // to recurse through every node in the XML document tree, you can use a more efficient XPATH expression:

<xsl:if test="/*/xsl:template" />
Mads Hansen
+1  A: 

It is incorrect to assume that an XSLT code file always contains an <xsl:template> instruction.

There are examples of XSLT stylesheet modules where the file contains just an <xsl:stylesheet> instruction and one or more global-level variables. Such stylesheet module is typically imported/included in another stylesheet module using an <xsl:import> or <xsl:include> instruction.

Also, what defines XSLT is not the prefix of the elements that constitute XSLT instructions. This prefix is not mandated to be "xsl" and some programmers use different prefixes, such as "xslt" or "x".

What really defines XSLT code is the XSLT namespace.

Finally, an XSLT code file isn't guaranteed even to contain an <xsl:stylesheet> directive, because XSLT offers a synonym: <xsl:transform>.

Taking in account all these considerations, a better test would be:

/*[contains('|stylesheet|transform|', concat('|', local-name(), '|')
          and
            namespace-uri()="http://www.w3.org/1999/XSL/Transform"
           )

Because there exists the possibility of having embedded stylesheets, the test above can be slightly modified to cover these as well:

//*[contains('|stylesheet|transform|', concat('|', local-name(), '|')
          and
            namespace-uri()="http://www.w3.org/1999/XSL/Transform"
           )
Dimitre Novatchev
I know it's incorrect, but if it doesn't, there's no information that I want to display from it. With stylesheets, I only collect the @match, @mode and @name attributes to display in a report. The system is just trying to detect if the XML file is any of the known types I've defined. If not, it's not interesting. Still, very good point!
Workshop Alex
A: 

Why do you need to check anything beyond "/xsl:template|xsl:transform"? Those must be at the top level, and can only be at the top level.

Tony Nassar
I don't need to. But the XML file might or might not be an XSLT file. The project I'm working on receives XML data from a special resource and needs to detect what it is and provide information about the content. With XSLT files, I need to list the templates. With other XML files, I need to check it's format from one of possible 40 different ones and display it's content. And that's all it has to do.
Workshop Alex