www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] XSD and XSLT clash

  1. #1
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,745

    resolved [RESOLVED] XSD and XSLT clash

    I want a XSL stylesheet for my XML file. I also want to write an XML Schema instead of relying on DTDs. However, when I try to give my XML file a namespace, I get a wall of text when I try to view the file through the stylesheet. What am I doing wrong?

  2. #2
    Join Date
    May 2008
    Posts
    381
    My first thought is namespace issues (you're not qualifying the elements in the XSLT document with the XML namespace). It's rather common actually, and it's easily avoided but hard to discover at first. You can find a set of three documents attached in a ZIP file. They show how XSLT, XSD and XML can all be tied together.
    Attached Files Attached Files

  3. #3
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,745
    Thanks for your advice. I got the XSL file working, but could you help me with the XSD? I'll post the old Doctype.

    HTML Code:
    <!DOCTYPE links [
    	<!ELEMENT links	(link+)>
    	<!ATTLIST links
    		xmlns		CDATA #FIXED "http://Localhost"
    		xmlns:xsi	CDATA #FIXED "http://www.w3.org/2001/XMLSchema-instance"
    		xsi:schemaLocation CDATA #FIXED "http://Localhost ./linksmarkup.xsd"
    	>
    	<!ELEMENT link	(name, url, kwds, secs?)>
    	<!ELEMENT name	(#PCDATA)>
    	<!ELEMENT url	(#PCDATA)>
    	<!ELEMENT kwds	(kwd+)>
    	<!ELEMENT kwd	(#PCDATA)>
    	<!ELEMENT secs	(sec+)>
    	<!ELEMENT sec	(kword, s)>
    	<!ELEMENT kword	(#PCDATA)>
    	<!ELEMENT s	(#PCDATA)>
    	
    	<!ENTITY mdash	"—">
    	<!ENTITY amp2	"&amp;amp;">
    ]>
    Here's what I have so far for XSD:

    HTML Code:
    <?xml version="1.0"?>
    <xs:schema
    	version="1.0"
    	xmlns:xs="http://www.w3.org/2001/XMLSchema"
    	xmlns="http://LocalHost"
    	targetNamespace="http://LocalHost"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	elementFormDefault="qualified"
    >
    	<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
    	<xs:element name="links">
    		<xs:complexType>
    			<!--  This element can occur any number of times -->
    			<xs:element name="link">
    				<xs:complexType>
    					<xs:sequence>
    						<xs:element name="name" type="xs:string" />
    						<xs:element name="url" type="xs:string" />
    						<xs:element name="kwds">
    							<xs:complexType>
    								<!--  This element can occur any number of times -->
    								<xs:element name="kwd" type="xs:string" />
    							</xs:complexType>
    						</xs:element>
    						<!--  This element MAY or MAY NOT occur -->
    						<xs:element name="secs">
    							<xs:complexType>
    								<!--  This element can occur any number of times -->
    								<xs:element name="sec">
    									<xs:complexType>
    										<xs:sequence>
    											<xs:element name="kword" type="xs:string" />
    											<xs:element name="s" type="xs:string" />
    										</xs:sequence>
    									</xs:complexType>
    								</xs:element>
    							</xs:complexType>
    						</xs:element>
    					</xs:sequence>
    				</xs:complexType>
    			</xs:element>
    		</xs:complexType>
    	</xs:element>
    </xs:schema>
    What I don't know how to handle is elements that loop and elements that might or might not appear.
    Last edited by Mr Initial Man; 02-22-2009 at 12:25 PM.

  4. #4
    Join Date
    May 2008
    Posts
    381
    On most things (xs:element and xs:group), the minOccurs and maxOccurs attributes work well. For example:
    Code:
    			<!--  This element can occur any number of times -->
    			<xs:element name="link" minOccurs="1" maxOccurs="unbounded">
    				<xs:complexType>
    That matches your DTD since it says the link element can occur once or more when it is a child element of the links element. If link could occur any number of times (or not at all), you would use minOccurs="0".

    If you have any more questions, feel free to continue posting. ^_^

    Also be aware that things like the minOccurs attribute in a schema would be declared as the following in a DTD:
    Code:
    <!ATTLIST ...
        minOccurs CDATA #IMPLIED
        ...>
    And in a schema:
    Code:
    <xs:attribute name="minOccurs" type="xs:nonNegativeInteger"/>
    In other words, there are more data types available for you to use. I'd recommend taking advantage of them when possible.

  5. #5
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,745
    Okay, does this match what the DocType says?

    HTML Code:
    <?xml version="1.0"?>
    <xs:schema
    	version="1.0"
    	xmlns:xs="http://www.w3.org/2001/XMLSchema"
    	xmlns="http://LocalHost"
    	targetNamespace="http://LocalHost"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	elementFormDefault="qualified"
    >
    	<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
    	<xs:element name="links">
    		<xs:complexType>
    			<xs:attribute name="xmlns">
    				<xs:simpleType>
    					<xs:restriction base="xs:string">
    						<xs:pattern value="http://Localhost" />
    					</xs:restriction>
    				</xs:simpleType>
    			</xs:attribute>
    			<xs:attribute name="xmlns:xsi">
    				<xs:simpleType>
    					<xs:restriction base="xs:string">
    						<xs:pattern value="http://www.w3.org/2001/XMLSchema-instance" />
    					</xs:restriction>
    				</xs:simpleType>
    			</xs:attribute>
    			<xs:attribute name="xsi:schemaLocation">
    				<xs:simpleType>
    					<xs:restriction base="xs:string">
    						<xs:pattern value="http://Localhost ./linksmarkup.xsd" />
    					</xs:restriction>
    				</xs:simpleType>
    			</xs:attribute>
    			<xs:sequence>
    				<!--  This element can occur any number of times -->
    				<xs:element name="link" minOccurs="1" maxOccurs="unbounded">
    					<xs:complexType>
    						<xs:sequence>
    							<xs:element name="name" type="xs:string" />
    							<xs:element name="url" type="xs:string" />
    							<xs:element name="kwds">
    								<xs:complexType>
    									<!--  This element can occur any number of times -->
    									<xs:element name="kwd" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    								</xs:complexType>
    							</xs:element>
    							<!--  This element may or may not occur -->
    							<xs:element name="secs" minOccurs="0" maxOccurs="1">
    								<xs:complexType>
    									<!--  This element can occur any number of times -->
    									<xs:element name="sec" minOccurs="1" maxOccurs="unbounded">
    										<xs:complexType>
    											<xs:sequence>
    												<xs:element name="kword" type="xs:string" />
    												<xs:element name="s" type="xs:string" />
    											</xs:sequence>
    										</xs:complexType>
    									</xs:element>
    								</xs:complexType>
    							</xs:element>
    						</xs:sequence>
    					</xs:complexType>
    				</xs:element>
    			</xs:sequence>
    		</xs:complexType>
    	</xs:element>
    </xs:schema>

  6. #6
    Join Date
    May 2008
    Posts
    381
    Pretty close. Here are a few things to note:
    • Attributes must be declared after sequences, choices, etc.
    • You forgot xs:sequence in a few spots.
    • You don't need to declare xmlns or xmlns:whatever attributes on your own (declaring namespaces is a natural part of using XML).
    • You can use the xs:anyURI type for URLs rather than xs:string
    • xsi:schemaLocation is already defined in the xsi namespace, so you should use the ref attribute instead of the name attribute. This also means you don't need to use the type attribute.
    • With xsi:schemaLocation, you just need to use the fixed attribute, much like "#FIXED" in a DTD:
      Code:
      <xs:attribute ref="xsi:schemaLocation" fixed="http://Localhost ./linksmarkup.xsd"/>
    • You'll still need a DTD if you want to use named character references:
      Code:
      <!DOCTYPE links [
        <!ENTITY mdash "—">
        <!ENTITY amp2 "&amp;amp;">
      ]>

  7. #7
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,745
    How's this:

    HTML Code:
    <?xml version="1.0"?>
    <xs:schema
    	version="1.0"
    	xmlns:xs="http://www.w3.org/2001/XMLSchema"
    	xmlns="http://LocalHost"
    	targetNamespace="http://LocalHost"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	elementFormDefault="qualified"
    >
    	<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"/>
    	<xs:element name="links">
    		<xs:complexType>
    			<xs:sequence>
    				<!--  This element can occur any number of times -->
    				<xs:element name="link" minOccurs="1" maxOccurs="unbounded">
    					<xs:complexType>
    						<xs:sequence>
    							<xs:element name="name" type="xs:string" />
    							<xs:element name="url" type="xs:anyURI" />
    							<xs:element name="kwds">
    								<xs:complexType>
    									<xs:sequence>
    										<xs:element name="kwd" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
    									</xs:sequence>
    								</xs:complexType>
    							</xs:element>
    							<!--  This element may or may not occur -->
    							<xs:element name="secs" minOccurs="0" maxOccurs="1">
    								<xs:complexType>
    									<xs:sequence>
    										<xs:element name="sec" minOccurs="1" maxOccurs="unbounded">
    											<xs:complexType>
    												<xs:sequence>
    													<xs:element name="kword" type="xs:string" />
    													<xs:element name="s" type="xs:string" />
    												</xs:sequence>
    											</xs:complexType>
    										</xs:element>
    									</xs:sequence>
    								</xs:complexType>
    							</xs:element>
    						</xs:sequence>
    					</xs:complexType>
    				</xs:element>
    			</xs:sequence>
    		</xs:complexType>
    	</xs:element>
    </xs:schema>
    Update: I'm still getting an error:

    Code:
    TYPE 		LOC 		MESSAGE
    Validation 	29, 2 		cvc-elt.1: Cannot find the declaration of element 'links'.
    Last edited by Mr Initial Man; 02-23-2009 at 01:03 AM.

  8. #8
    Join Date
    May 2008
    Posts
    381
    Make sure your namespace in your instance document matches the targetNamespace in your schema. I noticed in your schema it is "http://LocalHost", but in your DTD, it is typed as "http://Localhost". Other than that, make sure your schema is well-formed and valid. At a quick glance, it appears to be well-formed and valid, but you might have an error somewhere that I didn't notice.

    I don't know of anything else that might cause problems yet.

  9. #9
    Join Date
    Sep 2004
    Location
    At the corner of WALK and DONT WALK
    Posts
    1,745
    That was indeed the error.


    I really want to thank you, because now I can feel more confident in my website redesign (which is gonna have more hidden geekery than you can shake a stick at ^^).
    Last edited by Mr Initial Man; 02-23-2009 at 03:48 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles