0

I'd like to do something similar as asked here: how to include the node XML in my XSLT text output? or, shortly explained:

  • input is XML
  • convert using XSL to a different syntax (non-XML)
  • copy an XML sub-tree verbatim to the output

Both options presented in the topic referenced above are interesting, but will not work, because my input XML doc uses (possibly multiple) namespaces and:

  • when using xsl:output=text (answer 2) the namespaces of the input XML are lost. I can use name() instead of local-name() but then still the namespace declarations are missing. Is there a nice way of collecting the referred namespace declarations and insert them?
  • if I use the xsl:output=xml way (answer 1), I get the namespaces in the generated XML properly, but then I somehow need to escape the quotes of the XML attributes - any idea how to do this?
  • Another problem with solution 1 is, if XML-special chars like & are in the input XML, then the textual output has them still as & (which is of course correct but not what I need)

Example:

<bla:Library xmlns:bla="ns1" xmlns:blub="ns2">
  <blub:Book id="123">
    <Title>Python Does Everythig &amp; more</Title>
    <Author>Smith</Author>
  </Book>
...

And, different from the original post, the result shall be (note the quoting and handling of the &). And, yes the output syntax in my case is different (not DB-related, therefore I need the quotes around the raw XML output), but to stick with the original example ...:

Python Does Everything & more|Smith|"<bla:Book xmlns:bla=\"ns1\" xmlns:blub=\"ns2\" id=\"123\"><blub:Title>Python Does Everythig &amp; more</Title>Author>Smith</Author></Book>"

Seems as if this is quite impossible but maybe somebody knows a nice trick ... :-) Regards, tge

Community
  • 1
  • 1
tge
  • 91
  • 9
  • *"Seems as if this is quite impossible."* Indeed it is. "Something that looks like XML, but with all the double quotes escaped" is not an output mode that XSLT processors implement. You can still use XSLT to create the XML bits, but you have to use a higher layer (i.e. the host language that contains your XSLT processor) to put together your custom output format. – Tomalak May 14 '14 at 14:39

1 Answers1

0

With standard XSL I could not find a way of doing this, but I used the possibility to create an extension module (in my case with libxslt - but seems to be standard XSL), which does the conversion. So, my XSL now looks like this:

<xsl:stylesheet version="1.0" ...
     xmlns:tge="http://xmlsoft.org/xslt/tgeplugin"
     extension-element-prefixes="tge">
...
<xsl:for-each select="bla:Book">
  <xsl:text> "</xsl:text><tge:tgeplugin select="."/><xsl:text>"</xsl:text>
</xsl:for-each>

and my extension plugin implements "tgeplugin" (see examples with libxslt how to do that). There, I use a few libxml2 functions to effectively produce a new XML tag "<text>" containing the actual input XML as text value (with quotes escaped).

Not standard XSL, but for my problem probably the least ugly way :-)

tge
  • 91
  • 9