XPath 1.0选择第一个节点回到祖先



我的xml如下:

<Query>
  <Comp>
    <Pers>
        <Emp>
            <Job>
                <Code>Not selected</Code>
            </Job>
        </Emp>
        <Emp>
            <Job>
                <Code>selected</Code>
            </Job>
        </Emp>
    </Pers>
  </Comp>
</Query>

我有一个xpath:/query/comp/pers/emp/job [code ='selected']/../../../../..

结果应只有一个&lt;EMP>符合条件

<Query>
  <Comp>
    <Pers>
        <Emp>
            <Job>
                <Code>selected</Code>
            </Job>
        </Emp>
    </Pers>
  </Comp>
</Query>        

我怎么能得到结果?该系统与祖先不使用::*。我必须使用'/..'填充祖先。

您不必在此处使用ancestor来获取<emp>标签,以下everdath应该选择符合您条件的任何<emp>标签:

/Query/Comp/Pers/Emp[Job[Code='selected']]

注意:您说您的结果应该是一个,在这种情况下,这是正确的,但是此表达式将返回 snodes符合您的标准


编辑:

您已经说您正在使用XSLT,并且在下面给了我一些片段,但是我仍然不确定您的实际结构。您可以使用XPath识别所有不等于selected的节点,然后使用XSLT复制除这些节点以外的所有内容。

// Copy's all nodes in the input to the output
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
  </xsl:copy>
</xsl:template>
// Matches specifically the Emp records that are not equal to selected and 
// applies no action to them to they do not appear in the output
<xsl:template match="/Query/Comp/Pers/Emp[Job[Code!='selected']]" />

上面的两个模板将您的输入转换为所需的输出!

最新更新