Reputation: 4645
I'm trying to resolve an id/idref reference in xslt. Unfortunately, no data is displayed... am I doing something wrong?
Since I defined projektId
as type ID
I should be able to use the id(...)
-function or am I wrong?
<?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>[email protected]</email>
<projektRef projektIdRef="p1" />
</person>
<person id="2">
<name>b</name>
<kuerzel>b</kuerzel>
<email>[email protected]</email>
<projektRef projektIdRef="p1" />
</person>
<person id="3">
<name>c</name>
<kuerzel>c</kuerzel>
<email>[email protected]</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>
<?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>
<?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>
I also tried to use the following function:
<td><xsl:value-of select="key('projektKey', @projektId)/name" /></td>
but I didn't get any data displayed as well...
What am I doing wrong?
Thanks in advance
Upvotes: 1
Views: 679
Reputation: 116993
I am not sure which XSLT 1.0 processors (if any) support the id()
function (and - as Martin Honnen points out - it needs a DTD, not an XSD).
With regard to using a key, you have several syntax errors. Your key should be:
<xsl:key name="projectKey" match="df:projekt" use="@projektId" />
and you need to call it as:
<xsl:value-of select="key('projectKey', df:projektRef/@projektIdRef)/df:name" />
Note also that df:zhaw
needs to be df:school
.
Upvotes: 1
Reputation: 167571
XSLT 1.0 does not have any supported for schema defined types, you would need to define a DTD and make sure you use an XML parser and XSLT processor that support and parse external DTDs (which for instance the XSLT processors in browsers don't (Mozilla) do or only do when requested (IE with MSXML)) to be able to use the id
function.
As for your attempts to use a schema, the schemaLocation
attribute takes pairs of namespace-URI schema-URI
, not a single URI.
And to use a key you need to set it up consistently:
<?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>
Upvotes: 1