【问题标题】:xslt transform an xsd validated xmlxslt 转换经过 xsd 验证的 xml
【发布时间】:2015-08-25 11:11:11
【问题描述】:

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

问题

xsi:schemaLocation="Projekt.xsd" 添加到我的xml 文件的根元素时,xslt 转换不再起作用。当我不指定 xsd 文件时,转换工作并显示数据,但只要我添加 xsd 文件,就不再显示任何数据。

  • 我遗漏了什么,以便在将 xsd 文件添加到 schemaLocation 后仍能正确显示和转换数据?

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


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>

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

【问题讨论】:

    标签: xml xslt xsd xsd-validation


    【解决方案1】:

    schemaLocation 无关紧要,但您的输入样本在根元素上具有默认命名空间声明 xmlns="http://www.w3schools.com",它将所有元素放在该命名空间中。然而,您的 XSLT 使用像 school/personen/person 这样的路径(在 XSLT/XPath 1.0 中)选择没有命名空间中的元素。要么移动到 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 等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2020-02-26
      • 2010-12-17
      • 2021-09-04
      • 2012-03-19
      • 2018-05-24
      • 1970-01-01
      相关资源
      最近更新 更多