如何纳入我的XSLT文本输出节点XML?(how to include the node XML i

2019-06-24 10:04发布

我想一个XML文件转换成平面,管道分隔的文件用XSLT(批量装载于Postgres)。 我想在我的输出的最后一列是节点(额外的后处理和调试)的实际XML。 例如:

<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是

<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>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>    

Answer 1:

我不知道如果这是一个推荐的解决方案,但你可以尝试输出方式设置为XML,然后只需使用xsl:复制的功能。

所以,下面的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>&#xa;</xsl:text> 
  </xsl:template> 
</xsl:stylesheet>  

当应用到示例XML,会产生以下结果

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>


Answer 2:

尝试:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//Book"/>
    </xsl:template>
    <xsl:template match="Book">
        <xsl:value-of select="Title" />
        <xsl:text>|</xsl:text>
        <xsl:value-of select="Author" />
        <xsl:text>|</xsl:text>
        <xsl:apply-templates select="." mode="outputTags"/>
    </xsl:template>
    <xsl:template match="*"  mode="outputTags">
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:apply-templates select="@*"/>
        <xsl:text>></xsl:text>
        <xsl:apply-templates mode="outputTags"/>
        <xsl:text>&lt;/</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>></xsl:text>
        <xsl:if test="self::Book">
            <xsl:text>&#x0A;</xsl:text>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:text> </xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>="</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
    </xsl:template>
</xsl:stylesheet>

它产生的输入文件如下结果:

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>


文章来源: how to include the node XML in my XSLT text output?
标签: xslt