开发者

Detect if an XML file contains a specific namespace

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 transfor开发者_运维百科m 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?


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">), 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">

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" />


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"
           )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜