I am trying to use increment in XSLT. and write the output to txt file. Here's my code: Xslt code:
我试图在XSLT中使用增量。并将输出写入txt文件。这是我的代码:Xslt代码:
<?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代码是:
<?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
我尝试使用
2 个解决方案
#1
7
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.
这里你的代码有必要的变化。原始代码的一部分(例如method =“text”)表示您希望结果是文本文件,而
This creates an XML file:
这会创建一个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>
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.
输出文件在名称为版本的文件夹中创建,如您所愿,但请注意该文件夹必须预先存在。
#2
0
In my environment XSLT 2.0 is not supported, so I came up with the following workaround:
在我的环境中,不支持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_) $*)
Basically, in this zsh script XSLT is used to produce statements like
基本上,在这个zsh脚本中,XSLT用于生成类似的语句
cat <<'XXXXX' > file_name
the value of nodes, etc ....
XXXXX
These statements are then sourced by zsh, resulting in files being created.
然后这些语句由zsh提供,从而导致创建文件。
#1
7
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.
这里你的代码有必要的变化。原始代码的一部分(例如method =“text”)表示您希望结果是文本文件,而
This creates an XML file:
这会创建一个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>
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.
输出文件在名称为版本的文件夹中创建,如您所愿,但请注意该文件夹必须预先存在。
#2
0
In my environment XSLT 2.0 is not supported, so I came up with the following workaround:
在我的环境中,不支持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_) $*)
Basically, in this zsh script XSLT is used to produce statements like
基本上,在这个zsh脚本中,XSLT用于生成类似的语句
cat <<'XXXXX' > file_name
the value of nodes, etc ....
XXXXX
These statements are then sourced by zsh, resulting in files being created.
然后这些语句由zsh提供,从而导致创建文件。