Question

This is in continuing for Need to assign attribute value in single quote xslt.

The problem: surround attribute value with single quote ' instead of double ".

Use case: Store JSON in attributes to be passed as settings for plugins etc. Eg:

<div id="plugin-instance" data-plugin-options='{"opt1":"foo", "opt2": "baz\'s bar"}'/>

G. Ken Holman answered:

Unless the vendor of your XSLT processor gives you an extension to request this, it is out of your hands. Such serialization choices are made by the processor and cannot be specified in an XSLT stylesheet using standardized mechanisms. A processor may offer this, but I haven't seen it (not that I've gone looking ... from an XML perspective, the resulting information set is unchanged regardless of the attribute delimiter chosen).

Q1: Is it possible to output single quotes with LibXslt in PHP?

Q2: If Q1 is Yes, then is it possible to output per-attribute basis? (eg: I want only some attributes to be rendered with single quotes).

Thank you for your time.

Was it helpful?

Solution

No, this isn't possible with libxslt. But libxslt will always output double quotes as &quot; inside attributes:

<div data-plugin-options="{&quot;opt1&quot;:&quot;foo&quot;, &quot;opt2&quot;: &quot;baz\'s bar&quot;}"></div>

Accessing the contents of the attribute from JavaScript should yield the correct JSON string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top