在XSLT 1.0中,如何使用key和document('')在XSLT中嵌入的XML数据上创建查找功能? [英] In XSLT 1.0 how to create a lookup function on XML data embedded in the XSLT using key and document('')?
问题描述
我的具体需求是在给定特定字体大小的情况下查找行高,但是我正在寻求学习创建查找/特定映射的通用技术.
My specific need is to lookup line-heights given a particular font-size, but I am seeking to learn a general technique of creating lookups/a specific mapping.
我认为可以将XML嵌入XSLT文档本身中(这样它可以独立工作),并可以使用document('')函数在其上构建键来引用当前的XSLT,如下所示:
I think it is possible to embed XML in the XSLT document itself (so it can work standalone) and build a key on it using the document('') function to reference the current XSLT, something like this:
<xsl:variable name="data.font-metrics.line-height">
<line-height font-size="11">12</line-height>
<line-height font-size="12">14</line-height>
<line-height font-size="13">15</line-height>
<line-height font-size="14">16</line-height>
<line-height font-size="15">17</line-height>
<line-height font-size="16">18</line-height>
<line-height font-size="17">20</line-height>
<line-height font-size="18">21</line-height>
</xsl:variable>
<xsl:key name="lookup.font-metrics.line-height" match="document('')//xsl:variable[@name='data.font-metrics.line-height'])/line-height" use="@font-size"/>
在那之后,我应该能够使用按键功能查找线高:
After that, I should be able to lookup a line height using the key function:
<xsl:value-of select="key('lookup.font-metrics.line-height',$font-size)"/>
...但是我收到以下错误消息:
...however I am getting the following error message:
XPath error : Invalid expression
//document('')//xsl:variable[@name='data.font-metrics.line-height'])/line-height/text()
^
我认为这里出现了几个问题:
I think several problems are coming together here:
- 使用文档功能
- 按键功能的使用
- 嵌入XML的最佳方法是什么?在变量中?
对于这个问题,也可能有完全不同的解决方案.
There may also be a completely different solution to the problem.
非常感谢您的帮助!
推荐答案
在XSLT 1.0中,key()函数仅在当前文档的上下文中起作用(在XSLT 2.0中,它具有第三个参数,允许您选择语境).为了在另一个文档中的节点上使用键,您必须首先将上下文切换到该文档-例如:
In XSLT 1.0, the key() function works only in the context of the current document (in XSLT 2.0 it has a third argument, allowing you to select the context). In order to use the key on nodes in another document, you must first switch the context to that document - for example:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="data.font-metrics.line-height">
<line-height font-size="11">12</line-height>
<line-height font-size="12">14</line-height>
<line-height font-size="13">15</line-height>
<line-height font-size="14">16</line-height>
<line-height font-size="15">17</line-height>
<line-height font-size="16">18</line-height>
<line-height font-size="17">20</line-height>
<line-height font-size="18">21</line-height>
</xsl:variable>
<xsl:key name="lookup.font-metrics.line-height" match="line-height" use="@font-size"/>
<xsl:template match="/">
<xsl:param name="font-size" select="14"/>
<output>
<!-- other nodes -->
<!-- switch context to the stylesheet itself in order to use the key -->
<xsl:for-each select="document('')">
<lookup>
<xsl:value-of select="key('lookup.font-metrics.line-height', $font-size)"/>
</lookup>
</xsl:for-each>
<!-- more nodes -->
</output>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<lookup>16</lookup>
</output>
请注意, xsl:key
元素在此开关中不起作用,可以用更简单的术语来定义.
Note that the xsl:key
element is not playing a part in this switch and can be defined in much simpler terms.
这篇关于在XSLT 1.0中,如何使用key和document('')在XSLT中嵌入的XML数据上创建查找功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!