When I add xsd file with xml I cannot access xsl file - html

I have a xml file, I can access the data with xsl file. But when I add xsd to my xml file, I cannot use my xsl file. Please help me
<updateClient
xsi:schemaLocation=" file://updateClient.xsd"
xmlns="http://"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fileHeader>
<messageType version="100">
<recordTp>722</recordTp>
</messageType>
<sendingCompany>some</sendingCompany>
<processDate>2018-02-13</processDate>
<recordCount>10</recordCount>
</fileHeader>
</updateClient>
My XSL file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://localhost" exclude-result-prefixes="x" >
<xsl:template match="/">
<html>
<body>
<h2>My Collection</h2>
< table>
<tr>
<xsl:for-each select="updateClient/fileHeader/messageType">
<td> <p>RecordType :<xsl:value-of select="recordTp" /></p> </td>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Your input XML is located in the http:// namespace. Your XPath expressions in the XSL file have no namespace prefix, so they are looking for elements in the default namespace.
If you can use XPath 2.0, you can use the ´ xpath-default=namespace´ attribute, i.e.:
<xsl:stylesheet version="2.0" xpath-default-namespace="http://" ...>
In XPath 1.0 you could do something like this:
<xsl:for-each select="/ns1:updateClient/ns1:fileHeader/ns1:messageType" xmlns:ns1="http://">
Or, you could remove the namespace declaration in your input XML and use xsi:noNamespaceSchemaLocation="file://updateClient.xsd" instead of xsi:schemaLocation.

Related

How do I fix colon problems in XML Conversion using XSL

For some reason, colons in the tags of my XML files seem to be causing problems when I'm trying to render it in HTML format using XSL. I think it's the im: portion of the tag name, so are special characters just not allowed? Below is my xml.
<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type="text/xsl" href="formatting.xsl" xmlns:im="im"?>
<im:Command xmlns:im="im">
<im:CommandHdr>
<im:Name>John</im:Name>
<im:DisplayName>Jo</im:DisplayName>
</im:CommandHdr>
</im:Command>
Here is my XSL.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="im:Command/im:CommandHdr">
<tr>
<td><xsl:value-of select="im:Name"/></td>
<td><xsl:value-of select="im:DisplayName"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
How do I fix this problem.

XML to XSL transformation

I am new to XML. I am trying to convert the following XMl file
<?xml version="1.0"?>
<parent original_id="OI123" id="I123">
<custompanel panel="cp"></custompanel>
</parent>
into the following HTML
<html>
<body><div xmlAttribute="{"original-id":"OI123","id":"I123"}">
<p xmlAttribute={"panel":"cp"}/>
</div>
</body>
</html>
XML tag <parent> should be converted to <div> and <custompanel> should be converted to <p> tag.
I have read the XSTL documentation from W3CSchool but still I am not exactly sure how to approach the problem.Can anyone help me with it?
The custom attribute needs to be stored in xmlAttribute as JSONObject.
After a quick research of the correct syntax I came up with this.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
indent="yes"
encoding="utf-8" />
<xsl:template match="parent">
<html>
<body>
<div xmlAttribute="{{'original-id':'{#original_id}','id':'{#id}'}}">
<xsl:apply-templates />
</div>
</body>
</html>
</xsl:template>
<xsl:template match="custompanel">
<p xmlAttribute="{{'panel':'{#panel}'}}" />
</xsl:template>
</xsl:stylesheet>
The tricky part is espacing the {} for the JSON, which we build ourselves. You need two curly braces {{ to have a literal one. Also you need to use single quotes ' inside the attributes as double quotes would be escaped to ". You can access attributes with the #foo selector, but now you need to use actual {} to make the processor recognize it should do something.
I guess that your actual file has more than one <parent>. In that case you need to have a root element around it, and you need to adjust the XSLT. Add another <xsl:template match="/"> and move the HTML frame there.

XML to XSL Error loading stylesheet: Parsing an XSLT stylesheet failed

I'm getting this error when trying to convert a XML document with XSL. I've only just started coding with XML, so I am sure I am missing something simple, but cant seem to find much information about it online.
Booklist.XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="booklist.xsl"?>
<CATALOG>
<BOOK>
<NAME>
<TITLE>Caring for Sheep</TITLE>
</NAME>
<PAGES> 60 </PAGES>
<PRICE>$34.99</PRICE>
</BOOK>
</CATALOG>
And my XSL File:
Booklist.XSL
<?xml version="1.0"?>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name">;
</div>
</xsl:for-each>
<xsl:for-each select="CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
Can anyone see an error here?
You're sort of mixing the Literal Result Element as Stylesheet simplified syntax with the regular syntax by putting a literal result element <body> outside any template, yet not at the top level. If you want to use this simplified syntax, get rid of the <xsl:stylesheet> and <xsl:output> elements, so that <body> is at the top level.
Also, XML and XPath are case-sensitive with regard to element names, so you'll need to fix the inconsistency between e.g. "catalog" and <CATALOG>.
Well, if the for some reasons which are hard to figure out the simplified syntax doesn't work for you I would suggest using the "normal coding", which would be something like this in your case:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:template match="/">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="/CATALOG/BOOK/NAME">
<div style="background-color:teal;color:white;padding:4px">
<xsl:value-of select="name"/>
</div>
</xsl:for-each>
<xsl:for-each select="/CATALOG/BOOK">
<div style="background-color:teal;color:white;padding:4px">
</div>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>

How to get a value from a JavaScript in XSLT?

I have the following XML and XSLT to transform to HTML.
XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<te>t1</te>
</root>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="root">
<html>
<div>
<xsl:variable name="name1" select="te" />
**
<xsl:value-of select="CtrlList['$name1']" />
**
</div>
<script language="javascript">var List={
"t1":"test"
}</script>
</html>
</xsl:template>
</xsl:stylesheet>
So my objective is get the value of "te" from the XML and map it with the JavaScript object "List" and return the value test while transforming with the XSLT. So i should get the value test as output.
Can anyone figure out what wrong in the XSLT.
When you look at your XSLT, it may seem like there is JavaScript there, but all XSLT sees is that it is outputing an element named "script", with an attribute "language", which contains some text. It is also worth noting that xsl:value-of is used to get the value from the input document, but your script element is actually part of the result tree, and so not accessible to xsl:value-of.
However, it is possible to extend XSLT so it can use javascript functions, but this is very much processor dependant, and you should think of it the same way as embedding JavaScript in HTML. Have a look at this question, as an example
How to include javaScript file in xslt
So, in your case, your XSLT would be something like this (Note this particular example will only work in Mircorsofts MSXML processor)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://mycompany.com/mynamespace"
exclude-result-prefixes="msxsl user">
<xsl:output method="xml" indent="yes" />
<msxsl:script language="JScript" implements-prefix="user">
var List={
"t1":"test"
}
function lookup(key) {
return List[key];
}
</msxsl:script>
<xsl:template match="root">
<html>
<div>
<xsl:variable name="name1" select="te"/>
<xsl:value-of select="user:lookup(string($name1))"/>
</div>
</html>
</xsl:template>
</xsl:stylesheet>
Of course, it might be worth asking why you want to use javascript in your XSLT. It may be possible to achieve the same result using purely XSLT, which would certainly make you XSLT more portable.

How to parse the xml data into html?

Here is my xml:
<Catalog>
<catalogDetail catalogId="DemoCatalog">
<catalogName>Demo Catalog</catalogName>
</catalogDetail>
<catalogDetail catalogId="GoogleCatalog">
<catalogName>Google Catalog</catalogName>
</catalogDetail>
</Catalog>
I want it to be read in HTML file how can I do this???
To do this your HTML file should contain some JavaScript code, so you will want to learn how to parse XML in Javascript.
Here is a good StackOverflow question on this topic: XML parsing in JavaScript
You can do by using PHP's XML Library called simplexml for more information check this link
http://www.w3schools.com/php/php_xml_simplexml.asp
NOTE : If you can elaborate on what technology you're using, I'll try to provide a more complete example.
I would suggest using XSLT for this. With XSLT, you can pass in the XML fragment, parse it, and return formatted HTML.
Here's an example XSLT document that converts XML to HTML:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>