Xslt 如何将xpath表达式转换为xsl键?

Xslt 如何将xpath表达式转换为xsl键?,xslt,xpath,xslt-1.0,Xslt,Xpath,Xslt 1.0,我被XSLT键的一个简单问题所困扰 如果相关,我将被迫使用XSLT1.0解析器 示例XML: <updates> <update> <id>first</id> <pkglist> <collection arch='i686'> <package name='bin' version='1.0'/> </collection>

我被XSLT键的一个简单问题所困扰

如果相关,我将被迫使用XSLT1.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>
我在整个文档中查找所有具有“name”属性的包的“id”值。但在现实世界中,我有比手工列出更多的软件包

所以我想一把钥匙是最好的选择

<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/@name" use="../../../../id" />
当我期望得到像

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

我做错了什么?

这是您需要的关键:

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

你能给我们举一个你想要得到的输出的例子吗?
<xsl:key name="idByPackage" match="/updates//update/pkglist/collection/package/@name" use="../../../../pkglist/collection/package/@name" />
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
<xsl:key name="idByPackage" 
         match="update/id" use="../pkglist/collection/package/@name" />
<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