Adrian Herscu schrieb am 01.04.2009 um 17:41:11 (+0300):
> Here it is:
> 
> 1) the test xsl
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform";

Nice test case! I'd nonetheless get in the habit of prefixing XSLT with
xsl, just because everybody does so, and not toeing the party line
is likely to cause confusion.

>   <template match="wsdl:definitions">
>     <gen:configuration>
>       <!-- BUG: couldn't make it work here -->
>       <copy-of select="xalan:checkEnvironment()" />

Works for me.

>     </gen:configuration>
>     <gen:test func="sayHello">
>       <value-of select="myext:sayHello()" />

Works.

>     </gen:test>
>     <gen:test func="lookupNamespaceUri">

Now what's wrong with <xsl:value-of select="namespace-uri()"/> ?
No need to call an extension function here.

To get the URIs for all the namespace nodes in scope, try:

    <xsl:for-each select="namespace::node()">
      <NS><xsl:value-of select="."/></NS>
    </xsl:for-each>

>       <!-- PROBLEM: works for 'wsdl' prefix,
>            for 'tns' and everything else,
>            e.g. 'soap', 'xsd', etc., returns null
>            -->
>       <value-of select="myext:lookupNamespaceUri(current(), 'tns')" />

Note you don't need current() here, although there is no harm in doing
so. The current() function is used to get the context node *outside* an
XPath expression from inside that expression when moving away from the
context node, which you're not doing. So you can just use ".", the dot,
which is the context node (or context item in XSLT 2.0).

>     public static String lookupNamespaceUri(final Node currentNode,
>             final String prefix) {
>         final String namespaceUri;
> 
>         namespaceUri =
>             currentNode.getOwnerDocument().getDocumentElement()
>                 .lookupNamespaceURI(prefix);
> 
>         return namespaceUri;
>     }

I don't know why this doesn't return the expected result. A call to
getNodeName() works just fine.

But then, maybe the expectation is wrong. An XSLT 1.0 processor isn't
obliged to respect your choice of prefixes for namespaces, so I think we
can't be sure the namespace has the same prefix inside the clockwork as
outside. This is a question for the experts.

You'd have to get hold of org.w3c.dom.NameList in your extension method,
which is an iterator over all tuples of namespace URI and prefix. But
according to the Javadoc, this isn't in use, so I'm not sure you can
access it.

So I'd just use the XSLT method, which is more convenient anyway.

Michael Ludwig

Reply via email to