xeraphim
xeraphim

Reputation: 4645

xslt transform an xsd validated xml

I have a very strange effect when adding the schemaLocation to my xml-file.

Problem:

When adding the xsi:schemaLocation="Projekt.xsd" to the root element of my xml file, the xslt transformation does not work anymore. When I don't specify the xsd-File, the transformation works and the data gets displayed but as soon as I add the xsd-File, no data are getting displayed anymore.

Info: All files are in the same folder


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>[email protected]</email>
        </person>
        <person id="2">
            <name>B</name>
            <kuerzel>b</kuerzel>
            <email>[email protected]</email>
        </person>
        <person id="3">
            <name>C</name>
            <kuerzel>C</kuerzel>
            <email>[email protected]</email>
        </person>
    </personen>
</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:sequence>
                            <xs:attribute name="id" type="xs:integer" />
                        </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">

<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">Image</th>
      </tr>
      <xsl:for-each select="school/personen/person">
      <tr>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="kuerzel"/></td>
        <td><a href="mailto:{email}"><xsl:value-of select="email" /></a></td>
        <td><img src="http://tempUri.com/{kuerzel}.jpg" width="100px" height="100px"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Does anyone have an idea what's wrong in my files?

Upvotes: 0

Views: 1307

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

The schemaLocation does not matter but your input sample has the default namespace declaration xmlns="http://www.w3schools.com" on the root element which puts all elements in that namespace. Your XSLT however uses paths like school/personen/person which (in XSLT/XPath 1.0) select elements in no namespace. Either move to an XSLT 2.0 processor where you can then define xpath-default-namespace="http://www.w3schools.com" on your stylesheet or, if you want to solve it with XSLT 1.0, declare xmlns:df="http://www.w3schools.com" in your stylesheet and adapt your paths to use that prefix as in df:school/df:personen/df:person and df:name and so on.

Upvotes: 1

Related Questions