XPath position where text (or attribute) is equal to value

泄露秘密 提交于 2019-12-11 07:42:25

问题


XML:

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

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>

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>

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

回答1:


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]" />

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]" />


来源:https://stackoverflow.com/questions/27354417/xpath-position-where-text-or-attribute-is-equal-to-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!