Meph
Meph

Reputation: 280

writing XML on a single line with text and tag

I have one XML file, which a XSLT transforms into a HTML file. I would like to write my XML in a javascript variable, on a single line, with all its tag and text inside the HTML file.

For example, I would like to transform this:

<root>
   <groupe>
      <link>
        ...
      </link>
   </groupe> 
</root>

into:

<script>
   var xml = '<root><groupe><link>...</link></groupe></root>';
</script>

I know that <xsl:copy-of ... /> keeps text and tag, but I can't get rid of linebreaks and whitespace. I have seen the normalize-space option but

<xsl:template match="@* | node()" >
   <xsl:copy-of select="normalize-space(.)" />
</xsl:template>

prints my output XML without tags.

Upvotes: 0

Views: 7066

Answers (1)

Jirka Š.
Jirka Š.

Reputation: 3428

I think you could use something like

...
<script>
    var xml = '<xsl:copy-of select="." />'
</script> 
...

in your template.

What you need is to declare attribute indent="no" in xsl:output tag, e.g.

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

And probably

<xsl:strip-space elements="*"/>

could be also beneficial.

Upvotes: 3

Related Questions