跨XSLT节点集的不同值

跨XSLT节点集的不同值,xslt,xpath,node-set,Xslt,Xpath,Node Set,再次介绍节点集中的不同节点(基于属性值)。 假设你有以下结构: <struct> <a> <x id="1">a_1</x> <x id="2">a_2</x> <x id="3">a_3</x> </a> <b inherits="a"> <x id="2">b_2</x> </b> <

再次介绍节点集中的不同节点(基于属性值)。 假设你有以下结构:

<struct>
  <a>
    <x id="1">a_1</x>
    <x id="2">a_2</x>
    <x id="3">a_3</x>
  </a>
  <b inherits="a">
    <x id="2">b_2</x>
  </b>
</struct>
使用

  $vB/x
 |
  /*/*[name() = $vB/@inherits]
                /x[not(@id = $vB/x/@id)]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/b/x
     |
      /*/*[name() = /*/b/@inherits]
                    /x[not(@id = /*/b/x/@id)]

   "/>
 </xsl:template>
</xsl:stylesheet>
<struct>
    <a>
        <x id="1">a_1</x>
        <x id="2">a_2</x>
        <x id="3">a_3</x>
    </a>
    <b inherits="a">
        <x id="2">b_2</x>
    </b>
</struct>
<x id="1">a_1</x>
<x id="3">a_3</x>
<x id="2">b_2</x>
其中,
$vB
被定义为
b
元素

这将选择所有
b/x
元素和
x
子元素的并集(具有
id
属性,该属性不等于
b/x/@id
属性)的
struct/*
元素(
struct
的子元素),其名称为
b//code>的值

或者,根据OP在评论中的要求——无变量:

  /*/b/x
 |
  /*/*[name() = /*/b/@inherits]
                /x[not(@id = /*/b/x/@id)]
完成基于XSLT的验证

  $vB/x
 |
  /*/*[name() = $vB/@inherits]
                /x[not(@id = $vB/x/@id)]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/b/x
     |
      /*/*[name() = /*/b/@inherits]
                    /x[not(@id = /*/b/x/@id)]

   "/>
 </xsl:template>
</xsl:stylesheet>
<struct>
    <a>
        <x id="1">a_1</x>
        <x id="2">a_2</x>
        <x id="3">a_3</x>
    </a>
    <b inherits="a">
        <x id="2">b_2</x>
    </b>
</struct>
<x id="1">a_1</x>
<x id="3">a_3</x>
<x id="2">b_2</x>

当此转换应用于提供的(已更正为格式良好的)XML文档时

  $vB/x
 |
  /*/*[name() = $vB/@inherits]
                /x[not(@id = $vB/x/@id)]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/b/x
     |
      /*/*[name() = /*/b/@inherits]
                    /x[not(@id = /*/b/x/@id)]

   "/>
 </xsl:template>
</xsl:stylesheet>
<struct>
    <a>
        <x id="1">a_1</x>
        <x id="2">a_2</x>
        <x id="3">a_3</x>
    </a>
    <b inherits="a">
        <x id="2">b_2</x>
    </b>
</struct>
<x id="1">a_1</x>
<x id="3">a_3</x>
<x id="2">b_2</x>

a_1
a_2
a_3
b_2
计算单个XPath表达式并输出所选节点

  $vB/x
 |
  /*/*[name() = $vB/@inherits]
                /x[not(@id = $vB/x/@id)]
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/b/x
     |
      /*/*[name() = /*/b/@inherits]
                    /x[not(@id = /*/b/x/@id)]

   "/>
 </xsl:template>
</xsl:stylesheet>
<struct>
    <a>
        <x id="1">a_1</x>
        <x id="2">a_2</x>
        <x id="3">a_3</x>
    </a>
    <b inherits="a">
        <x id="2">b_2</x>
    </b>
</struct>
<x id="1">a_1</x>
<x id="3">a_3</x>
<x id="2">b_2</x>
a_1
a_3
b_2

谢谢你这么快的回答!但是,不声明变量也可以做同样的事情吗?是的,用
/*/b
@Savva Mikhalevski替换
$vB
:我用一个不引用任何变量的XPath表达式编辑了答案。不幸的是,我给出的问题描述不完整。请查看我的更新。更新的含义不清楚。当更新中的所有
x
元素都已具有不同的值时,结果应该是什么以及为什么需要“不同的
s”。请更新更新以使其清晰明了。