Quantcast

Generate a literal newline in an attribute value

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Generate a literal newline in an attribute value

Hans Hübner
Hi,

for readability reasons, I would like to generate some XML using Saxon 9 which has literal newlines in the value of an attribute.  Is that even possible?  Neither

<xsl:attribute name="x"><xsl:value-of disable-output-escaping="yes" select="''&#xa;'"/></xsl:attribute>

nor

<xsl:attribute name="x"><xsl:text>
</xsl:text></xsl:attribute>

work.  Both render the newline as &#xA; in the output XML.

Any hint would be appreciated.

Thanks,
Hans

-- 
LambdaWerk GmbH
Oranienburger Straße 87/89
10178 Berlin
Phone: +49 30 555 7335 0
Fax: +49 30 555 7335 99

HRB 169991 B Amtsgericht Charlottenburg
USt-ID: DE301399951
Geschäftsführer:  Hans Hübner




------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Eduard Drenth

Perhaps this helps:

<xsl:attribute name="x"><xsl:text disable-output-escaping="yes">
</xsl:text></xsl:attribute>

and maybe this: http://stackoverflow.com/questions/1761062/why-does-this-xslt-code-add-a-new-line

Eduard Drenth, Software Architekt


[hidden email]


Doelestrjitte 8

8911 DX  Ljouwert

+31 58 213 14 14

chat: [hidden email]




From: Hans Hübner <[hidden email]>
Sent: Friday, November 25, 2016 4:20 PM
To: [hidden email]
Subject: [saxon] Generate a literal newline in an attribute value
 
Hi,

for readability reasons, I would like to generate some XML using Saxon 9 which has literal newlines in the value of an attribute.  Is that even possible?  Neither

<xsl:attribute name="x"><xsl:value-of disable-output-escaping="yes" select="''&#xa;'"/></xsl:attribute>

nor

<xsl:attribute name="x"><xsl:text>
</xsl:text></xsl:attribute>

work.  Both render the newline as &#xA; in the output XML.

Any hint would be appreciated.

Thanks,
Hans

-- 
LambdaWerk GmbH
Oranienburger Straße 87/89
10178 Berlin
Phone: +49 30 555 7335 0
Fax: +49 30 555 7335 99

HRB 169991 B Amtsgericht Charlottenburg
USt-ID: DE301399951
Geschäftsführer:  Hans Hübner




------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Michael Kay
In reply to this post by Hans Hübner
Firstly, disable-output-escaping is defined to have no effect on attribute nodes. It only works for text nodes.

Secondly, the fact that NL is serialized as &#xa; is a consequence of the "round-tripping rule" in the serialization spec (section 5 in the 3.1 version):

A consequence of this rule is that certain characters MUST be output as character references, to ensure that they survive the round trip through serialization and parsing. Specifically, CR, NEL and LINE SEPARATOR characters in text nodes MUST be output respectively as "&#xD;", "&#x85;", and "&#x2028;", or their equivalents; while CR, NL, TAB, NEL and LINE SEPARATOR characters in attribute nodes MUST be output respectively as "&#xD;", "&#xA;", "&#x9;", "&#x85;", and "&#x2028;", or their equivalents.

(If it weren't for this rule, the result of serialization followed by parsing would be an attribute value without the newline character, because the XML parser does attribute value normalization).

I think this leaves you with a use case for character maps. I haven't tested it, but I think

<xsl:character-map name="NL">
  <xsl:output-character char="§" string="&#xa;"/>
</xsl:character-map>

<e att="--§--"/>

should give you a serialized attribute value containing a literal newline character.

Michael Kay
Saxonica


> On 25 Nov 2016, at 15:20, Hans Hübner <[hidden email]> wrote:
>
> Hi,
>
> for readability reasons, I would like to generate some XML using Saxon 9 which has literal newlines in the value of an attribute.  Is that even possible?  Neither
>
> <xsl:attribute name="x"><xsl:value-of disable-output-escaping="yes" select="''&#xa;'"/></xsl:attribute>
>
> nor
>
> <xsl:attribute name="x"><xsl:text>
> </xsl:text></xsl:attribute>
>
> work.  Both render the newline as &#xA; in the output XML.
>
> Any hint would be appreciated.
>
> Thanks,
> Hans
>
> --
> LambdaWerk GmbH
> Oranienburger Straße 87/89
> 10178 Berlin
> Phone: +49 30 555 7335 0
> Fax: +49 30 555 7335 99
>
> HRB 169991 B Amtsgericht Charlottenburg
> USt-ID: DE301399951
> Geschäftsführer:  Hans Hübner
>
> http://lambdawerk.com/
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Hans Hübner
Thanks a lot, Michael.  A character map works perfectly for my purposes.

-Hans

------------------------------------------------------------------------------

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Dimitre Novatchev
In reply to this post by Michael Kay
> I think this leaves you with a use case for character maps. I haven't tested it, but I think

Doesn't this contradict the XML specification -- Attribute Value
Normalization (section 3.3.3), at:

http://www.w3.org/TR/2008/REC-xml-20081126/#AVNormalize

See the table at the end of the section.


In case an XSLT processor output, with output method XML, doesn't
comply with the  Attribute Value Normalization rule, it doesn't keep
its promise to output a well-formed XML document / fragment -- or does
it?


Cheers,
Dimitre

On Fri, Nov 25, 2016 at 8:43 AM, Michael Kay <[hidden email]> wrote:

> Firstly, disable-output-escaping is defined to have no effect on attribute nodes. It only works for text nodes.
>
> Secondly, the fact that NL is serialized as &#xa; is a consequence of the "round-tripping rule" in the serialization spec (section 5 in the 3.1 version):
>
> A consequence of this rule is that certain characters MUST be output as character references, to ensure that they survive the round trip through serialization and parsing. Specifically, CR, NEL and LINE SEPARATOR characters in text nodes MUST be output respectively as "&#xD;", "&#x85;", and "&#x2028;", or their equivalents; while CR, NL, TAB, NEL and LINE SEPARATOR characters in attribute nodes MUST be output respectively as "&#xD;", "&#xA;", "&#x9;", "&#x85;", and "&#x2028;", or their equivalents.
>
> (If it weren't for this rule, the result of serialization followed by parsing would be an attribute value without the newline character, because the XML parser does attribute value normalization).
>
> I think this leaves you with a use case for character maps. I haven't tested it, but I think
>
> <xsl:character-map name="NL">
>   <xsl:output-character char="§" string="&#xa;"/>
> </xsl:character-map>
>
> <e att="--§--"/>
>
> should give you a serialized attribute value containing a literal newline character.
>
> Michael Kay
> Saxonica
>
>
>> On 25 Nov 2016, at 15:20, Hans Hübner <[hidden email]> wrote:
>>
>> Hi,
>>
>> for readability reasons, I would like to generate some XML using Saxon 9 which has literal newlines in the value of an attribute.  Is that even possible?  Neither
>>
>> <xsl:attribute name="x"><xsl:value-of disable-output-escaping="yes" select="''&#xa;'"/></xsl:attribute>
>>
>> nor
>>
>> <xsl:attribute name="x"><xsl:text>
>> </xsl:text></xsl:attribute>
>>
>> work.  Both render the newline as &#xA; in the output XML.
>>
>> Any hint would be appreciated.
>>
>> Thanks,
>> Hans
>>
>> --
>> LambdaWerk GmbH
>> Oranienburger Straße 87/89
>> 10178 Berlin
>> Phone: +49 30 555 7335 0
>> Fax: +49 30 555 7335 99
>>
>> HRB 169991 B Amtsgericht Charlottenburg
>> USt-ID: DE301399951
>> Geschäftsführer:  Hans Hübner
>>
>> http://lambdawerk.com/
>>
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Michael Kay
>
> In case an XSLT processor output, with output method XML, doesn't
> comply with the  Attribute Value Normalization rule, it doesn't keep
> its promise to output a well-formed XML document / fragment -- or does
> it?
>


Character maps quite explicitly allow you to generate output that isn't well-formed XML. See the note in the XSLT spec:

When character maps are used, there is no guarantee that the serialized output will be well-formed XML (or HTML). Furthermore, the fact that the result tree was validated against a schema gives no guarantee that the serialized output will still be valid against the same schema.

And there's also an example showing how to use character maps to produce JSP pages with <%....%> markup that is definitely not well-formed XML. (PHP would be a better example nowadays, but it's not technically different).

Michael Kay
Saxonica
------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Dimitre Novatchev
> Character maps quite explicitly allow you to generate output that isn't well-formed XML. See the note in the XSLT spec:
>
> When character maps are used, there is no guarantee that the serialized output will be well-formed XML (or HTML). Furthermore, the fact that the result tree was validated against a schema gives no guarantee that the serialized output will still be valid against the same schema.
>
> And there's also an example showing how to use character maps to produce JSP pages with <%....%> markup that is definitely not well-formed XML. (PHP would be a better example nowadays, but it's not technically different).


Shouldn't then the transformation require output method "text", and
fail if the output method is "xml"?


Cheers,
Dimitre

On Fri, Nov 25, 2016 at 10:33 AM, Michael Kay <[hidden email]> wrote:

>>
>> In case an XSLT processor output, with output method XML, doesn't
>> comply with the  Attribute Value Normalization rule, it doesn't keep
>> its promise to output a well-formed XML document / fragment -- or does
>> it?
>>
>
>
> Character maps quite explicitly allow you to generate output that isn't well-formed XML. See the note in the XSLT spec:
>
> When character maps are used, there is no guarantee that the serialized output will be well-formed XML (or HTML). Furthermore, the fact that the result tree was validated against a schema gives no guarantee that the serialized output will still be valid against the same schema.
>
> And there's also an example showing how to use character maps to produce JSP pages with <%....%> markup that is definitely not well-formed XML. (PHP would be a better example nowadays, but it's not technically different).
>
> Michael Kay
> Saxonica
> ------------------------------------------------------------------------------
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help



--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Generate a literal newline in an attribute value

Michael Kay

> On 25 Nov 2016, at 18:44, Dimitre Novatchev <[hidden email]> wrote:
>
>> Character maps quite explicitly allow you to generate output that isn't well-formed XML. See the note in the XSLT spec:
>>
>> When character maps are used, there is no guarantee that the serialized output will be well-formed XML (or HTML). Furthermore, the fact that the result tree was validated against a schema gives no guarantee that the serialized output will still be valid against the same schema.
>>
>> And there's also an example showing how to use character maps to produce JSP pages with <%....%> markup that is definitely not well-formed XML. (PHP would be a better example nowadays, but it's not technically different).
>
>
> Shouldn't then the transformation require output method "text", and
> fail if the output method is "xml"?
>

No, that's not the use case.

The use cases for character maps are to produce "customized" XML or HTML, which may involve

(a) achieving effects that aren't covered by standard serialization mechanisms (e.g. putting CDATA or comment tags around elements or generating the new <!DOCTYPE HTML> header in HTML5)

(b) generating output in formats that resemble XML or HTML but add their own conventions, e.g. templating languages like PHP.

So you can combine the ability to generate XML or HTML markup with the ability to add your own tweaks, like <%...%> delimiters.

(Though I don't want to sound as if I think character maps are the answer to everything. They're slightly less dreadful than disable-output-escaping, though still pretty kludgey. But they can often get you out of a hole.)

Michael Kay
Saxonica
------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Loading...