2016-05-08 35 views
1

我需要关于XSL和XML Schema的帮助。
这是XML文件:XML,XSL,XML Schema

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="spellstyle.xsl"?> 
<spells xmlns="spels.xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="spellsschema.xsd"> 
<spell category="fire" cooldown="18" manacost="100"> 
    <name>Fire Breath</name> 
    <image id="FireBreath"/> 
    <discription>Some text</discription> 
    <category>Fire</category> 
    <cooldown>18</cooldown> 
    <manacost>100</manacost> 
</spell> 
</spells> 

这是XML模式:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="spells.xml" elementFormDefault="qualified"> 
<xs:element name="spells"> 
<xs:complexType> 
    <xs:sequence> 
     <xs:element name="spell"> 
     <xs:attribute name="category" use="required"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
        <xs:pattern value="fire|water|air|earth"/> 
       </xs:restriction> 
      </xs:simpleType> 
     </xs:attribute> 
     <xs:attribute name="cooldown" type="xs:duration" use="required"/> 
     <xs:attribute name="manacost" type="xs:decimal" use="required"/> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="name" type="xs:string"/> 
        <xs:element name="image"/> 
        <xs:element name="discription" type="xs:string"/> 
        <xs:element name="category" type="xs:string"/> 
        <xs:element name="cooldown" type="xs:duration"/> 
        <xs:element name="manacost" type="xs:decimal"/> 
       </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
</xs:complexType> 
</xs:element> 
</xs:schema> 

这是XSL:

<?xml version="1.0" encoding="utf-8"?> 
<!-- DWXMLSource="spells.xml" --> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
    <xsl:for-each select="spells/spell"> 
    <xsl:value-of select="discription"/> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet>  

的事情是,当我APLY我的XSL文件,XML停止显示在所有。我认为我的XML Schema也无法正常工作。我的限制也被忽略。
我该怎么办?

+0

在xml中你有'xmlns =“spels.xml”',在xsd中你有'targetNamespace =“spells.xml”'。这是一个错字吗? –

+0

是的。没关系。 – stroibot

回答

0

xsi:schemaLocation="spellsschema.xsd"是错的,你需要把一对名称空间uri和位置uri放在那里:xsi:schemaLocation="spells.xml spellsschema.xsd"

+0

我不明白。我已经像你说过的那样做了,但它仍然忽略了我的限制。我的意思是,当我删除属性''类别'''例如它仍然正常显示,但''类别''是必需的属性。或者当我更改属性''“冷却时间”('xs:duration'到''Hello World''')时,它仍然正常工作。 – stroibot

+0

好吧,您需要详细解释如何使用模式来验证XML,也就是您使用哪种验证解析器或编辑器,因为基于模式的验证根本不受许多XML解析器的支持,当然不是由那些例如,浏览器中使用的XML解析器。当然,名称空间名称中的拼写错误很重要,只要XML实例和模式不具有相同的名称空间URI,任何验证解析器都无法检查模式。因此,首先编辑您的问题并纠正问题,并告诉我们您使用哪种软件进行验证。 –