Charles
Charles

Reputation: 11778

An attribute node (checked) cannot be created after the children of the containing element

The following code only works when I remove all new lines.

This does not work:

<input type="radio">
    <xsl:if test="@selected = 'true'" >
        <xsl:attribute name="checked">
            <xsl:value-of select="@selected" />
        </xsl:attribute>
    </xsl:if>
</input>

This works:

<input type="radio"><xsl:if test="@selected = 'true'" ><xsl:attribute name="checked"><xsl:value-of select="@selected" /></xsl:attribute></xsl:if></input>

This code is also working, but it's not really better:

<input type="radio"
    ><xsl:if test="@selected = 'true'" 
        ><xsl:attribute name="checked"
            ><xsl:value-of select="@selected" 
        /></xsl:attribute
    ></xsl:if
></input>

How can I make the xslt file readable and keep the code working?

Upvotes: 0

Views: 466

Answers (1)

Tim C
Tim C

Reputation: 70608

I'll expand on my comment as an answer, so it may help other people....

According the W3C specification about Whitespace stripping text nodes are stripped from the XSLT unless one of the following is true

  • The element name of the parent of the text node is in the set of whitespace-preserving element names.
  • The text node contains at least one non-whitespace character. As in XML, a whitespace character is #x20, #x9, #xD or #xA.
  • An ancestor element of the text node has an xml:space attribute with a value of preserve, and no closer ancestor element has xml:space with a value of default.

In your case, you have an xml:space="preserve" on a parent node to input. This means the whitespace nodes that you use for indenting the XSLT are actually copied to the output rather than being stripped out from the XSLT. However, if you are adding an attribute to a parent element, this must be done before any child nodes (which includes text nodes) are added to parent (See http://www.w3.org/TR/xslt#creating-attributes).

The solution is your case is to remove the xml:space, or not set it to "preserve".

Upvotes: 2

Related Questions