使用变量作为 XPath 选择的一部分 [英] Using a variable as part of a XPath selection

查看:35
本文介绍了使用变量作为 XPath 选择的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将变量用作 XPath 表达式的一部分.

I'm looking to use a variable as part of an XPath expression.

我的问题可能是 msxsl 节点集功能...不确定.但是不要让这影响您的判断...继续阅读...

My problem might be the msxsl node-set function... not sure. But don't let that cloud your judgement... read on...

我正在使用 .NET 函数加载内容文件,该文件通过定制的 XML 内容传入.@file 生成一个 XML 文件.

I'm using a .NET function to load up the content file, which is passed in via bespoke XML content. The @file results in an XML file.

页面上的定制 XML 如下所示:

The bespoke XML the sits on the page looks like :

<control name="import" file="information.xml" node="r:container/r:group[@id='set01']/r:item[1]" />

XSL 看起来像:

<xsl:variable name="document">
    <xsl:copy-of select="ext:getIncludedContent(@file)" />
</xsl:variable>

然后我将其转换为节点集,以便我可以查询文档

I'm then translating this to a node-set so I can query the document

<xsl:variable name="preset-xml" select="msxsl:node-set($document)" />   

我加载的源文件如下:

<container>
    <group id="set01">
        <item>value01</item>
        <item>value02</item>
        <item>value03</item>
    </group>
    <group id="set02">
        <item>value04</item>
        <item>value05</item>
    </group>                    
</container>

它一直工作到这一点.我可以看到源文件被导入并以 XML 格式输出.

It works up until this point. I can see the source file being brought thru and output as XML.

当我尝试使用从内容中输入的 XPath 表达式查询源文件时,我的问题就出现了.

My problem comes in when I am trying to query the source file with an XPath expression fed in from the content.

我试过了:

<xsl:value-of select="$preset-xml/@node" />

显然这是行不通的,因为它将 @node 寻找为 XML 中加载的直接子节点.

Clearly that doesn't work as it looks for @node as a direct child of the loaded in XML.

我试过了:

<xsl:variable name="node" select="@node" />
<xsl:value-of select="$preset-xml/$node" />

但它不喜欢第二个变量.

But it doesn't like the second variable.

我已经尝试过 concat($preset-xml,'/',$node),但这仍然在结果中绘制了整个文档.

I've tried concat($preset-xml,'/',$node), but that still draws out the entire document in the result.

我目前唯一能让它工作的方法是在模板中写出完整的表达式:

The only way I can get this working at the moment is to write out the full expression in the template :

<xsl:value-of select="$preset-xml/r:container/r:group[@id='set01']/r:item[1]" />

正确地通过 value01

但那是一个硬编码的解决方案.我想开发一个解决方案,该解决方案可以通过内容文件中声明的参数从内容中进行操作以适应任何类型的导入内容.

But that is then a hard coded solution. I want to develop a solution which can be manipulated from the content to suit any sort of imported content, with the parameters declared in the content file.

附言我正在使用 XSLT 1.0,因为技术管理员不会更改 Microsoft 解析器以支持更高版本的 XSL,因此在编写任何解决方案时都需要考虑到这一点.

p.s. I'm using XSLT 1.0 because the tech admin won't change the Microsoft parser to support later versions of XSL, so any solution would need to be written with that in mind.

推荐答案

一个可能的技巧是调用一个参数化的 xsl:template,它执行关键的选择比较部分,并将它的输出作为字符串进行评估.

One possible trick is to call an parametrized xsl:template which carries out the crucial select comparison part and evaluate it's output as string.

我有一个类似的问题(计算任意属性值的所有节点),我是这样解决的(顺便说一句:$sd = 'document($filename)' 所以我加载了一个二级 xml 文件内容):

I had a similar problem (count all nodes of an arbitrary attribute value) which I solved this way (btw: $sd = 'document($filename)' so I load a secondary xml files content):

<xsl:template match="/">
  <xsl:variable name="string_cntsuccess">
    <xsl:for-each select="./Testcase">
      <xsl:variable name="tcname" select="@location"/>
      <xsl:for-each select="$sd//TestCase[@state='Passed']">
        <xsl:call-template name="producedots">
          <xsl:with-param name="name" select="$tcname"/>
        </xsl:call-template>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="cntsuccess" select="string-length($string_cntsuccess)"/>
</xsl:template>

<xsl:template name="producedots">
  <xsl:param name="name"/>
  <xsl:variable name="ename" select="@name"/>
  <xsl:if test="$name = $ename">
    <xsl:text>.</xsl:text>
  </xsl:if>
</xsl:template>

这篇关于使用变量作为 XPath 选择的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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