www.webdeveloper.com
Results 1 to 7 of 7

Thread: Is this XML invalid?

  1. #1
    Join Date
    Nov 2008
    Posts
    16

    Is this XML invalid?

    XML Notepad points out that the following header is invalid, can you give me more specific guidance?
    The XML Validation service at http://xmlvalidation.com/index.php?id=1&L=0 does not seem to confirm this error, so I am lost.

    <?xml version="1.0" encoding="utf-8"?><Employees><Employee><ID>1</ID><FirstName>David</FirstName><LastName>Smith</LastName><Salary>10000</Salary></Employee><Employee><ID>3</ID><FirstName>Mark</FirstName><LastName>Drinkwater</LastName><Salary>30000</Salary></Employee><Employee><ID>4</ID><FirstName>Norah</FirstName><LastName>Miller</LastName><Salary>20000</Salary></Employee><Employee><ID>12</ID><FirstName>Cecil</FirstName><LastName>Walker</LastName><Salary>120000</Salary></Employee></Employees>

    Invalid header
    <?xml version="1.0" encoding="utf-8"?>
    <ErrorList />

    References
    1. XML Notepad - http://www.microsoft.com/en-us/downl...s.aspx?id=7973
    2. XML Validation service - http://xmlvalidation.com/index.php?id=1&L=0

  2. #2
    Join Date
    May 2014
    Posts
    893
    Welcome to why I don't trust IDE's or specialized 'document specific' editors. You have valid XML document for 'internal' use, but not as an actual "document". It is most likely complaining that you have gone from your prologue directly to elements without a doctype declaring the DTD.

    It doesn't know what employees, employee, id, firstname, lastname or any of the rest of your markup is, because you never declared them, and I'm pretty sure XML notepad expects those.

    Might also help if you had actual formatting in there, since the only reason to use XML instead of a real data format is to be human legible. (no matter how many xml nutters wildly claim it's a "machine readable" format in their ignorance of what "machine readable" means)

    See if this works:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE Employees [
    	<!ELEMENT Employee (ID, FirstName, LastName, Salary)>
    	<!ELEMENT ID (#PCDATA)>
    	<!ELEMENT FirstName (#PCDATA)>
    	<!ELEMENT LastName (#PCDATA)>
    	<!ELEMENT Salary (#PCDATA)>
    ]>
    <Employees>
    	<Employee>
    		<ID>1</ID>
    		<FirstName>David</FirstName>
    		<LastName>Smith</LastName>
    		<Salary>10000</Salary>
    	</Employee><Employee>
    		<ID>3</ID>
    		<FirstName>Mark</FirstName>
    		<LastName>Drinkwater</LastName>
    		<Salary>30000</Salary>
    	</Employee><Employee>
    		<ID>4</ID>
    		<FirstName>Norah</FirstName>
    		<LastName>Miller</LastName>
    		<Salary>20000</Salary>
    	</Employee><Employee>
    		<ID>12</ID>
    		<FirstName>Cecil</FirstName>
    		<LastName>Walker</LastName>
    		<Salary>120000</Salary>
    	</Employee>
    </Employees>
    If it does not, then I'd say that "XML Notepad" is a bunch of BS... but then I have a suspicious mind thanks to decades of seeing the same nonsense over and over -- so I label a lot of things people use and see nothing wrong with as "BS".

    But then there's a reason I ditched XML and JSON and went back to using ASCII control characters.... which would look something like this
    Code:
    #29;Employee
    #01;ID#31;FirstName#31;LastName#31;Salary#02;
    1#31;David#31;Smith#31;10000#30;
    3#31;Mark#31;Drinkwater#31;30000#30;
    4#31;Norah#31;Miller#31;20000#30;
    12#31;Cecil#31;Walker#31;120000#30;
    #03;#04;
    Naturally it would actually lack the linefeeds, and the #xx; would be the actual ascii characters. I just put those in for clarity
    Last edited by deathshadow; 06-19-2014 at 12:11 AM.
    Java is to JavaScript as Ham is to Hamburger.

  3. #3
    Join Date
    Nov 2008
    Posts
    16
    So would you provide an example of how to create an XSLT template on through an XML template that is not only human-readable but also valid XML according to the template?

    Hyperlinks to tutorials would help, not that I do not know how to Google of course, perhaps you have some solution specifically in mind.

  4. #4
    Join Date
    May 2014
    Posts
    893
    I've never really used XSLT, but I'll take a stab at it if I have time later... BUT

    Something else occurred to me about your XML. You've got uppercase in your tags; that is invalid as XML tags can only be lowercase. That's why it's also considered bad practice to declare HTML 4/newer tags in uppercase...

    Though I just installed XML notepad, and it *****es about their BEING a DTD, so that's even LESS useful a tool; but to be frank, That's why I just use a flat text editor, not fancy editors like that one.

    In any case this:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE employees [
    	<!ELEMENT employee (id, firstname, lastname, salary)>
    	<!ELEMENT id (#PCDATA)>
    	<!ELEMENT firstname (#PCDATA)>
    	<!ELEMENT lastname (#PCDATA)>
    	<!ELEMENT salary (#PCDATA)>
    ]>
    <employees>
    	<employee>
    		<id>1</id>
    		<firstname>David</firstname>
    		<lastname>Smith</lastname>
    		<salary>10000</salary>
    	</employee><employee>
    		<id>3</id>
    		<firstname>Mark</firstname>
    		<lastname>Drinkwater</lastname>
    		<salary>30000</salary>
    	</employee><employee>
    		<id>4</id>
    		<firstname>Norah</firstname>
    		<lastname>Miller</lastname>
    		<salary>20000</salary>
    	</employee><employee>
    		<id>12</id>
    		<firstname>Cecil</firstname>
    		<lastname>Walker</lastname>
    		<salary>120000</salary>
    	</employee>
    </employees>
    Appears to be valid according to XML notepad AND validators... I'm a little surprised that alleged validator you were using wasn't reporting errors because of the uppercase tags, as that most certainly was NOT valid XML.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    May 2014
    Posts
    893
    Ok, made a working XSLT of that as an example.... the core data document is easy, you just add the line:

    <?xml-stylesheet type="text/xsl" href="test.xsl"?>

    After the XML prologue so that the data knows what to be styled, which results in our text.xml looking like this:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
    <!DOCTYPE employees [
    	<!ELEMENT employee (id, firstname, lastname, salary)>
    	<!ELEMENT id (#PCDATA)>
    	<!ELEMENT firstname (#PCDATA)>
    	<!ELEMENT lastname (#PCDATA)>
    	<!ELEMENT salary (#PCDATA)>
    ]>
    <employees>
    	<employee>
    		<id>1</id>
    		<firstname>David</firstname>
    		<lastname>Smith</lastname>
    		<salary>10000</salary>
    	</employee><employee>
    		<id>3</id>
    		<firstname>Mark</firstname>
    		<lastname>Drinkwater</lastname>
    		<salary>30000</salary>
    	</employee><employee>
    		<id>4</id>
    		<firstname>Norah</firstname>
    		<lastname>Miller</lastname>
    		<salary>20000</salary>
    	</employee><employee>
    		<id>12</id>
    		<firstname>Cecil</firstname>
    		<lastname>Walker</lastname>
    		<salary>120000</salary>
    	</employee>
    </employees>
    The test.xsl file just gets a XML prologue, followed by declaring that it's a version 1 stylesheet linking to it's namespace, then matching all root level elements to start the markup.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    	version="1.0"
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    <xsl:template match="/">
    From there it's pretty much standard XHTML for a page until we want to loop the elements. That takes a xsl:for-each pointing to the element we want to loop, in this case "employees/employee" -- we then use xsl:value-of select="tagname" to isolate each child tag for output in our markup, then close out the xsl:for-each.... which looks something like this:

    Code:
    		<xsl:for-each select="employees/employee">
    			<tr>
    				<th scope="row"><xsl:value-of select="id" /></th>
    				<td><xsl:value-of select="firstname" /></td>
    				<td><xsl:value-of select="lastname" /></td>
    				<td><xsl:value-of select="salary" /></td>
    			</tr>
    		</xsl:for-each>
    So the entire test.xsl would read:

    Code:
    <?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
    	xmlns="http://www.w3.org/1999/xhtml"
    	lang="en"
    	xml:lang="en"
    ><head>
    
    <meta
    	http-equiv="Content-Type"
    	content="text/html; charset=utf-8"
    />
    
    <meta
    	http-equiv="Content-Language"
    	content="en"
    />
    
    <meta
    	name="viewport"
    	content="width=device-width; height=device-height; initial-scale=1.0"
    />
    
    <link
    	type="text/css"
    	rel="stylesheet"
    	href="screen.css"
    	media="screen,projection,tv"
    />
    
    <title>
    	XSLT Salary Demo
    </title>
    
    </head><body>
    
    <table id="salaries">
    	<caption>Salaries</caption>
    	<thead>
    		<tr>
    			<th scope="col">ID</th>
    			<th scope="col">First Name</th>
    			<th scope="col">Last Name</th>
    			<th scope="col">Salary Name</th>
    		</tr>
    	</thead><tbody>
    		<xsl:for-each select="employees/employee">
    			<tr>
    				<th scope="row"><xsl:value-of select="id" /></th>
    				<td><xsl:value-of select="firstname" /></td>
    				<td><xsl:value-of select="lastname" /></td>
    				<td><xsl:value-of select="salary" /></td>
    			</tr>
    		</xsl:for-each>
    	</tbody>
    </table>
    
    </body></html>
    </xsl:template>
    </xsl:stylesheet>
    I put a live demo up here to show that in action.
    http://www.cutcodedown.com/for_others/jon80/test.xml

    As with all my examples the directory is unlocked for easy access too all the gooey bits and pieces.
    http://www.cutcodedown.com/for_others/jon80

    I put a .txt copies of the XML and XSL files for easy online viewing, and gave it some CSS to show how easily the semantic markup can be formatted/targeted by CSS when you use the proper semantic markup for a table.

    Any questions, fire away.
    Java is to JavaScript as Ham is to Hamburger.

  6. #6
    Join Date
    Nov 2008
    Posts
    16
    Thanks

  7. #7
    Join Date
    Jun 2014
    Location
    San Antonio
    Posts
    5
    This XML document is invalid, likely due to invalid characters. XML error: Invalid document end at line 194, column 1RSS Error.

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