变量从一个 xsl 文件到另一个文件不可用 [英] Variable not available from one xsl file to other

查看:66
本文介绍了变量从一个 xsl 文件到另一个文件不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Perl 执行 XSLT 1.0 转换.我正在使用此处列出的解决方案作为我的脚本.

I am trying to perform XSLT 1.0 transformation using Perl. I am using the solution listed here as my script.

我的输入 xml 文件看起来有点像这样.

My input xml file looks somewhat like this.

<?xml version="1.0" encoding="UTF-8" ?>
    <bsg>
          <msg>
               <entry key="testingString">This is a test.</entry>
               <!-- <trs> This is me, trs!!! </trs> -->
            </msg>
    </bsg>

trs 属性是可选的,这意味着它并不总是出现在所有其他 entry 节点中.条目可以有可选属性 tslate,它可以是 truefalse,即输入 xml 文件可以有类似

trs attribute is optional meaning its not always present in all other entry nodes. Entry can have optional property tslate which can be true or false i.e input xml file can have something like

<entry key="Doctor" tslate="false">Physician.</entry>

对于 Perl 脚本,我提供了 xslt 文件,该文件导入了另外两个 xslt 文件(比如文件 A 和文件 B),其中定义了变量和模板.

To the Perl script, I supply the xslt file which imports two other xslt files (say File A and File B) which has variables and templates defined.

文件A从输入的xml文件中读取一个节点并执行以下操作

File A reads a node from input xml file as and performs following operation

<xsl:param name="isOTHERFILEavailable"/>    
  <xsl:variable name="tvariables">
    <xsl:choose>
      <xsl:when test="$isOTHERFILEavailable = 'true'">
        <xsl:copy-of select="document($OTHERTHE_File)/bsg/msg"/>
      </xsl:when>
    </xsl:choose>
  </xsl:variable>

isOTHERFILEavailable 是从 Perl 文件传递​​的字符串参数,用于验证 OTHERFILE 是否存在.

isOTHERFILEavailable is a string parameter passed from Perl file that validates whether OTHERFILE exists or not.

然后访问此 tvariables 以在文件 B 中执行一些其他操作,如下所示

This tvariables is then accessed to perform some other operation in File B as below

<xsl:template match="msg">
<xsl:variable name="trsExists">
            <xsl:for-each select="entry">
                <xsl:variable name="current_key" select="@key"/>
                <xsl:variable name="trsMatch" select="$tvariables/msg/entry[@key = $current_key]"/>
                <xsl:choose>
                    <xsl:when test="$trsMatch and normalize-space($trsMatch/text) = normalize-space(.) and (not(@tslate) or @tslate = true())">
                        <xsl:copy>
                            <xsl:copy-of select="$trsMatch/@key|$trsMatch/tvariables/text()|@context"/>                     
                        </xsl:copy>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>        
</xsl:variable>
</xsl:template>

我的问题是,当我到达这条线时,

My problem is, when I reach this line,

<xsl:variable name="trsMatch" select="$tvariables/msg/entry[@key = $current_key]"/>

Perl 错误如下

无效类型运行时错误:文件 B.xslt 行 # 元素变量无法计算变量 'tvariables' 的表达式

我是否遗漏了一些明显的东西?

Am I missing something obvious?

PS => 使用 XSLT 2.0(Saxon) 从 Java 运行相同的转换时,一切都运行良好,即使 tvariables 未定义,即 isOTHERFILEavailable 是 <代码>假.

P.S => Everything runs fine when run the same transform from Java using XSLT 2.0(Saxon) and even when tvariables is not defined, that is when isOTHERFILEavailable is false.

推荐答案

$tvariable 是一个 xml 片段.

$tvariable is a xml fragment.

在我看来,Perl 很可能使用 XSL 1.0 处理器.在这种情况下,您的问题是 $tvariable 需要先从片段转换为节点集,然后才能使用它.XSL 2 像您期望的那样处理片段,这就是代码在 Saxon 中运行的原因.幸运的是,大多数 XSL 1.0 处理器(prehap all?)都提供对具有扩展功能的片段的支持.您需要查看文档以了解详细信息.你的 select 语句会变成这样:

It seems likely to me that Perl is using a XSL 1.0 Processor. In which case your problem is that $tvariable will need to be converted froma fragment into a node-set before you can use it. XSL 2, handles fragments just as you would expect which is why the code runs in Saxon. Fortunaltely most XSL 1.0 Processors (prehap all?) provide support for fragments with an extension function. You'll need to check your documentation for the details. Your select statement will become something like:

 select="node-set($tvariables)/msg/entry[@key = $current_key]"

查看 http://www.xml.com/pub/a/2003/07/16/nodeset.html 有关节点集功能的更多信息.

Check out http://www.xml.com/pub/a/2003/07/16/nodeset.html for more info about node-set functions.

这篇关于变量从一个 xsl 文件到另一个文件不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆