2012-05-14 128 views
5

我想將XML文件轉換爲一個帶XSLT的管道分隔文件(用於批量加載到Postgres中)。我希望輸出中的最後一列是節點的實際XML(用於其他後處理和調試)。例如:如何在我的XSLT文本輸出中包含節點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>  

回答

0

試一下:

<?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> 
9

我不知道如果這是一個推薦的解決方案,但你可以嘗試輸出方式設置爲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> 
相關問題