Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create XML from XML using XSL?

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

like image 429
Alex Avatar asked Aug 10 '09 14:08

Alex


People also ask

How create XML from XSLT?

Execute an XSLT transformation from an XML fileOpen an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <?

How does XSL work with XML?

XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation . The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology perspective.

How do you transform one XML to another XML document using XSLT transform?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

Is XSL and XML same?

XSL (Extensible Stylesheet Language), formerly called Extensible Style Language, is a language for creating a style sheet that describes how data sent over the Web using the Extensible Markup Language (XML) is to be presented to the user.

What is the relationship between XML and XSL?

XSL is a language that can transform XML into XHTML, a language that can filter and sort XML data, a language that can define parts of a XML document, a language that can format XML data based on the data value, like displaying negative numbers in red, and a language that can output XML data to different devices, like ...


2 Answers

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" ?>
like image 148
mkoeller Avatar answered Oct 10 '22 23:10

mkoeller


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>
like image 40
Tomalak Avatar answered Oct 11 '22 00:10

Tomalak