Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML and XSLT to write output to text file

Tags:

output

xml

xslt

I am trying to use increment in XSLT. and write the output to txt file. Here's my code: Xslt code:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="text"/>
    <xsl:result-document href="foo.txt" method=text />
    <xsl:template match="/">
    </xsl:template>

  <xsl:template match="/build/build">
    <xsl:variable name="buildNumber" select="."/>
    <xsl:element name="build">
      <xsl:value-of select="$buildNumber + 1"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="/|@*|node()">
    <xsl:value-of select="build/major"/>
    <xsl:value-of select="build/minor"/>
    <xsl:value-of select="build/build"/>
    <xsl:value-of select="build/release"/>
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>  
  </xsl:template>
</xsl:stylesheet>

XML code is:

 <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="E:\Build\genVer.xsl"?>
    <build>
        <major>1.</major><minor>0.</minor><build>0</build><release>.0</release>
    </build>

I tried using <xsl:result-document> it gives error saying <xsl:result-document> cannot be child of <xsl:stylesheet> or <xsl:template>. Thankyou

like image 451
Rizwana Khan Avatar asked Mar 29 '13 15:03

Rizwana Khan


People also ask

Which XSLT element is used to write literal text to the output?

The <xsl:text> element writes literal text to the output tree.

Can XSLT transform XML to JSON?

XSLTJSON: Transforming XML to JSON using XSLTXSLTJSON is an XSLT 2.0 stylesheet to transform arbitrary XML to JavaScript Object Notation (JSON). JSON is a lightweight data-interchange format based on a subset of the JavaScript language, and often offered as an alternative to XML in—for example—web services.

Can XSLT transform XML to CSV?

The following XSL Style Sheet (compatible with XSLT 1.0) can be used to transform the XML into CSV. It is quite generic and can easily be configured to handle different xml elements by changing the list of fields defined ar the beginning.

Can XSLT transform XML to PDF?

You can't convert directly from XML to PDF with XSLT. What you can do is convert from XML to an XSL-FO (Formatting Objects) document using XSLT. You then take the XSL-FO document and run it through a rendering engine to produce your PDF.


1 Answers

Your approach with <xsl:result-document> is perfectly sound. The only problem is that your stylesheet is XSLT 1, and for <xsl:result-document> you need XSLT 2. XSLT being a functional language has nothing to do with this problem.

If necessary, post your XSLT code that uses <xsl:result-document> and I'll fix it for you.

Edit:

Here your code with the necessary changes. Parts of your original code such as method="text" indicate you want the result to be a text file, while <xsl:element> looks like you're trying to output an XML file, so I wrote code for both. As mentioned before, <xsl:result-document> requires XSLT 2.

This creates an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml"/>

   <xsl:template match="/">
      <xsl:variable name="foldername" select="concat(/build/major, /build/minor, string(/build/build + 1), build/release)"/>
      <xsl:result-document href="{$foldername}/foo.xml" method="xml">
         <xsl:copy>
            <xsl:apply-templates/>
         </xsl:copy>
      </xsl:result-document>
   </xsl:template>

   <xsl:template match="element()">
      <xsl:copy>
         <xsl:apply-templates/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="/build/build">
      <xsl:copy>
         <xsl:variable name="buildNumber" select="."/>
         <xsl:value-of select="$buildNumber + 1"/>
      </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

And this creates a text file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:variable name="foldername" select="concat(/build/major, /build/minor, string(/build/build + 1), build/release)"/>
      <xsl:result-document href="{$foldername}/foo.txt" method="text">
         <xsl:apply-templates/>
      </xsl:result-document>
   </xsl:template>

   <xsl:template match="element()">
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="/build/build">
      <xsl:variable name="buildNumber" select="."/>
      <xsl:value-of select="$buildNumber + 1"/>
   </xsl:template>

</xsl:stylesheet>

The output file is created in a folder whose name is the version, as you wanted, but note the folder must exist beforehand.

like image 81
Dabbler Avatar answered Sep 24 '22 09:09

Dabbler