【发布时间】:2019-08-29 09:05:21
【问题描述】:
我使用xsl:key 元素和key() 函数的方式似乎不起作用。我的语法有问题吗?
在 W3Schools 上查找了元素和函数,但在我看来,我的语法与他们的一致。 https://www.w3schools.com/xml/func_key.asp https://www.w3schools.com/xml/ref_xsl_el_key.asp
我的 xml 示例
<?xml version="1.0" encoding="UTF-8"?>
<customerdatabase>
<customer id="1" Shortname="Shortname1"/>
<customer id="2" Shortname="Shortname2"/>
<customer id="48412" Shortname="Shortname48412"/>
</customerdatabase>
我的 XSLT 示例
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:dn="myPersonalNamespace">
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:key name="contents" match="/customerdatabase/customer" use="@id"/>
<xsl:template name="MasterTemplate" match="customerdatabase">
<xsl:result-document href="keyTest.xmi">
<dn:Output xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dn="myPersonalNamespace">
<dn:Test1>
<xsl:for-each select="key('contents', 48412)">
<xsl:value-of select="@Shortname"/>
</xsl:for-each>
</dn:Test1>
<dn:Test2>
<xsl:value-of select="key('contents',1)/@Shortname"/>
</dn:Test2>
<dn:Test3>
<xsl:value-of select="key('contents',48412)/@Shortname"/>
</dn:Test3>
</dn:Output>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
生成的 keyTest.xmi 文件已创建,但 Testx 元素为空。
【问题讨论】: