首页 文章

使用xsl:variable进行xsl:for-each选择时出现问题

提问于
浏览
1

Problem:

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

<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的值时,我得到:

/example/inputs/dataset[1]/parameter

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

我对xsl:变量的使用是否为xsl:for-each select属性不正确?

Background and Full Explanation:

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

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

对于'process'的每个输出,我生成一个RDF个体并定义该流程执行的输入和输出 . 基本上,我想将/ example / inputs / dataset [n] / parameters中的所有值定义为生成输出/ 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>

1 回答

  • 3

    “我对xsl:变量的使用是否为xsl:for-each select属性不正确?”

    是 . select属性中指定的值必须是valide节点集表达式,也就是说,它必须是一个评估为(可能为空)节点集的Xpath表达式 .

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

    我认为你可以通过这样编写来实现你想要的结果:

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

相关问题