6

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>

Should generate

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>

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

2 Answers 2

9

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.

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

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>
Sign up to request clarification or add additional context in comments.

Comments

0

Try that :

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

It produces the following result from your input file :

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.