无法使 XSLT select id() 函数成功运行 [英] Unable to get the XSLT select id() function to work successfully
问题描述
根据下面的示例代码,我无法让 id() 函数在我的 xslt 转换中工作,我不知道为什么.当我将 data.xml 加载到浏览器中时,我只希望看到名称 &每个样式表/转换的BC"项的大小值.有什么建议?仅供参考:我已经改编了来自另一个站点的示例.
I'm having trouble getting the id() function to work in my xslt transformation per the example code below and I'm not sure why. When I load data.xml into a browser I'm expecting to see just the name & size values of the 'BC' item per the stylesheet/transformation. Any suggestions? FYI: I've adapted the example from another site.
文件:transform.xslt
File: transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<html>
<body>
<xsl:value-of select="id('BC')/name" />
<xsl:value-of select="id('BC')/size" />
</body>
</html>
</xsl:template>
文件:data.xml
File: data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xslt" ?>
<provinces xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="data.xsd">
<province id="AB">
<name>Alberta</name>
<size>33</size>
</province>
<province id="BC">
<name>British Columbia</name>
<size>44</size>
</province>
<province id="MB">
<name>Manitoba</name>
<size>55</size>
</province>
</provinces>
文件:data.xsd
File: data.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="provinces">
<xs:complexType>
<xs:sequence>
<xs:element ref="province" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="province">
<xs:complexType>
<xs:sequence>
<xs:element name="name" />
<xs:element name="size" />
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
推荐答案
仅仅因为属性被命名为 id 并不能使它成为 ID 属性:它必须在 DTD 或模式中声明为 ID(或者它可以如果处理器支持,则命名为 xml:id).
Just because the attribute is named id doesn't make it an ID attribute: it has to be declared as an ID in the DTD or schema (or it can be named xml:id if the processor supports that).
在这里切换到使用 key() 可能是最简单的.
It's probably simplest here to switch to using key() instead.
这篇关于无法使 XSLT select id() 函数成功运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!