文本(或属性)等于值的 XPath 位置 [英] XPath position where text (or attribute) is equal to value

查看:54
本文介绍了文本(或属性)等于值的 XPath 位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML:

<row type="header">
 <column>href</column>
 <column>other</column>
</row>
<row type="data">
 <column>a</column>
 <column>b</column>
</row>

XSLT:(我确保我在下面模板的行[@type='data']/列中)

XSLT:(I'm making sure I'm in row[@type='data']/column in the template below)

<xsl:template match="column" mode="panelTabsBody">
 <td>
  <a>
   <xsl:attribute name="href">
    <xsl:value-of select="../../row[@type='header']/column[text()='href'][position()]" />
   </xsl:attribute>
  </a>
 </td>
</xsl:template>

如何获取 text() 等于 href 的列位置?如果更容易,我可以为值为href"的列指定一个属性.示例:href

How can I get the column position where the text() is equal to href? If it's easier, I can give the column with the value 'href' an attribute. Example: <column type='link'>href</column>


我尝试了以下方法,但这没有用
XSLT:


I tried the following, but this didnt work
XSLT:

<xsl:template match="column" mode="panelTabsBody">
 <xsl:variable name="testt" select="count(../../row[@type='header']/column[text()='href']/preceding-sibling::column) + 1" />
 <td>
  <a>
   <xsl:attribute name="href">
    <xsl:value-of select="../../row[@type='header']/column[position() = testt]" />
   </xsl:attribute>
  </a>
 </td>
</xsl:template>

推荐答案

简单的答案是 testt 是一个变量,所以你需要使用一个 $ 前缀来参考一下

The simple answer is that testt is a variable, so you need to use a $ prefix to reference it

<xsl:value-of select="../../row[@type='header']/column[position() = $testt]" />

当然,这是为标题"行选择列,您已经知道该列包含href".也许你只需要这样做?

Of course, this is selecting the column for the 'header' row, which you already know contains "href". Perhaps you just need to do this?

<xsl:value-of select="../column[position() = $testt]" />

这篇关于文本(或属性)等于值的 XPath 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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