XML 和 XSLT 将输出写入文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15706498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:57:21  来源:igfitidea点击:

XML and XSLT to write output to text file

xmlxsltoutput

提问by Rizwana Khan

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

我尝试使用<xsl:result-document>它会出错,说<xsl:result-document>不能是<xsl:stylesheet>or 的孩子<xsl:template>。谢谢

回答by Dabbler

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.

你的方法<xsl:result-document>很完美。唯一的问题是您的样式表是 XSLT 1,而<xsl:result-document>您需要 XSLT 2。XSLT 作为一种函数式语言与这个问题无关。

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

如有必要,请发布您使用的 XSLT 代码,<xsl:result-document>我会为您修复它。

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"表明您希望结果是一个文本文件,而<xsl:element>看起来您正在尝试输出一个 XML 文件,因此我为两者都编写了代码。如前所述,<xsl:result-document>需要 XSLT 2。

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.

输出文件创建在名称为版本的文件夹中,如您所愿,但请注意该文件夹必须事先存在。

回答by Alex J.

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 获取,从而创建文件。