XML and XSLT to write output to text file

39,672

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.

Share:
39,672
Rizwana Khan
Author by

Rizwana Khan

Updated on July 09, 2022

Comments

  • Rizwana Khan
    Rizwana Khan almost 2 years

    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