-2

for some reason I'd like to output some invalid or at least only partly valid XML from an XSL transformation.

For example, I'd like to print out

<openTag attribute="yes"> <openTag2>

without any closing tags from a template.

I tried:

<xsl:template match="//MyElement">

    <xsl:text disable-output-escaping="yes">
     <xsl:value-of select=<book xmlns="<openTag attribute="yes"> <openTag2>"/>
    </xsl:text>

    <xsl:apply-templates select="node()"/>

</xsl:template>

However, this leads to an error that the select statement must not contain a "<".

It's late and maybe I don't see the forest for the trees. Can anybody give me a tipp??

Edit:

The background of the problem is that I have an XML file named source.xml. This contains the element:

<MyElement>Text</MyElement>

I'd like to do an XSL transformation to transform this into another XML file named target.xml with the content

<openTag attribute="Text"> <openTag2>

The document will then be further processed to add the closing elements. So, the fact that the elements are not closed here, does not matter. The resulting file target.xml does not have to be valid XML.

I hope the background helps a bit.

Thanks Norbert

Norbert
  • 735
  • 8
  • 19
  • 2
    This is a [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Please provide fuller background of problem, complete XML source, complete XSLT script, and complete desired result. – Parfait Mar 07 '18 at 17:34
  • I edited the question to provide some more background information. Hope this helps. I guess that the most tricky part is that the result of my XSLT transformation will not be valid XML. – Norbert Mar 07 '18 at 17:55
  • 2
    (1) You're confusing [**invalid vs not well-formed**](https://stackoverflow.com/a/25830482/290085). (2) Your plan is misguided, and your question still suffers from being an XY Problem as @Parfait pointed out. Just don't output "XML" that's not well-formed -- you'll save yourself and others a lot of trouble. – kjhughes Mar 07 '18 at 19:07

1 Answers1

1

XSLT is designed for XML-to-XML transformations. If you want to produce non-XML, use a different technology.

Actually, you can produce arbitrary text output files using the text output method. But you'll need to write the pseudo-tags by hand, for example

<xsl:text>&lt;openTag&gt;....&lt;/openTag2&gt;</xsl:text>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • 1
    IMO, a better answer to "How do I poke my eye out?" is just "Don't poke your eye out" without saying "Actually you can..." – kjhughes Mar 07 '18 at 18:01
  • There are some use cases for generating stuff in "XML-like" formats, given that people invent such formats: especially ad-hoc templating languages like the original ASP / JSP / PHP / Freemarker. Though I don't recognize any of these in this post! – Michael Kay Mar 07 '18 at 22:32
  • Ok, if I can stop cringing at OP's purpose (*the document will then be further processed to add the closing elements [sic]*) long enough to consider that some future reader may actually have a legitimate need to output pseudo-tags, then I'll grant you the point. – kjhughes Mar 07 '18 at 22:56