How create XML from XML using XSL ?
I try like this.. but i not get a result
Test.xml
Alex
<
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" ?>
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.
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>