解析 xslt 中的引用

Resolve reference in xslt

我正在尝试解析 xslt 中的 id/idref 引用。 不幸的是,没有显示任何数据...我做错了什么吗?

因为我将 projektId 定义为 ID 类型,所以我应该可以使用 id(...) 函数,还是我错了?


XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Projekt.xsl"?>
<school xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Projekt.xsd">
    <personen>
        <person id="1">
            <name>a</name>
            <kuerzel>a</kuerzel>
            <email>a@a.ch</email>
            <projektRef projektIdRef="p1" />
        </person>
        <person id="2">
            <name>b</name>
            <kuerzel>b</kuerzel>
            <email>b@b.ch</email>
            <projektRef projektIdRef="p1" />
        </person>
        <person id="3">
            <name>c</name>
            <kuerzel>c</kuerzel>
            <email>c@c.ch</email>
            <projektRef projektIdRef="p2" />
        </person>
    </personen>

    <projekte>
        <projekt projektId="p1">
            <name>Projekt 1</name>
        </projekt>
        <projekt projektId="p2">
            <name>Projekt 2</name>
        </projekt>
    </projekte>
</school>

XSD

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="school">
  <xs:complexType>
    <xs:sequence>
        <xs:element name="personen" maxOccurs="unbounded">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                  <xs:sequence>
                    <xs:element name="person">
                        <xs:complexType>
                            <xs:sequence>        
                                <xs:element name="name" type="xs:string"/>
                                <xs:element name="kuerzel" type="xs:string"/>
                                <xs:element name="email" type="xs:string"/>
                                <xs:element name="projektRef">
                                    <xs:complexType>
                                        <xs:attribute name="projektIdRef" type="xs:IDREF" />
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                            <xs:attribute name="id" type="xs:integer" />
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
              </xs:choice>
            </xs:complexType>
        </xs:element>

        <xs:element name="projekte" maxOccurs="unbounded">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:sequence>
                        <xs:element name="projekt">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="name" type="xs:string" />
                                </xs:sequence>
                                <xs:attribute name="projektId" type="xs:ID" />
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://www.w3schools.com">
<xsl:key name="projectKey" match="df:projekte/projekt" use="@projektId" />

<xsl:template match="/">
  <html>
  <body>
  <h2>School</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Kürzel</th>
        <th style="text-align:left">Email</th>
        <th style="text-align:left">Link</th>
        <th style="text-align:left">Project</th>
      </tr>
      <xsl:for-each select="df:school/df:personen/df:person">
      <tr>
        <td><xsl:value-of select="df:name"/></td>
        <td><xsl:value-of select="df:kuerzel"/></td>
        <td><a href="mailto:{df:email}"><xsl:value-of select="df:email" /></a></td>
        <td><img src="http://test.com/portraet/images/{df:kuerzel}.jpg" width="100px" height="100px"/></td>
        <td><xsl:value-of select="id(projekte/@projektId)/name" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我也试过使用以下功能:

<td><xsl:value-of select="key('projektKey', @projektId)/name" /></td>

但我也没有显示任何数据...

我做错了什么?

提前致谢

XSLT 1.0 不支持模式定义的类型,您需要定义 DTD 并确保使用支持和解析外部 DTD(例如浏览器中的 XSLT 处理器不 (Mozilla) 执行或仅在请求时执行(IE with MSXML))才能使用 id 函数。

至于您尝试使用模式,schemaLocation 属性采用成对的 namespace-URI schema-URI,而不是单个 URI。

要使用密钥,您需要对其进行一致的设置:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:df="http://www.w3schools.com"
  exclude-result-prefixes="df">
<xsl:key name="projectKey" match="df:projekte/df:projekt" use="@projektId" />

<xsl:template match="/">
  <html>
  <body>
  <h2>School</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Name</th>
        <th style="text-align:left">Kürzel</th>
        <th style="text-align:left">Email</th>
        <th style="text-align:left">Link</th>
        <th style="text-align:left">Project</th>
      </tr>
      <xsl:for-each select="df:school/df:personen/df:person">
      <tr>
        <td><xsl:value-of select="df:name"/></td>
        <td><xsl:value-of select="df:kuerzel"/></td>
        <td><a href="mailto:{df:email}"><xsl:value-of select="df:email" /></a></td>
        <td><img src="http://pd.zhaw.ch/portraet/images/{df:kuerzel}.jpg" width="100px" height="100px"/></td>
        <td><xsl:value-of select="key('projectKey', df:projektRef/@projektIdRef)/df:name" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

我不确定哪个 XSLT 1.0 处理器(如果有的话)支持 id() 函数(而且——正如 Martin Honnen 指出的那样——它需要 DTD,而不是 XSD)。

关于使用键,您有几个语法错误。您的密钥应该是:

<xsl:key name="projectKey" match="df:projekt" use="@projektId" />

您需要将其命名为:

<xsl:value-of select="key('projectKey', df:projektRef/@projektIdRef)/df:name" />

另请注意,df:zhaw 需要为 df:school