gpt4 book ai didi

xslt - 如何将 xpath 表达式转换为 xsl key ?

转载 作者:行者123 更新时间:2023-12-04 20:02:55 24 4
gpt4 key购买 nike

我被 XSLT key 的一个简单问题困住了。

如果相关,我将被迫使用 XSLT 1.0 解析器。

示例 XML:

<updates>
<update>
<id>first</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.0'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.0'/>
</collection>
</pkglist>
</update>
<update>
<id>second</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.1'/>
<package name='conf' version='1.1'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.2'/>
<package name='conf' version='1.1'/>
</collection>
</pkglist>
</update>
<update>
<id>third</id>
<pkglist>
<collection arch='i686'>
<package name='bin' version='1.3'/>
<package name='conf' version='1.1'/>
<package name='src' version='1.3'/>
</collection>
<collection arch='x86_64'>
<package name='bin' version='1.3'/>
<package name='conf' version='1.2'/>
<package name='src' version='1.3'/>
</collection>
</pkglist>
</update>
</updates>

这个 XPATH 选择我正在寻找的东西

/updates//update[pkglist/collection/package/@name = 'bin']/id/text()

我正在查找整个文档中所有包含属性为“name”的包的“id”值。但在现实世界中,我有比手动列出更多的包裹。

所以我认为 key 是可行的方法

<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/@name" use="../../../../id" />

但这并没有给我任何有用的东西

<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/@name" use="../../../../id" />

<xsl:template match="/">
<xsl:apply-templates select="updates/update" />
</xsl:template>
<xsl:template match="update">
Updates:
<xsl:value-of select="id" />
Related updates
<xsl:apply-templates select="pkglist" />
</xsl:template>
<xsl:template match="pkglist">
<xsl:for-each select="key('idByPackage', collection/package/@name)">
<xsl:value-of select="." />
<xsl:value-of select="collection/package/@name" />
</xsl:for-each>
</xsl:template>

当我将 key 更改为此时,我知道我在正确的区域:

<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/@name" use="../../../../pkglist/collection/package/@name" />

相同的 xsl 模板吐出我的包名称。

当我使用“对我来说看起来有效但不起作用”键运行模板时,我得到了这个:

Updates:
first
Related updates
Updates:
second
Related updates
Updates: third
Related updates

当我期望得到类似的东西时

Updates:
first
Related updates
second
third
Updates:
second
Related updates
first
third
Updates: third
Related updates
first
second

我做错了什么?

最佳答案

这是您需要的 key :

<xsl:key name="idByPackage" 
match="update/id" use="../pkglist/collection/package/@name" />

当此 XSLT 在您的样本输入上运行时:

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

<xsl:key name="idByPackage"
match="update/id" use="../pkglist/collection/package/@name" />
<xsl:variable name="nl" select="'&#xA;'" />

<xsl:template match="/">
<xsl:apply-templates select="updates/update" />
</xsl:template>

<xsl:template match="update">
<xsl:value-of select="concat('Updates:', $nl,
id, $nl,
'Related updates:', $nl)" />

<xsl:variable name="name" select="pkglist/collection/package/@name" />
<xsl:apply-templates select="key('idByPackage', $name)[. != current()/id]" />
</xsl:template>

<xsl:template match="id">
<xsl:value-of select="concat(., $nl)" />
</xsl:template>

</xsl:stylesheet>

结果是:

Updates:
first
Related updates:
second
third
Updates:
second
Related updates:
first
third
Updates:
third
Related updates:
first
second

关于xslt - 如何将 xpath 表达式转换为 xsl key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16109713/

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