Search code examples
xmlxsltline-breakseol

Cannot insert line break in attribute value via XSLT


I'm new to XSLT, and I'm having a hard time inserting a line break between two sentences. I know similar questions have been asked in the forum but none of the solutions is working for me, I'm sorry if I'm breaking any community rule.

So basically I have something like this:

<xsl:attribute name="messageText">
<xsl:value-of select="eligInformation/@messageText02"/>
<br />
<xsl:value-of select="eligInformation/@messageText03"/>
</xsl:attribute>    

But the <br /> or any of its variants (<br/>, <br></br>, etc) is not working, the .NET parser doesn't seem to like it and it throws the following error: "An item of type 'Element' cannot be constructed within a node of type 'Attribute'".

I've also tried with all sort of alternatives such as:

<xsl:text>&#xa;</xsl:text>
<xsl:text>&#10;</xsl:text>
<xsl:text>

</xsl:text>

etc., but in those cases, even though I don't get any errors, both messages simply appear in the same row.


Solution

  • xsl:attribute is used to set an attribute value.

    <br/> is an HTML element, and elements are not allowed in attribute values, thus the error message that you reported.

    While line break characters are permitted in XML attributes, such designs or plans are likely to run aground due to improper, inconsistent, or even proper interpretation of 3.3.3 Attribute-Value Normalization:

    3.3.3 Attribute-Value Normalization

    Before the value of an attribute is passed to the application or checked for validity, the XML processor must normalize the attribute value by applying the algorithm below, or by using some other method such that the value passed to the application is the same as that produced by the algorithm.

    1. All line breaks must have been normalized on input to #xA as described in 2.11 End-of-Line Handling, so the rest of this algorithm operates on text normalized in this way.

    2. Begin with a normalized value consisting of the empty string.

    3. For each character, entity reference, or character reference in the unnormalized attribute value, beginning with the first and continuing to the last, do the following:

      • For a character reference, append the referenced character to the normalized value.

      • For an entity reference, recursively apply step 3 of this algorithm to the replacement text of the entity.

      • For a white space character (#x20, #xD, #xA, #x9), append a space character (#x20) to the normalized value.

    For another character, append the character to the normalized value.

    Recommendation: Avoid line breaks in XML attribute values.

    See also