XML XSL XSD Validation:



我很难引用所有3个。我已经编写了XML、XSD和XSL,但它似乎不适用于引用。下面是一个使用相同引用的简单示例。

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="email">
     <xs:complexType>
       <xs:sequence>
         <xs:element name="to" type="xs:string"/>
       </xs:sequence>
     </xs:complexType>
   </xs:element>
 </xs:schema> 

XML:

 <?xml version="1.0"?>
 <?xml-stylesheet type="text/xsl" href="email.xsl"?>
 <email
   xmlns="http://www.w3schools.com"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.w3schools.com email.xsd">
   <to>John</to>
 </email>

XSL:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="email">
          <h2>To</h2>
          <td><xsl:value-of select="John"/></td>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
 </xsl:stylesheet>

XSLT不起作用,因为email元素有一个名称空间,要使用XPath将元素与名称空间匹配,您必须明确声明前缀并使用它。

您需要这样编写XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:ws="http://www.w3schools.com"
  version="1.0"> 
  <xsl:template match="/"> 
    <html> 
      <body> 
        <xsl:for-each select="ws:email"> 
          <h2>To</h2> 
          <td><xsl:value-of select="ws:to"/></td> 
        </xsl:for-each> 
      </body> 
    </html> 
  </xsl:template> 

我不确定您对XSD的期望是什么:它看起来是正确的,但它不会以任何方式影响XSLT的应用。

email是XML中的根元素,因此每个XML文件只能有一个email元素——可能在它上面应该有一个不同的根元素。

还要注意,您正在生成可疑的HTML:一个不在表中的<td>

最新更新