gpt4 book ai didi

xslt - 所有X元素都在Y以下,但在另一个之后,Y

转载 作者:行者123 更新时间:2023-12-03 17:05:36 25 4
gpt4 key购买 nike

<div n="a">
. . .
. . .
<spec>red</spec>
<div n="d">
. . .
</div>
<spec>green</spec>
. . .
<div n="b">
. . .
<spec>blue</spec>
. . .
</div>
<div n="c">
<spec>yellow</spec>
</div>
. . .
. . .
. . .
</div>


[编辑删除了肖恩注意到的歧义。 - 谢谢]

当当前元素为 <div n="a">时,我需要一个XPATH表达式,该表达式返回红色和绿色元素,但不返回蓝色和黄色元素,就像 .//spec一样。

当前元素为 <div n="b">时,相同的表达式需要返回blue元素; <div n="c">,黄色元素。

类似于 .//spec[but no deeper than another div if there is one]

最佳答案

在XSLT 1.0中,假设当前节点是div

.//spec[generate-id(current())=generate-id(ancestor::div[1])]


在XSLT 2.0中,在相同的假设下:

.//spec[ancestor::div[1] is current()]


还有一个纯XPath 2.0表达式:

for $this in .
return
$this//spec[ancestor::div[1] is $this]




完整的XSLT 1.0转换:

<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="div">
<div n="{@n}"/>
<xsl:copy-of select=
".//spec[generate-id(current())=generate-id(ancestor::div[1])]"/>
==============
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>


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

<div n="a">
. . .
. . .
<spec>red</spec>
<spec>green</spec>
. . .
<div n="b">
. . .
<spec>blue</spec>
. . .
</div>
<div n="c">
<spec>yellow</spec>
</div>
. . .
. . .
. . .
</div>


所需的正确结果产生了:

<div n="a"/>
<spec>red</spec>
<spec>green</spec>
==============
<div n="b"/>
<spec>blue</spec>
==============
<div n="c"/>
<spec>yellow</spec>
==============




完整的XSLT 2.0转换:

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

<xsl:template match="div">

<div n="{@n}"/>
<xsl:sequence select=".//spec[ancestor::div[1] is current()]"/>
===================================
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>


当应用于相同的XML文档(上面)时,会产生相同的正确结果:

<div n="a"/>
<spec>red</spec>
<spec>green</spec>
===================================
<div n="b"/>
<spec>blue</spec>
===================================
<div n="c"/>
<spec>yellow</spec>
===================================




并使用纯XPath 2.0(无 current()):

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

<xsl:template match="div">

<div n="{@n}"/>
<xsl:sequence select="
for $this in .
return
$this//spec[ancestor::div[1] is $this]"/>
===================================
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>


产生相同的正确结果:

<div n="a"/>
<spec>red</spec>
<spec>green</spec>
===================================
<div n="b"/>
<spec>blue</spec>
===================================
<div n="c"/>
<spec>yellow</spec>
===================================

关于xslt - 所有X元素都在Y以下,但在另一个之后,Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com