XML and XSLT to write output to a text file

I am trying to use increment in XSLT. and write the output to a txt file. Here is 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:

 <?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 to use <xsl:result-document>, it gives an error saying that <xsl:result-document>it cannot be a child of <xsl:stylesheet>or <xsl:template>. Thankyou

+5
source share
2 answers

Your approach <xsl:result-document>sounds great. The only problem is that your stylesheet is XSLT 1, but <xsl:result-document>you need XSLT 2. XSLT, which is a functional language, has nothing to do with this problem.

Post your XSLT code that uses <xsl:result-document>, if necessary , and I will fix it for you.

Edit:

. , method="text", , , <xsl:element> , XML , . , <xsl:result-document> XSLT 2.

XML:

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

:

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

, , , , .

+7

XSLT 2.0 , :

#!/usr/bin/zsh

. =(xsltproc =(<<'XSLT_'
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>
   <xsl:strip-space elements="*"/>

<xsl:template match="/SummitFTMonitor/messages">
  <xsl:variable name="key" select="Key/text()"/>

<![CDATA[cat <<'IJEBVNKCEUJGHKLO' > ]]><xsl:value-of select="$key"/>_response.xml
<xsl:value-of select="Response"/>
IJEBVNKCEUJGHKLO

</xsl:template>
</xsl:stylesheet>
XSLT_) $*)

, zsh script XSLT ,

cat <<'XXXXX' > file_name
the value of nodes, etc ....
XXXXX

zsh, .

0

All Articles