XSLT 键()查找 [英] XSLT key() lookup

查看:22
本文介绍了XSLT 键()查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 XSLT 中尝试查找表的示例,但无法使其正常工作

I'm trying out a sample of look-up tables in XSLT and am not able to get it to work

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" />
   <xsl:key name="classification-lookup" match="classification" use="id" />
   <xsl:variable name="classification-top" select="document('')/*/classifications" />
   <xsl:template match="BusinessListing">
      <listing>
         <id>
            <xsl:value-of select="id" />
         </id>
         <xsl:apply-templates select="$classification-top">
            <xsl:with-param name="curr-label" select="." />
         </xsl:apply-templates>
      </listing>
   </xsl:template>
   <xsl:template match="classifications">
      <xsl:param name="curr-label" />
      <category>
         <xsl:value-of select="key('classification-lookup', $curr-label/listingData/classifications/classificationId)/description" />
      </category>
   </xsl:template>
   <classifications>
      <classification>
         <id>7981</id>
         <description>Category1</description>
      </classification>
      <classification>
         <id>7982</id>
         <description>Category2</description>
      </classification>
      <classification>
         <id>7983</id>
         <description>Category3</description>
      </classification>
      <classification>
         <id>7984</id>
         <description>Category4</description>
      </classification>
   </classifications>
</xsl:stylesheet>

来源如下.

<?xml version="1.0"?>
<BusinessListings>
<BusinessListing>
    <id>1593469</id>
    <listingData>
        <classifications>
            <classificationId>7982</classificationId>
            <classificationId>7983</classificationId>
        </classifications>
    </listingData>
</BusinessListing>
</BusinessListings>

在下面的结果中,类别为空,但我需要来自源的分类 ID 与分类标签中的 id 和生成的类别相匹配.

In the result below , The category is empty but I need the Classification Id from the source to be matched with the id in the classification tag and the category generated .

<?xml version="1.0" encoding="UTF-8"?>

<listing>
<id>1593469</id> -- Empty I need the Category2 and Category3 here
<category/>
</listing>

我知道我可能不太清楚,但我刚刚开始使用 XSLT 并在此处引用示例 http://www.ibm.com/developerworks/xml/library/x-xsltip.html.感谢您的帮助.

I know that I may be wide off mark but I've just started off with XSLT and referred the sample here http://www.ibm.com/developerworks/xml/library/x-xsltip.html . Thanks for the help .

推荐答案

您的 XSLT 样式表包含错误 -- 根据 specxsl:stylesheet 的任何子元素(又名 top-level 元素) 必须位于非空命名空间中:

Your XSLT stylesheet contains an error -- according to spec, any child-element of xsl:stylesheet (aka top-level element) must be in a non-null namespace:

"*此外,xsl:stylesheet元素可以包含任何不包含的元素来自 XSLT 名称空间,前提是元素的扩展名有一个非空命名空间 URI."

"*In addition, the xsl:stylesheet element may contain any element not from the XSLT namespace, provided that the expanded-name of the element has a non-null namespace URI. "

如果您使用的 XSLT 处理器没有引发错误,那么它是不合规且有缺陷的,不应使用.查找并使用兼容的 XSLT 处理器(我使用的是 .NET XslCompiledTransform、Saxon 6.5.5 等).

If the XSLT processor you are using doesn't raise an error, then it is non-compliant and buggy and shouldn't be used. Find and use a compliant XSLT processor (I am using .NET XslCompiledTransform, Saxon 6.5.5, ..., etc).

还有其他错误.

解决方案:

  1. 定义一个带有前缀(比如)x:"的新命名空间:

  1. Define a new namespace with prefix (say) "x:":

将嵌入的 更改为 -- 现在这符合规范.

Change the embedded <classifications> to <x:classifications> -- now this conforms to the Spec.

对代码执行更多更改,直到获得此转换:

Perform more changes to the code until you get this transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="my:x" exclude-result-prefixes="x">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="classification-lookup" match="classification"
         use="id" />

    <xsl:template match="BusinessListing">
        <listing>
            <id>
                <xsl:value-of select="id" />
            </id>
            <xsl:apply-templates/>
        </listing>
    </xsl:template>

    <xsl:template match="classificationId">
     <xsl:variable name="vCur" select="."/>
        <xsl:for-each select="document('')">
         <category>
            <xsl:value-of select=
            "key('classification-lookup',$vCur)/description" />
         </category>
        </xsl:for-each>
    </xsl:template>

 <xsl:template match="text()"/> 

 <x:classifications>
    <classification>
        <id>7981</id>
        <description>Category1</description>
    </classification>
    <classification>
        <id>7982</id>
        <description>Category2</description>
    </classification>
    <classification>
        <id>7983</id>
        <description>Category3</description>
    </classification>
    <classification>
        <id>7984</id>
        <description>Category4</description>
    </classification>
 </x:classifications>
</xsl:stylesheet>

.4.在上面的代码中注意这一行: .

.4. In the above code notice the line: <xsl:for-each select="document('')"> .

这样做的目的是使样式表成为当前文档.key() 函数只作用于当前文档,如果你希望嵌入的 classification 元素被索引和使用,你必须改变当前文档(通常是这种方式)).在 XSLT 2.0 中,key() 函数允许第三个参数,它是应该使用其索引的文档中的一个节点.

The purpose of this is to make the stylesheet the current document. The key() function operates only on the current document and if you want the embedded classification elements to be indexed and used, you must change the current document (usually in this way). In XSLT 2.0 the key() function allows a 3rd argument which is a node from the document whose index should be used.

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

    <BusinessListings>
        <BusinessListing>
            <id>1593469</id>
            <listingData>
                <classifications>
                    <classificationId>7982</classificationId>
                    <classificationId>7983</classificationId>
                </classifications>
            </listingData>
        </BusinessListing>
    </BusinessListings>

产生了想要的、正确的结果:

<listing>
   <id>1593469</id>
   <category>Category2</category>
   <category>Category3</category>
</listing>

这篇关于XSLT 键()查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆