Solved

How do I rename a node?

Posted on 2001-07-17
12
317 Views
Last Modified: 2011-10-03
I have an XML document - lots of them - how do I use the XML DOM to rename nodes within the document or create a new document with the reanmes nodes

eg
<a>
  <b>
   <data>
   </data>
  </b>
</a>
<x>
</x>

becomes

<first>
  <second>
    <data>
    </data>
  </second>
</first>
<x>
</x>

I think I may need to do some XSL - can anyone help
0
Comment
Question by:ShaunWilde
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6290502
What language are you going to perform the conversion in?  I don't think you can do this in XSL, but who knows...
0
 

Expert Comment

by:xaviergeorge
ID: 6290518
hi Shaun,
Since node Name is a ready only property, the solution i have suggested is a bit of a round about way.

The function requires 3 parameters to make it generic.
viz. oldName,newName and parentName

What u do here is remove the <oldName> node after keeping a pointer to all its children.

Create a new node with the name <newName> and append it
to parent Node. Then to this node append all the childeren to which we had kept pointers earlier.


function changeNodeName(oldName,newName,parentName) {
          xmldom = new ActiveXObject("MICROSOFT.XMLDOM")
          xmldom.load(zvon1.XMLDocument);
         
          t_ptr = xmldom.selectSingleNode("//" + oldName).childNodes    
          xmldom.selectSingleNode("//" + parentName).removeChild(xmldom.selectSingleNode("//" + oldName))
         
          newNode = xmldom.createNode(1,newName,"")          
          xmldom.selectSingleNode("//" + parentName).appendChild(newNode)
         
          for(i=0;i<t_ptr.length;++i) {
               xmldom.selectSingleNode("//" + parentName).childNodes(0).appendChild(t_ptr(i))
          }
          alert(xmldom.xml)
     }

I fell this code must work and hope it will be useful to.

rgds
xav
0
 
LVL 4

Expert Comment

by:chabaud
ID: 6290623
Here is your XSLT stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:space="preserve">

<xsl:template match="a">
     <first><xsl:apply-templates/></first>
</xsl:template>

<xsl:template match="b">
     <second><xsl:apply-templates/></second>
</xsl:template>

<xsl:template match="/ | @* | node()">
     <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:xaviergeorge
ID: 6292311
hi Shaun,
Since node Name is a ready only property, the solution i have suggested is a bit of a round about way.

The function requires 3 parameters to make it generic.
viz. oldName,newName and parentName

What u do here is remove the <oldName> node after keeping a pointer to all its children.

Create a new node with the name <newName> and append it
to parent Node. Then to this node append all the childeren to which we had kept pointers earlier.


function changeNodeName(oldName,newName,parentName) {
          xmldom = new ActiveXObject("MICROSOFT.XMLDOM")
          xmldom.load(zvon1.XMLDocument);
         
          t_ptr = xmldom.selectSingleNode("//" + oldName).childNodes    
          xmldom.selectSingleNode("//" + parentName).removeChild(xmldom.selectSingleNode("//" + oldName))
         
          newNode = xmldom.createNode(1,newName,"")          
          xmldom.selectSingleNode("//" + parentName).appendChild(newNode)
         
          for(i=0;i<t_ptr.length;++i) {
               xmldom.selectSingleNode("//" + parentName).childNodes(0).appendChild(t_ptr(i))
          }
          alert(xmldom.xml)
     }

I fell this code must work and hope it will be useful to.

rgds
xav
0
 

Expert Comment

by:xaviergeorge
ID: 6292343
sorry guys, i first gave the answer y'day.
came today and refreshed the page and lo,
my answer gets posted again. Me real sorry for it, but to some extent i feel it is the fault of the way this site ( i am only reffering to this functionality)  is build.
When i submit it should go to a page wihich inserts into the db my commenst/answers, redirect to another page which is the view page. Now if i refresh, only the view generating page is refreshed, so  repeat d/b entry does not happen.
Hope the suggestion is take in the right spirit.
cheers
xav
0
 
LVL 9

Author Comment

by:ShaunWilde
ID: 6292690
xaviergeorge - that is why there is a Reload Question option at the top of the page - as I remember it will not let you post the same message multiple times within a certain time frame

chabaud - I was working on someting similar - I'll check yours out - my XSLT is awful :( - will the atributes be preserved also?
0
 
LVL 9

Author Comment

by:ShaunWilde
ID: 6293491
chabaud - it works - with some mods as it didn't preserve the attributes of the node so I used xsl:copy-of eg

<xsl:template match="a">
    <first><xsl:copy-of select="@*"/><xsl:apply-templates/></first>
</xsl:template>

just one extra (if you don't mind)

if I had

<a NLSname="A" ...>
  <b NLSname="B" ...>
  </b>
</a>

how do I change it to

<X NLSname="x" ...>
  <Y NLSname="y" ...>
  </Y>
</X>

eg <a> always has the NLSName == A and I wish to change it to <X> with NLSName = x etc



0
 
LVL 4

Accepted Solution

by:
chabaud earned 100 total points
ID: 6293551
Try something like this:

<xsl:template match="a">
   <first><xsl:attribute name="NLSname">x</xsl:attribute>
   <xsl:copy-of select="@* and not(@NLSname)"/><xsl:apply-templates/></first>
</xsl:template>
0
 
LVL 9

Author Comment

by:ShaunWilde
ID: 6297345
I actually did it this way

<xsl:template match="a">
    <first>
          <xsl:copy-of select="@*"/>
          <xsl:attribute name="Name">x</xsl:attribute>
          <xsl:apply-templates/>
     </first>
</xsl:template>

but the effect is the same
0
 
LVL 9

Author Comment

by:ShaunWilde
ID: 6297350
thanks - could you possibly comment on some of the lines and what they do  eg what @* means etc
0
 
LVL 4

Expert Comment

by:chabaud
ID: 6297465
For your information,

@   attribute prefix
@*  all attributes matches

For more example and documentation (from Microsoft):

Patterns:
---------
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmconpatterns.asp

XSL developers guide:
---------------------
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmconxsldevelopersguide.asp

XSLT reference:
---------------
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmrefxsltreference.asp

0
 
LVL 9

Author Comment

by:ShaunWilde
ID: 6297643
thanks again - look out for more XSLT related questions from me - I am sure ther will be some
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question