locked
How create XML from XML using XSL ? RRS feed

  • Question

  • hi friends,

    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

    Alex

    Monday, August 10, 2009 2:06 PM

Answers

  • <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>

    MVP XML My blog
    • Marked as answer by Alex Q8 Monday, August 10, 2009 4:33 PM
    Monday, August 10, 2009 2:56 PM
  • You need to adapt the stylesheet to deal with the namespace:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.Test.com/schemas/2005/06/messages"
    xmlns:ms="http://www.Test.com/schemas/2005/06/messages"
    exclude-result-prefixes="ms">
    
    <xsl:template match="/ms:Address">
    <Address>
          <FirstName><xsl:value-of select="ms:name" /></FirstName>
          <LastName><xsl:value-of select="ms:lastname" /></LastName>
    </Address>
    </xsl:template>
    
    </xsl:stylesheet>

    MVP XML My blog
    • Marked as answer by Alex Q8 Monday, August 10, 2009 4:33 PM
    Monday, August 10, 2009 4:20 PM

All replies

  • <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>

    MVP XML My blog
    • Marked as answer by Alex Q8 Monday, August 10, 2009 4:33 PM
    Monday, August 10, 2009 2:56 PM
  • Hi its working fine...

    But i face another problem , when i adopt xmlns, its comes error

    <Address xmlns=http://www.Test.com/schemas/2005/06/messages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.Test.com/schemas/2005/06/messages address.xsd" TotalList="3">
      <name> Alex</name>
      <lastname>Mathew</lastname>
    </Address>


    this is the asp code i use..

    <%
    '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.ContentType = "text/plain; charset=UTF-8"
     Dim doc  
     Set doc = Server.CreateObject("Msxml2.DOMDocument.3.0")  
     doc.async = False 
     doc.loadXML(xml.transformNode(xsl))  
     doc.setProperty "SelectionLanguage", "XPath" 
     doc.setProperty "SelectionNamespaces", "xmlns:df='http://www.Test.com/schemas/2005/06/messages'" 

    response.write doc.getElementsByTagName("Address").item(0).getElementsByTagName("FirstName").item(0.text
     %>

    error is
    Object required: 'getElementsByTagName(...).item(...)'

    Monday, August 10, 2009 3:51 PM
  • You need to adapt the stylesheet to deal with the namespace:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.Test.com/schemas/2005/06/messages"
    xmlns:ms="http://www.Test.com/schemas/2005/06/messages"
    exclude-result-prefixes="ms">
    
    <xsl:template match="/ms:Address">
    <Address>
          <FirstName><xsl:value-of select="ms:name" /></FirstName>
          <LastName><xsl:value-of select="ms:lastname" /></LastName>
    </Address>
    </xsl:template>
    
    </xsl:stylesheet>

    MVP XML My blog
    • Marked as answer by Alex Q8 Monday, August 10, 2009 4:33 PM
    Monday, August 10, 2009 4:20 PM