1

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

1

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

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thank you @kjhughes, I think this clarifies why I'm being unable to add that line-break. And since I've tried everything in the links, I guess it is time for me to look for a change of design. – Emmanuel Morales Dec 18 '20 at 16:31
0

It is not clear how you use XSLT with .NET but if you put a character reference in an attribute value then it should be serialized as such for instance at https://xsltfiddle.liberty-development.net/nb9PtDz the XML

<root>
    <item info1="Line 1." info2="Line 2."/>
</root>

is transformed by

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
      <item infos="{@info1}&#10;{@info2}"/>
  </xsl:template>

</xsl:stylesheet>

using .NET's XslCompiledTransform into the result

<root>
    <item infos="Line 1.&#xA;Line 2." />
</root>

so the LF is there, it has to be escaped as a character reference as otherwise, any XML parser parsing the result would turn a normal LF into a space on attribute normalization.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you Martin Honnen. Seems like I couldn't adapt this solution to my code, which includes attributes and I think is the main issue, as pointed by @kjhughes. I'll update you if I manage to make it work. – Emmanuel Morales Dec 18 '20 at 16:35