【问题标题】:Select attribute value of first node of ancestor选择祖先第一个节点的属性值
【发布时间】:2015-02-06 06:34:02
【问题描述】:

想要的 HTML:

<table>
 <thead>
  <tr>
   <th>1</th>
   <th>2</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>a</td>
   <td>b</td>
  </tr>
  <tr>
   <td>
    <table>
     <tr>
      <td>thisWidth</td>
     </tr>
    </table>
   </td>
  </tr>
 </tbody>
</table>

XML:

<row type='header'>
 <column width='150'>1</column>
 <column>2</column>
 <column type='tableintable' />
</row>
<row type='data'>
 <column>a</column>
 <column>b</column>
 <column>thisWidth</column>
</row>

一切都如我所愿,我只想在 HTML 中设置 &lt;td&gt; 元素的宽度。这必须是第一个 XML 标题列的宽度。
现在我正在使用这个:

<xsl:attribute name="width"><xsl:value-of select="//row[@type='header']/@width[position() = 1]" /></xsl:attribute>

这不起作用。如何选择所需列的值?

【问题讨论】:

    标签: xml xslt xpath nodes ancestor


    【解决方案1】:

    试试这个 XPath 表达式

    <xsl:attribute name="width">
        <xsl:value-of select="//row[@type='header']/column[position() = 1]/@width" />
    </xsl:attribute>
    

    【讨论】:

    • 你想像这样//row[1][@type='header']/column[position() = 1]/@width 这样它保证在有多个“标题”行的情况下只选择第一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2017-02-10
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多