1

I need help with XSL and XML Schema.
This is XML file:

<?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>

This is XML Schema:

<?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>

This is 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>    

The thing is when I aply my XSL file, XML stop showing at all. And I think my XML Schema is not working correctly either. My restrictions are also ignored.
What do I do?

stroibot
  • 808
  • 1
  • 11
  • 25

1 Answers1

0

xsi:schemaLocation="spellsschema.xsd" is wrong, you need to put a pair of namespace uri and location uri in there: xsi:schemaLocation="spells.xml spellsschema.xsd".

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • I don't get it. I've made this like you said but it still ignore my restrictions. What I mean is when I remove attribute `"category"` for example it still showing normaly, but `"category"` is required attribute. Or when I change attribute `"cooldown"` wich is `xs:duration` to `"Hello World"` it still working normaly. – stroibot May 09 '16 at 08:20
  • Well, you will need to explain to use in detail how you try to validate your XML against the schema, i.e. which validating parser or editor you use, as schema based validation is not supported by many XML parsers at all, certainly not by those XML parsers used in browsers for instance. And certainly that typo in the namespace name matters, as long as the XML instance and the schema do not have the same namespace URI any validating parser will certainly have no reason to check the schema. So start by editing your question and correct the problems and tell us which software you use to validate. – Martin Honnen May 09 '16 at 08:46