xml XSLT - 比较前兄弟的元素与当前的节点元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2990290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT - Comparing preceding-sibling's elements with current's node element
提问by siondream
I have this XML file:
我有这个 XML 文件:
<recursos>
<recurso url="http://w3c.com">
<descripcion>Consorcio W3C</descripcion>
<tipo>externo</tipo>
<idioma>ingles</idioma>
<contenido>General</contenido>
<unidad>Unidad 2</unidad>
</recurso>
<recurso url="http://html.com">
<descripcion>Especificación HTML</descripcion>
<tipo>externo</tipo>
<idioma>castellano</idioma>
<contenido>HTML</contenido>
<version>4.01</version>
<unidad>Unidad 3</unidad>
</recurso>
</recursos>
I want to compare one "recurso"'s preceding sibling element "unidad" with the "unidad" of the current "recurso" to check if they're different.
我想将一个“recurso”的前一个兄弟元素“unidad”与当前“recurso”的“unidad”进行比较,以检查它们是否不同。
I was trying:
我在尝试:
<xsl:if test="preceding-sibling::recurso[position()=1]::unidad != unidad">
</xsl:if>
But I know it's horribly wrong :( I hope you could help me, thank you very much.
但我知道这是非常错误的:(我希望你能帮助我,非常感谢。
回答by Tomalak
Almost correct.
几乎正确。
<xsl:if test="preceding-sibling::recurso[1]/unidad != unidad">
</xsl:if>
The ::is for axes, not for moving along a path ("making a location step"). In XPath terminology:
的::是轴,而不是沿路径移动(“制作位置步骤”)。在 XPath 术语中:
preceding-sibling::recurso[1]/unidad != unidad
''''''''''''''''' ++++++++++ ++++++
###
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
' = axis name (optional, defaults to "child")
+ = node test (required)
# = predicate (optional, for filtering)
~ = location step (required at least once per select expression)
The [1]is a shorthand for [position()=1].
该[1]是一个速记[position()=1]。
The childaxis is implicit in a location step, so this
该child轴是在一个位置步骤隐式的,所以这
preceding-sibling::recurso[1]/unidad != unidad
is equivalent to this:
相当于:
preceding-sibling::recurso[1]/child::unidad != unidad

