Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 12th, 2004, 02:30 AM
Registered User
 
Join Date: Sep 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default copy xml to xml (with java script)

Hello experts !
this is my first post in this msg forum so be gentle ...
im trying to find a way to copy xml structure from xml file to different xml structure .
the thing is that the the second xml the one i like to copy from i need it without the root tag.
tnx

 
Old September 12th, 2004, 05:03 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

The most likely solution sounds like using XSLT and the identity template. You will have to add a matching template for any nodes you want to modify or edit:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="node()|@*">

    <xsl:copy>

      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <!--
    Templates to match nodes that need changing go here
  -->
</xsl:stylesheet>
To remove the document element you must make sure that the new document is well formed or output as text. For example if your xml is:
Code:
<oldDocElement>
  <newDocElement>
    <data>Hello, World!</data>
  </newDocElement>
</oldDocElement>
then add a template body to process oldDocElement:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="node()|@*">

    <xsl:copy>

      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <!--
    Templates to match nodes that need changing go here
  -->
  <xsl:template match="oldDocElement">

    <xsl:apply-templates select="node()"/>  
  </xsl:template>
</xsl:stylesheet>
For details of how to use JavaScript to apply tranforms see the Ms xml 4 core services sdk at http://msdn.microsoft.com/library/de...l/xmmscXML.asp

or better still download the docs from http://msdn.com/xml.



--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 13th, 2004, 03:17 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

After posting I realised that although the identity template provides a good generic solution to your problem a more straightforward approach if you just want to copy everything except the document element would be:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:copy-of select="*/*"/>
  </xsl:template>
</xsl:stylesheet>


--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 14th, 2004, 05:52 AM
Registered User
 
Join Date: Sep 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tnx for the replay , i tried to look in to the msdn xml + js examples put with no real luck i also tried to get help from google but with no much luck.
can someone give some basic examples in js to me get the basic idea.
tnx

 
Old September 14th, 2004, 06:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I find it hard to believe you can't find any examples but here is some code to start you off, assuming your source file is oldXml.xml, the result file is newXml.xml and your transform is called removeDocElement.xsl and they are in the same folder as the following js file:
Code:
var _oDom = null;
function getSyncDom()
{
  if (!_oDom)
  {
    _oDom = new ActiveXObject("Msxml2.DomDocument.4.0");
    _oDom.async = false;
  }
  return _oDom.cloneNode(false);  
}

function doTransform(SourceDom, StyleDom)
{
  var oResultDom = getSyncDom(); 
  SourceDom.transformNodeToObject(StyleDom, oResultDom);
  return oResultDom;
}

function getErrorDescription(err)
{
  var sErrorMessage = "Error on line: " + err.line
                    + "\nat position: " + err.linePos
                    + "\nsource: " + err.srcText
                    + "\nreason: " + err.reason;
  return sErrorMessage;
} 

function main()
{
  var oSource = getSyncDom();
  var bSuccess = oSource.load("oldXml.xml");
  if (!bSuccess)
  {
    WScript.echo(getErrorDescription(oSource.parseError));
    return;
  }
  var oStyle = getSyncDom();
  bSuccess = oStyle.load("removeDocElement.xsl");
  if (!bSuccess)
  {
    WScript.echo(getErrorDescription(oStyle.parseError));
    return;
  }
  var oResult = doTransform(oSource, oStyle);
  oResult.save("newXml.xml");
  WScript.echo(oResult.xml);
}

main();
--

Joe (Co-author Beginning XML, 3rd edition)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Building a new XML > lost in copy-of-select and if akentanaka XSLT 2 June 30th, 2008 07:35 AM
java script to prevent users to copy image... ethantinder Javascript How-To 3 May 23rd, 2008 12:52 PM
How to connecting java script form to xml file ashraf Javascript How-To 1 December 10th, 2004 05:47 AM
about XML receive by tomcatand XML parse by java taianmhzy Servlets 0 May 20th, 2004 01:59 AM
copy xml-to-xml kay XSLT 4 November 19th, 2003 06:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.