我正在尝试 XSLT 中的查找表示例,但无法使其工作
<?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 以及生成的类别相匹配。
<?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 http://www.ibm.com/developerworks/xml/library/x-xsltip.html。谢谢您的帮助 。
您的 XSLT 样式表包含错误- 根据spec http://www.w3.org/TR/xslt#section-Stylesheet-Structure,任何子元素xsl:stylesheet
(aka 顶级元素) 必须位于非空命名空间中:
“*此外,xsl:stylesheet
元素可以包含任何不包含的元素
来自 XSLT 命名空间,前提是
元素的扩展名称有一个
非空命名空间 URI。 ”
如果您使用的 XSLT 处理器没有引发错误,那么它是不合规且存在缺陷的,不应使用。查找并使用兼容的 XSLT 处理器(我正在使用 .NET XslCompiledTransform、Saxon 6.5.5,...等)。
还有其他错误。
Solution:
-
定义一个带有前缀(例如)“x:”的新名称空间:
改变嵌入的<classifications>
to <x:classifications>
-- 现在这符合规范。
-
对代码执行更多更改,直到获得此转换:
<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.在上面的代码中,请注意这一行:<xsl:for-each select="document('')">
.
这样做的目的是使样式表成为当前文档。这key()
函数仅对当前文档起作用,如果您想要嵌入classification
要索引和使用元素,您必须更改当前文档(通常采用这种方式)。在 XSLT 2.0 中key()
函数允许第三个参数,它是应使用索引的文档中的节点。
当此转换应用于提供的 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)