2

我有以下 XML 输入

<a href="h1" />
<a href="h2" />
<a href="h3" />

<b id="h1">E1</b>
<b id="h2">E1</b>
<b id="h3">E2</b>
<b id="h4">E3</b>
<b id="h5">E3</b>
<b id="h6">E4</b>
<b id="h7">E5</b>

有没有办法使用 XSLT2/Xpath2 仅获取 h1 和 h3 引用不同的值 E1 和 E2 并忽略 h2,因为它引用相同的值 E1?

谢谢。

4

3 回答 3

1

使用, 假设ab是顶部元素的子元素:

   for $href in /*/a/@href,
       $b in /*/b[@id = $href and not(. = preceding-sibling::b)]
     return
       string($href)

基于 XSLT-2 的验证

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:sequence select=
      "for $href in /*/a/@href,
           $b in /*/b[@id = $href and not(. = preceding-sibling::b)]
          return
             string($href)
      "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<html>
    <a href="h1" />
    <a href="h2" />
    <a href="h3" />
    <b id="h1">E1</b>
    <b id="h2">E1</b>
    <b id="h3">E2</b>
</html>

计算 XPath 表达式,并将计算结果复制到输出

h1 h3

更新

正如 Michael Kay 所指出的,上述解决方案是 O(N^2) 并且在有很多b兄弟姐妹的情况下可能会很慢。

这是一个至少是线性(或更快)的 XSLT 2.0 解决方案:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kReferrer" match="b" use="@id"/>

 <xsl:template match="/*">
   <xsl:for-each-group select="key('kReferrer', a/@href)" group-by=".">
     <xsl:sequence select="string(@id)"/>
   </xsl:for-each-group>
 </xsl:template>
</xsl:stylesheet>

同样,当此转换应用于同一个 XML 文档(如上)时,会产生所需的正确结果

h1 h3
于 2012-09-27T17:53:25.867 回答
1

你可以试试这个 XPath:

/*/b[@id = /*/a/@href and not(preceding::b = .)]
于 2012-09-27T18:21:43.927 回答
1

我无法看到<a>元素扮演什么角色 - 在我看来问题只涉及b元素 - 我是否完全误解了它?

给定您的输入,将给定节点的父节点作为上下文项,

<xsl:for-each-group select="b" group-by=".">
  <xsl:value-of select="current-group()[1]/@id"/>
</xsl:for-each-group>

将返回 h1 h2

于 2012-09-27T21:51:52.503 回答