如何在我的 XSLT 文本输出中包含节点 XML? [英] How to include the node XML in my XSLT text output?
问题描述
我正在尝试使用 XSLT(用于批量加载到 Postgres)将 XML 文件转换为平面的、以竖线分隔的文件.我希望输出中的最后一列是节点的实际 XML(用于额外的后处理和调试).例如:
I'm trying to convert an XML file into a flat, pipe-delimited file with XSLT (for bulk-loading into Postgres). I would like the last column in my output to be the actual XML of the node (for additional post-processing and debugging). For example:
<Library>
<Book id="123">
<Title>Python Does Everythig</Title>
<Author>Smith</Author>
</Book>
<Book id="456">
<Title>Postgres is Neat</Title>
<Author>Wesson</Author>
</Book>
</Library>
应该生成
Python Does Everything|Smith|<Book id="123"><Title>Python Does Everythig</Title>Author>Smith</Author></Book>
Postgres is Neat|Wesson|<Book id="456"><Title>Postgres is Neat</Title><Author>Wesson</Author></Book>
我当前的 XSL 是
My current XSL is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="text" omit-xml-declaration="yes" indent="no" />
<xsl:template match="//Book">
<xsl:value-of select="Title" />
<xsl:text>|</xsl:text>
<xsl:value-of select="Author" />
<!-- put in the newline -->
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
推荐答案
我不确定这是否是推荐的解决方案,但您可以尝试将输出方法设置为 xml,然后仅使用 xsl:copy-of 函数.
I am not sure if this is a recommended solution, but you could try setting the output method to xml, and then just using the xsl:copy-of function.
所以,下面的 XSLT
So, the following XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
<xsl:template match="//Book">
<xsl:value-of select="Title" />
<xsl:text>|</xsl:text>
<xsl:value-of select="Author" />
<xsl:text>|</xsl:text>
<xsl:copy-of select="." />
<!-- put in the newline -->
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
应用于您的示例 XML 时,生成以下输出
When applied to your sample XML, generates the following output
Python Does Everythig|Smith|<Book id="123"><Title>Python Does Everythig</Title><Author>Smith</Author></Book>
Postgres is Neat|Wesson|<Book id="456"><Title>Postgres is Neat</Title><Author>Wesson</Author></Book>
这篇关于如何在我的 XSLT 文本输出中包含节点 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!