Project

Profile

Help

Bug in Saxon Basic?

Added by Anonymous over 16 years ago

Legacy ID: #4750908 Legacy Poster: juangamnik (juangamnik)

I've tried to create new elements with namespaces with xsl:element this way: <xsl:element name="prefix:localName"> <xsl:namespace name="prefix" select="'http://www.example.com'&quot;/> </xsl:element> Saxon returns with error XDT830: Undeclared namespace 'prefix', though the xsl:namepspace instruction creates an namespace node and adds it to the element. This created node is the same as an element would have that has been created as a literal result element like this (IIRC the XPath Datamodel): <prefix:localName xmlns:prefix="http://www.example.com&quot;/> A namespace node has to be present for the element itself hasn't it? Because of that, SAX starts prefix mappings before contenthandler.startElement() for example. The following workaround works but seems to be redundant for me: <xsl:element name="prefix:localName" namespace="http://www.example.com"> <xsl:namespace name="prefix" select="'http://www.example.com'&quot;/> </xsl:element> wfg,


Replies (8)

Please register to reply

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4750941 Legacy Poster: Michael Kay (mhkay)

The spec might not be what you expect, but that doesn't make it a bug... The rules are that when you specify <xsl:element name="prefix:localName"> without a namespace attribute, then the prefix must be declared statically in the stylesheet. It's not enough for it to be declared dynamically in the result tree. You don't need xsl:namespace here. Just do <xsl:element name="prefix:localName" namespace="http://www.example.com&quot;/> and the namespace node will be created for you automatically. Or if you prefer, you can also do: <xsl:element name="prefix:localName" xmlns:prefix="http://www.example.com&quot;/> which is less code if you are creating lots of elements in the same namespace, as the xmlns:prefix declaration only needs to appear once.

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4750969 Legacy Poster: juangamnik (juangamnik)

I'll try your first hint omitting the <xsl:namespace>-element. The second one will not work for me, I think, because I generate the lexical QName of <xsl:element> in a attribute value template, so I can't add the prefix statically in a namespace declaration. I would like to have a dynamic namespace declaration, that is copied for the child node as with a static ns-decl. This is because I would like to do something like this: <xsl:element name="{concat(concat($prefix, ':'), $name_1)}" /> <xsl:dynamic-namespace name="{$prefix}" select="'http://www.example.com'&quot;/> <xsl:element name="{concat(concat($prefix, ':'), $name_2)}> <!-- ... --> </xsl:element> </xsl:element> Now I have to store my so far namespaces and add the respective to every element I create like this (it is an excerpt of a script I use to transform an xml serialization of a graph representing an xml document semantically): <!-- Handle element nodes. --> <xsl:template match="ElementNode"> <xsl:param name="namespaces"/> <!-- Add new namespaces. --> <xsl:variable name="newNamespaces"> <xsl:sequence select="$namespaces"/> <xsl:apply-templates select="tt:selectAxis(., 'namespace')" /> </xsl:variable> <xsl:variable name="prefix" select="tt:buildPrefix(.)" /> <xsl:element name="{tt:buildQName(.)}" namespace="{$newNamespaces/namespace[@prefix eq $prefix][1]/@uri}"> <!-- Process attributes --> <xsl:apply-templates select="tt:selectAxis(., 'attribute')"> <xsl:with-param name="namespaces" select="$newNamespaces"/> </xsl:apply-templates> <!-- Process childs --> <xsl:apply-templates select="tt:selectAxis(., 'child')" > <xsl:with-param name="namespaces" select="$newNamespaces"/> </xsl:apply-templates> </xsl:element> </xsl:template> Or do you know a better way? wfg, Johannes Neubauer

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4750974 Legacy Poster: juangamnik (juangamnik)

Of course the namespace should be dynamic in the first example, too: <xsl:element name="{concat(concat($prefix, ':'), $name_1)}" /> <xsl:dynamic-namespace name="{$prefix}" select="$uri"/> <xsl:element name="{concat(concat($prefix, ':'), $name_2)}> <!-- ... --> </xsl:element> </xsl:element>

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4750996 Legacy Poster: Michael Kay (mhkay)

It seems to me that you just need <xsl:element name="{$prefix}:{$name_1}" namespace="{$uri}"/> This is fully dynamic: if the namespace attribute of xsl:element is present, then the prefix in the name attribute doesn't need to be statically declared. Really, though, the best place for XSLT coding advice is not on the Saxon forum (that's for product-specific issues). I'd recommend the xsl-list at mulberrytech.com Michael Kay

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4751018 Legacy Poster: juangamnik (juangamnik)

I've tried to omit the <xsl:namespace>-element and it worked as you said. Unfortunately I really don't understand the meaning of the specification in this point. In your book (XSLT 2.0 3rd Edition) you said the <xsl:namespace>-element can be used for creating namespace nodes, which enforce the serializer to produce the necessary namespace declarations. In the example there was a attribute with a namespace (prefix) that was not declared and with a <xsl:namespace>-element the problem was solved (page 348). I don't understand why the serializer does not produce a declaration in my example and produces an error instead. Could you explain me the difference or point at my error in reasoning? Thank you very much! XSLT rockz. At another point I think, I have found an error in your book (XSLT 2.0 3rd Edition) on page 420: You write, that a xsl:sequence has an optional parameter "select", which is mandatory in the spec and if the "select parameter is omitted, the sequence constructor is evaluated, which is not permitted. I use XSLT not for converting data to html in a very straightforward manner, but for complex computational tasks and Your book is a great help for me. Thumbs up. wfg, Johannes Neubauer.

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4751037 Legacy Poster: juangamnik (juangamnik)

Hi, Ok, I got a little bit off topic, but I was very lucky about your fast answer... I tried to subscribe to the mulberry list and I reveived a confirmation e-mail. I answered in the requested way, but nothing happened (tried it twice and have no emailed to the admin). Your instruction does not solve my problem, that I have to inherit my prefix to uri mappings, which I wouldn't have to, declaring a namespace at a ancestor statically. Of course your code is nicer to read (because of my two concats, didn't know that I can use attribute value templates this way) but it's semantically the same as my code. wfg, Johannes Neubauer

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4751073 Legacy Poster: Michael Kay (mhkay)

There is no way in XSLT to say "create an element and put it in the same namespace as its parent in the result tree". That's because creating the element and attaching it to its parent are conceptually two separate operations, and the name of the element must be determined at the time the element is created, this can't be deferred until it is added to the content of a parent element (which in general might never happen). It might have been possible to define xsl:element so that the prefix can be resolved by reference to the namespace nodes present in the content sequence, but I think it would probably have added a lot of complexity without adding much functionality. But that's academic; the spec wasn't defined that way so that's that. The error in your example is explained on page 263 of the book (top of the page): If there is no namespace attribute (for xsl:element) and the supplied QName includes a prefix, the prefix must be in scope at this point in the stylesheet. Concerning xsl:sequence, the book was written before the XSLT 2.0 spec was finalized, and this is one of the changes that happened after it went to press. Fortunately, there were not very many of these. There's be a 4th edition soon to fix these. Regards Michael Kay

RE: Bug in Saxon Basic? - Added by Anonymous over 16 years ago

Legacy ID: #4751098 Legacy Poster: juangamnik (juangamnik)

Hi, Thanks a lot for the enlightenment. I have looked at the problem from the point of view of the XPath data model, not of the XSLT processing model. A namespace node created by a declaration cannot be differentiated from a node created by a namespace declaration. But the latter are copied to its children and the others not. You're right it is that way defined in the spec and it's not a bug in Saxon. wfg, Johannes Neubauer.

    (1-8/8)

    Please register to reply