Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT - how to put original XML into transformation result in text output mode

Tags:

tsql

xslt

i am trying to create a transformation which output will be text but including original xml as well. Simply i got the xml message that should be transformed to SQL insert but in case of an SQL error i want to insert the original xml message to database as well.

The input is e.g.:

<message><tag name="foo">dummy</tag></message>

The result of the transformation should be then:

INSERT INTO table (column) VALUES ('dummy')
IF @@error <> 0
BEGIN
   INSERT INTO errMsgLog (message) VALUES ('<message><tag name="foo">dummy</tag></message>')
END

The problem is if i set the output in XSLT to 'text' there are no xml tags included (just the values). So is there any mixed output mode or attribute override?

Thanks for any help.

like image 552
Petr Filas Avatar asked Sep 01 '11 12:09

Petr Filas


2 Answers

Before approaching this solution (don't know if through XSLT you can find some better solution), also consider the problems you will encounter with much more complex input and output.

Even if purists will reject this answer (and the question also), you can use "xml" output method to do a (very ugly) trickery:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>    
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:text disable-output-escaping="yes">INSERT INTO table (column) VALUES ('dummy')
IF @@error &lt;> 0
BEGIN
     INSERT INTO errMsgLog (message) VALUES ('</xsl:text>
        <xsl:copy-of select="."/><xsl:text>')
END</xsl:text>
    </xsl:template>

</xsl:stylesheet>

outputs:

INSERT INTO table (column) VALUES ('dummy')
IF @@error <> 0
BEGIN
     INSERT INTO errMsgLog (message) VALUES ('<message><tag name="foo">dummy</tag></message>')
END
like image 117
Emiliano Poggi Avatar answered Oct 16 '22 06:10

Emiliano Poggi


Some processors (e.g. Saxon) have an extension function serialize() which allows you to convert an XML node into a serialised XML representation, which the function returns as a string. You could call this and then output it in your text result. If your processor doesn't have such an extension function, then it might not be difficult to write one.

like image 1
Michael Kay Avatar answered Oct 16 '22 08:10

Michael Kay