How create XML from XML using XSL ?
I try like this.. but i not get a result
Test.xml
<Address>
<name> Alex</name>
<lastname>Mathew</lastname>
</Address>
Test.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="/">
<Address>
<FirstName><xsl:value-of select="name" /></FirstName>
<LastName><xsl:value-of select="lastname" /></LastName>
</Address>
</xsl:template>
</xsl:stylesheet>
I need out put like this
<Address>
<FirstName> Alex</FirstName>
<LastName>Mathew</LastName>
</Address>
I try to convert in my asp page (test.asp)
<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("Test.xml"))
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("Test.xsl"))
'Response.Write(xml.transformNode(xsl))
'Response.ContentType = "text/plain; charset=UTF-8"
Set doc = Server.CreateObject("Msxml2.DOMDocument.3.0")
doc.async = False
doc.loadXML(xml.transformNode(xsl))
response.write xml.transformNode(xsl)
response.write doc.getElementsByTagName("FirstName").item(0).text
%>
Plz help me solve this problem
You might also want to add an output directive in your stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Address">
<Address>
<FirstName><xsl:value-of select="name" /></FirstName>
<LastName><xsl:value-of select="lastname" /></LastName>
</Address>
</xsl:template>
</xsl:stylesheet>
This causes the output to have a leading xml declaration:
<?xml version="1.0" ?>
The problem is that "/"
is the root, not the root element (or "document element").
Hierarchically, "/"
is one level above the document element (<Address>
, in yor case). So this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Address">
<Address>
<FirstName><xsl:value-of select="name" /></FirstName>
<LastName><xsl:value-of select="lastname" /></LastName>
</Address>
</xsl:template>
</xsl:stylesheet>
would actually work. Note the tiny little difference? Nicer would be this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- the identity template (copies your input verbatim) -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<!-- special templates only for things that need them -->
<xsl:template match="name">
<FirstName><xsl:value-of select="." /></FirstName>
</xsl:template>
<xsl:template match="lastname">
<LastName><xsl:value-of select="." /></LastName>
</xsl:template>
</xsl:stylesheet>
Just to expand and clarify a bit on what Tomalak posted: the root of an XML document is, in the DOM hierarchy, above the top-level element. It's exceptionally common to see the two confused. Consider this XML document:
<!-- This is a node - yes, comments are nodes. -->
<root>
<child/>
</root>
<!-- This is also a node. -->
The root of this document has three child nodes: a comment node, an element node, and another comment node. The top-level element is named root
, because that's what everyone who creates XML instance documents does in order to perpetuate the confusion between the document root and the top-level element. (Especially if they're still at the point in their XML education where they use "node" when they mean "element.")
This gets us to one of the reasons that the template that Tomalak describes as "nicer" is nicer. If you extend the identity transform, the only thing that the XSLT will change in your document are the elements that you've built templates for. Every other node in the document is copied unchanged. So if your input document has comments around the top-level element, as in the above example, they won't get stripped out of the output, as they would if you simply implemented a template matching the Address
element.
Of course, if you want your output to exclude comments, that's easily accomplished too; just don't implement the identity transform.