Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 需要找到父节点id_Xslt - Fatal编程技术网

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>