XSLT - 如何在文本输出模式下将原始 XML 转化为转换结果 [英] XSLT - how to put original XML into transformation result in text output mode

查看:22
本文介绍了XSLT - 如何在文本输出模式下将原始 XML 转化为转换结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个输出将是文本但也包括原始 xml 的转换.只是我得到了应该转换为 SQL 插入的 xml 消息,但是如果出现 SQL 错误,我也想将原始 xml 消息插入到数据库中.

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.

输入是例如:

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

转换的结果应该是:

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

问题是,如果我将 XSLT 中的输出设置为文本",则不包含 xml 标记(仅包含值).那么是否有任何混合输出模式或属性覆盖?

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?

感谢您的帮助.

推荐答案

在接近这个解决方案之前(不知道通过 XSLT 是否可以找到更好的解决方案),还要考虑在更复杂的输入和输出.

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.

即使纯粹主义者会拒绝这个答案(以及这个问题),您也可以使用xml"输出方法来做一个(非常丑陋的)诡计:

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>

输出:

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

这篇关于XSLT - 如何在文本输出模式下将原始 XML 转化为转换结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆