XML parsing and CDATA
Hi, The following is based on this question here: http://stackoverflow.com/questions/28398651/how-to-generate-markup-inside-of... What I ended up using in my XSLT stylesheet was something along these lines: <xsl:attribute name="bla"> <![CDATA[Here is <em>some</em>verbatim text with id="]]> <xsl:value-of select="@id"/> <![CDATA[" and <strong>more</strong> stuff.]]> </xsl:attribute> This worked reliably. However, this worked only after a seemingly subtle fix to the text itself! Initially I had something like id-<xsl:value-of select="@id"/> i.e. there was some text before that <xsl:value>. This worked sometimes, but not others where suddenly the CDATA following that text/element combination was swallowed away. Removing any text around the <xsl:value> seems to work in all cases though. Unfortunately, the document is too large to send and I don't really have the time to build a small test case which illustrates this problem. But it seems to me that there is a parser issue here when multiple CDATA inside of the <xsl:attribute> were combined with text and another element like <xsl:value>. There was no error message, just garbled output. Cheers, Jens -- Jens Tröger http://savage.light-speed.de/
Actually, the initial fix did not work for larger XML documents. Here is what I noticed though: intermediate CDATA are swallowed away: <xsl:attribute name="bla"> <![CDATA[Here is <em>some</em>verbatim text with id="]]> <xsl:value-of select="@id"/> <![CDATA[" and <strong>more</strong> stuff. Another id="]]> <xsl:value-of select="@id"/> <![CDATA[" And the end.]]> </xsl:attribute> What happens fairly reliably in my document is that the first and last CDATA are written out, not the middle one, yet the <xsl:value-of> tags work. What comes out as the attribute is the following (assuming that @id="some-id") bla='Here is <em>some</em>verbatim text with id="some-idsome-id" And the end.' Hope this helps. Jens On Sat, Feb 21, 2015 at 08:21:25PM +0100, Jens Tröger wrote:
Hi,
The following is based on this question here:
http://stackoverflow.com/questions/28398651/how-to-generate-markup-inside-of...
What I ended up using in my XSLT stylesheet was something along these lines:
<xsl:attribute name="bla"> <![CDATA[Here is <em>some</em>verbatim text with id="]]> <xsl:value-of select="@id"/> <![CDATA[" and <strong>more</strong> stuff.]]> </xsl:attribute>
This worked reliably. However, this worked only after a seemingly subtle fix to the text itself! Initially I had something like
id-<xsl:value-of select="@id"/>
i.e. there was some text before that <xsl:value>. This worked sometimes, but not others where suddenly the CDATA following that text/element combination was swallowed away. Removing any text around the <xsl:value> seems to work in all cases though.
Unfortunately, the document is too large to send and I don't really have the time to build a small test case which illustrates this problem. But it seems to me that there is a parser issue here when multiple CDATA inside of the <xsl:attribute> were combined with text and another element like <xsl:value>. There was no error message, just garbled output.
Cheers, Jens
-- Jens Tröger http://savage.light-speed.de/
Jens Tröger schrieb am 21.02.2015 um 20:21:
The following is based on this question here:
http://stackoverflow.com/questions/28398651/how-to-generate-markup-inside-of...
What I ended up using in my XSLT stylesheet was something along these lines:
<xsl:attribute name="bla"> <![CDATA[Here is <em>some</em>verbatim text with id="]]> <xsl:value-of select="@id"/> <![CDATA[" and <strong>more</strong> stuff.]]> </xsl:attribute>
This worked reliably. However, this worked only after a seemingly subtle fix to the text itself! Initially I had something like
id-<xsl:value-of select="@id"/>
i.e. there was some text before that <xsl:value>. This worked sometimes, but not others where suddenly the CDATA following that text/element combination was swallowed away. Removing any text around the <xsl:value> seems to work in all cases though.
I think this is better written as follows in XSLT: <tag bla="Here is <some> id={@id} ..." /> Stefan
Are you sure about the id={@id} notation? It's being written through as is, without replacing that with the actual id value. Isn't { } used for an element, not an attribute? See: http://www.keller.com/xslt/8/ Cheers, Jens On Wed, Mar 18, 2015 at 03:50:12PM +0100, Stefan Behnel wrote:
I think this is better written as follows in XSLT:
<tag bla="Here is <some> id={@id} ..." />
Stefan
-- Jens Tröger http://savage.light-speed.de/
[fixing citation order] Jens Tröger schrieb am 20.03.2015 um 22:02:
On Wed, Mar 18, 2015 at 03:50:12PM +0100, Stefan Behnel wrote:
I think this is better written as follows in XSLT:
<tag bla="Here is <some> id={@id} ..." />
Are you sure about the
id={@id}
notation? It's being written through as is, without replacing that with the actual id value. Isn't { } used for an element, not an attribute? See: http://www.keller.com/xslt/8/
See http://www.w3.org/TR/xslt#dt-attribute-value-template Stefan
On Sat, Mar 21, 2015 at 07:10:46AM +0100, Stefan Behnel wrote:
Jens Tröger schrieb am 20.03.2015 um 22:02:
On Wed, Mar 18, 2015 at 03:50:12PM +0100, Stefan Behnel wrote:
I think this is better written as follows in XSLT:
<tag bla="Here is <some> id={@id} ..." />
Are you sure about the
id={@id}
notation? It's being written through as is, without replacing that with the actual id value. Isn't { } used for an element, not an attribute? See: http://www.keller.com/xslt/8/
See
http://www.w3.org/TR/xslt#dt-attribute-value-template
Stefan
Right, that is for actual attributes. However, I'm using this from within <xsl:attribute name="data-content"> ... </xsl:attribute> and then this approach doesn't seem to work. Jens -- Jens Tröger http://savage.light-speed.de/
Jens Tröger schrieb am 25.03.2015 um 21:37:
On Sat, Mar 21, 2015 at 07:10:46AM +0100, Stefan Behnel wrote:
Jens Tröger schrieb am 20.03.2015 um 22:02:
On Wed, Mar 18, 2015 at 03:50:12PM +0100, Stefan Behnel wrote:
I think this is better written as follows in XSLT:
<tag bla="Here is <some> id={@id} ..." />
Are you sure about the
id={@id}
notation? It's being written through as is, without replacing that with the actual id value. Isn't { } used for an element, not an attribute? See: http://www.keller.com/xslt/8/
See
Right, that is for actual attributes. However, I'm using this from within
<xsl:attribute name="data-content"> ... </xsl:attribute>
and then this approach doesn't seem to work.
And I assume you really have to use <xsl:attribute> ? (your initial example wasn't clear here) In the worst case, you can write a Python function that builds the complete attribute value and call it from inside of the <xsl:attribute> section. Sure, not great, but should at least give you what you want. Stefan
participants (2)
-
Jens Tröger
-
Stefan Behnel