Xslt 需要找到父节点id
这是我的示例输入Xslt 需要找到父节点id,xslt,Xslt,这是我的示例输入 <table id="1" style="width=100%"> <tr> <td id="1"> <table id="2" style="width=50%"> <tr> <td id="2"> </td>
<table id="1" style="width=100%">
<tr>
<td id="1">
<table id="2" style="width=50%">
<tr>
<td id="2">
</td>
</tr>
</table>
</td>
</tr>
</table>
我正在使用xslt1.0。当模板匹配“td”匹配时,我需要找到相应的表id值。例如,如果id=1的td匹配,我想从表(id=1)中获取样式属性值,如果id=2的td匹配,我想从表(id=2)中获取样式属性值。我已经在我的模板中编写了
祖先::表/@style
,但这两个td都引用了id=1的表的样式 假设您处于“td”上下文中,请使用以下XPath:
../../@style
针对您的示例测试XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="td">
<xsl:value-of select="../../@style"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
假设您处于“td”上下文中,请使用以下XPath:
../../@style
针对您的示例测试XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="td">
<xsl:value-of select="../../@style"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
我写过
my中的祖先::表/@style
模板
你很接近。因为在祖先
轴中可以有多个表
,所以需要获得第一个类似于祖先::表[1]/@style
的表。当然,如果您绝对确定始终存在一系列表->tr
->td
(非可选tbody
),那么您可以使用@Flack的答案
我写过
my中的祖先::表/@style
模板
你很接近。因为在祖先
轴中可以有多个表
,所以需要获得第一个类似于祖先::表[1]/@style
的表。当然,如果您绝对确定总是有一系列的表->tr
->td
(不是可选的tbody
),那么您可以使用@Flack的答案。试试这个XPath,它工作得非常好
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="td">
<xsl:value-of select="ancestor::table[1]/@style"/>
<xsl:apply-templates/>
</xsl:template>
试试这个XPath,它工作得很好
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="td">
<xsl:value-of select="ancestor::table[1]/@style"/>
<xsl:apply-templates/>
</xsl:template>