2012-05-14 31 views
5

Tôi đang cố gắng chuyển đổi tệp XML thành tệp phẳng, được phân tách bằng đường ống với XSLT (để tải hàng loạt vào Postgres). Tôi muốn cột cuối cùng trong đầu ra của tôi là XML thực tế của nút (để xử lý sau và gỡ lỗi thêm). Ví dụ:cách bao gồm XML nút trong đầu ra văn bản XSLT của tôi?

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

nên tạo

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 hiện tại của tôi là

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

Trả lời

0

Hãy thử rằng:

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

Nó tạo ra các kết quả sau từ tập tin đầu vào của bạn:

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

Tôi không chắc chắn nếu điều này là một giải pháp được khuyến khích, nhưng bạn có thể thử thiết lập các phương pháp sản lượng để xml, và sau đó chỉ cần sử dụng xsl: copy-of chức năng.

Vì vậy, XSLT sau

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

Khi áp dụng cho XML mẫu của bạn, tạo đầu ra sau đây

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> 
Các vấn đề liên quan