Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include the node XML in my XSLT text output?

Tags:

xslt

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>    
like image 844
Chris Curvey Avatar asked May 14 '12 13:05

Chris Curvey


People also ask

What does node () do in XSLT?

XSLT current() Function Usually the current node and the context node are the same. However, there is one difference. Look at the following XPath expression: "catalog/cd". This expression selects the <catalog> child nodes of the current node, and then it selects the <cd> child nodes of the <catalog> nodes.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

How do you include in XSLT?

Remarks. An XSLT file can include another XSLT file using the <xsl:include> element. The href attribute value is a URI reference identifying the file to be included. The relative URI is resolved with relation to the base URI of the <xsl:include> element.


1 Answers

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>
like image 120
Tim C Avatar answered Oct 10 '22 10:10

Tim C