使用 xsl:variable 的 xsl:for-each 选择问题 [英] trouble with xsl:for-each selection using xsl:variable

查看:43
本文介绍了使用 xsl:variable 的 xsl:for-each 选择问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我无法生成按位置选择 XML 文档不同部分中的节点的 xpath 表达式.我使用 xsl:variable 创建表达式,但是当我使用 xsl:for-each 和 xsl:variable 的值作为我的选择语句时,我得到一个错误.

I am having trouble generating an xpath expression that selects a node in a different section of the XML document by position. I am using xsl:variable to create the expression, but when I use a xsl:for-each with the value of the xsl:variable as my select statement I get an error.

<xsl:variable name="input_params_query">
  <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text>
  <xsl:number value="position()" format="1" />
  <xsl:text disable-output-escaping="yes">]/parameter</xsl:text>
</xsl:variable>

<xsl:for-each select="$input_params_query">
  <input rdf:resource="#{@name}"/>
</xsl:for-each>

导致错误:

The 'select' expression does not evaluate to a node set.

当我打印出我使用的 xsl:variable 的值时,我得到:

When I print out the value of the xsl:variable I am using I get:

/example/inputs/dataset[1]/parameter

这是我尝试在 for-each 调用中选择的节点的有效且正确的 Xpath 表达式.

which is a valid and correct Xpath expression for the nodes I am trying to select in the for-each call.

我将 xsl:variable 用作 xsl:for-each 选择属性是否不正确?

Is my usage of the xsl:variable as xsl:for-each select attribute incorrect?

背景和完整说明:

我正在使用 XSLT 生成以下 XML 结构中可用信息的 RDF/XML 表示.

I am using XSLT to generate a RDF/XML representation of information available in the following XML structure.

在这种情况下,XML 的真正含义是一个进程运行了两次;第一次生成输出文件a",第二次生成输出文件b".参数p1"和p2"是生成文件a"的执行的输入,参数p3"是生成文件b"的执行的输入.

In this case what the XML really means is that a process was run twice; the first time generating output file "a" and the second time generation output file "b". The parameters "p1" and "p2" were inputs for the execution that generated file "a" and the parameter "p3" was an input to the execution that generated file "b".

对于流程"的每个输出,我生成一个 RDF 个体并定义该流程执行的输入和输出.基本上,我想将/example/inputs/dataset[n]/parameters 中的所有值定义为生成输出/example/process/outputs/file[n] 的过程的输入.

For each output of 'process' I am generating a RDF individual and defining the inputs and outputs for that process execution. Basically, I want to define all values from /example/inputs/dataset[n]/parameters as inputs to the process which generates the output /example/process/outputs/file[n].

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 version="1.0">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="//process">
  <xsl:for-each select="outputs/*">

  <!-- declare rdf resource of type Process, define Process outputs -->
  <!-- ... this I already have working so I have withheld for brevity -->

  <!-- define input parameters -->

   <xsl:variable name="input_params_query">
    <xsl:text disable-output-escaping="yes">/example/inputs/dataset[</xsl:text>
    <xsl:number value="position()" format="1" />
    <xsl:text disable-output-escaping="yes">]/parameter</xsl:text>
   </xsl:variable>

   <xsl:for-each select="$input_params_query">
    <input rdf:resource="#{@name}"/>
   </xsl:for-each>

  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

推荐答案

我将 xsl:variable 用作 xsl:for-each 选择属性是否不正确?"

"Is my usage of the xsl:variable as xsl:for-each select attribute incorrect?"

是的.select 属性中指定的值必须是有效节点集表达式,也就是说,它必须是计算结果为(可能为空)节点集的 Xpath 表达式.

yes. the value specified in select attributes must be a valide node-set expression, that is, it must be an Xpath expression that evaluates to a (possibly empty) node set.

您定义的变量是字符串类型.该字符串恰好是一个有效的 Xpath 表达式,但它仍然只是"一个字符串.

The variable you defined is of a string type. the string happens to be a valid Xpath expression, but it is still 'just' a string.

我认为你可以通过这样写来达到你想要的结果:

I think you can achieve your intended result by writing it like this:

<xsl:template match="//process">
  <xsl:for-each select="outputs/*">
    <!-- declare rdf resource of type Process, define Process outputs -->
    <!-- ... this I already have working so I have withheld for brevity -->

    <!-- define input parameters -->
    <xsl:variable name="position" select="position()"/>

    <xsl:for-each select="/example/inputs/dataset[$position]">
      <input rdf:resource="#{@name}"/>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

这篇关于使用 xsl:variable 的 xsl:for-each 选择问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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