Project

Profile

Help

Unexpected failure of xsl:attribute

Added by Anonymous almost 18 years ago

Legacy ID: #4083182 Legacy Poster: Joseph Thomas-Kerr (jak09)

Hi, Can someone please explain to me what on earth is going on in the following? I am receiving the error "An attribute node (optional) cannot be created after the children of the containing element" when trying to process the following stylesheet with saxon 8.8 or saxon 6.5.5. It occurs both when debugging the stylesheet in oxygen 8, and when running it from the commandline with saxon 8.8. Uncommenting the empty template for @* fixes the problem and my stylesheet works fine. I can pause it in oxygen and look at the value of $eltList and it is as expected. What is going on here? Joe. test stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:math="http://exslt.org/math" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0"> <xsl:template match="xsd:schema/xsd:element[1]/xsd:complexType/xsd:sequence"> <xsl:variable name="eltList"> <xsl:apply-templates mode="eltList"/> </xsl:variable> <ala> <xsl:apply-templates mode="actions" select="$eltList"/> </ala> </xsl:template> <xsl:template match="xsd:element" mode="eltList"> <read> <xsl:apply-templates select="@" mode="eltList"/> </read> </xsl:template> <xsl:template match="@minOccurs" mode="eltList"> <xsl:attribute name="optional">bar</xsl:attribute> </xsl:template> <!--<xsl:template mode="eltList" match="@"/>--> </xsl:stylesheet> test input: <xsd:schema targetNamespace="a" xmlns="a" xmlns:test="a" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="root"> <xsd:complexType> <xsd:sequence> <xsd:element name="optionalElement" type="test:b1" minOccurs="0"/> <xsd:element name="multipleElement" type="test:b2" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>


Replies (1)

RE: Unexpected failure of xsl:attribute - Added by Anonymous almost 18 years ago

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

The default template for attributes is in effect <xsl:template match="@"> <xsl:value-of select="."/> </xsl:template> This creates a text node (which isn't very useful or intuitive). This means that with your template commented out, you are doing: <read> <xsl:apply-templates select="@"/> </read> <xsl:template match="@*"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="@minOccurs"> <xsl:attribute name="optional">bar</xsl:attribute> </xsl:template> So you are processing all the attributes of the current element, in an unpredictable order, and for some of them you create text nodes, while for another you create an attribute node. This makes it highly likely that the attribute node will be preceded by a text node, which accounts for the error you are seeing. Michael Kay

    (1-1/1)

    Please register to reply