Search

I have this XSLT:

<a data-foo='{"bar": "baz"}'/>

I'm trying to obtain this HTML:

<a data-foo='{"bar": "baz"}'/>

It seems the result from Symphony always outputs double quotes for attribute value:

<a data-foo="{"bar": "baz"}"/>

Is there a way to output single quotes instead of the forced double quotes?

If not, how are you passing JSON objects from XSLT inside data- attributes to be used in JS?

Thanks.

This question on stackoverflow offers some information. Perhaps someone who understands the answer can enlighten here as well.

Are single quotes for attributes valid HTML?

Perhaps @nils-werner has an idea?

My guess is that the XSLT processor is set up to always use double quotes for attributes which is why this is changed in your output. I don't know if the JSON specification allows it, but does switching to single quotes in your JSON work?

does switching to single quotes in your JSON work?

Nope, it doesn't:

jQuery.parseJSON("{'foor': 'bar'}")

Double quoting does work:

jQuery.parseJSON('{"foor": "bar"}')

I wonder how other are by-passing this. For the moment I have a hidden div with raw JSON-text.

My guess is that the XSLT processor is set up to always use double quotes for attributes which is why this is changed in your output.

Do you have any idea how can the XSLT processor be changed?

No – I actually don't know if you can adjust this in the PHP XML/XSLT modules.

JSON by definition can only use double quotes. I believe there's no way to achieve exactly what you want using data attributes. I think the only way to do what you want is to define the object in a script tag, or follow the selected answer on this SO question.

Ohh, well. Here's a tip from someone:

XSLT automatically escapes double quotes inside attributes. jQuery.parseJSON knows how to parse these escaped values. So this XSLT:

<a data-foo="{&#34;bar&#34;: &#34;baz&#34;}"/>

or this similar version:

<a>
    <xsl:attribute name="data-foo">{"bar": "baz"}</xsl:attribute>
</a>

will actually output this HTML:

<a data-foo="{&quot;bar&quot;: &quot;baz&quot;}"></a>

And this javascript:

jQuery.parseJSON( jQuery('[data-foo]').attr('data-foo') )

will return the actual JSON.

Ypiiiiiiiiii!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details