xslt 转换 xsd 验证 xml

xslt transform an xsd validated xml

schemaLocation 添加到我的 xml 文件时,我有一个非常奇怪的效果。

问题:

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.

信息: 所有文件都在同一个文件夹中


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>
        </person>
        <person id="2">
            <name>B</name>
            <kuerzel>b</kuerzel>
            <email>b@b.ch</email>
        </person>
        <person id="3">
            <name>C</name>
            <kuerzel>C</kuerzel>
            <email>c@c.ch</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>

有谁知道我的文件有什么问题吗?

schemaLocation 无关紧要,但您的输入示例在根元素上具有默认名称空间声明 xmlns="http://www.w3schools.com",它将所有元素放入该名称空间中。但是,您的 XSLT 使用 school/personen/person 之类的路径,其中(在 XSLT/XPath 1.0 中)select 元素不在任何名称空间中。移动到 XSLT 2.0 处理器,然后您可以在样式表上定义 xpath-default-namespace="http://www.w3schools.com" 或者,如果您想使用 XSLT 1.0 解决它,请在样式表中声明 xmlns:df="http://www.w3schools.com" 并调整路径以使用该前缀作为在 df:school/df:personen/df:persondf:name 等等。