ShaunWilde
asked on
How do I rename a node?
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
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
What language are you going to perform the conversion in? I don't think you can do this in XSL, but who knows...
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,new Name,paren tName) {
xmldom = new ActiveXObject("MICROSOFT.X MLDOM")
xmldom.load(zvon1.XMLDocum ent);
t_ptr = xmldom.selectSingleNode("/ /" + oldName).childNodes
xmldom.selectSingleNode("/ /" + parentName).removeChild(xm ldom.selec tSingleNod e("//" + oldName))
newNode = xmldom.createNode(1,newNam e,"")
xmldom.selectSingleNode("/ /" + parentName).appendChild(ne wNode)
for(i=0;i<t_ptr.length;++i ) {
xmldom.selectSingleNode("/ /" + parentName).childNodes(0). appendChil d(t_ptr(i) )
}
alert(xmldom.xml)
}
I fell this code must work and hope it will be useful to.
rgds
xav
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,new
xmldom = new ActiveXObject("MICROSOFT.X
xmldom.load(zvon1.XMLDocum
t_ptr = xmldom.selectSingleNode("/
xmldom.selectSingleNode("/
newNode = xmldom.createNode(1,newNam
xmldom.selectSingleNode("/
for(i=0;i<t_ptr.length;++i
xmldom.selectSingleNode("/
}
alert(xmldom.xml)
}
I fell this code must work and hope it will be useful to.
rgds
xav
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-template s/></first >
</xsl:template>
<xsl:template match="b">
<second><xsl:apply-templat es/></seco nd>
</xsl:template>
<xsl:template match="/ | @* | node()">
<xsl:copy><xsl:apply-templ ates select="@* | node()"/></xsl:copy>
</xsl:template>
</xsl: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-template
</xsl:template>
<xsl:template match="b">
<second><xsl:apply-templat
</xsl:template>
<xsl:template match="/ | @* | node()">
<xsl:copy><xsl:apply-templ
</xsl:template>
</xsl:stylesheet>
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,new Name,paren tName) {
xmldom = new ActiveXObject("MICROSOFT.X MLDOM")
xmldom.load(zvon1.XMLDocum ent);
t_ptr = xmldom.selectSingleNode("/ /" + oldName).childNodes
xmldom.selectSingleNode("/ /" + parentName).removeChild(xm ldom.selec tSingleNod e("//" + oldName))
newNode = xmldom.createNode(1,newNam e,"")
xmldom.selectSingleNode("/ /" + parentName).appendChild(ne wNode)
for(i=0;i<t_ptr.length;++i ) {
xmldom.selectSingleNode("/ /" + parentName).childNodes(0). appendChil d(t_ptr(i) )
}
alert(xmldom.xml)
}
I fell this code must work and hope it will be useful to.
rgds
xav
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,new
xmldom = new ActiveXObject("MICROSOFT.X
xmldom.load(zvon1.XMLDocum
t_ptr = xmldom.selectSingleNode("/
xmldom.selectSingleNode("/
newNode = xmldom.createNode(1,newNam
xmldom.selectSingleNode("/
for(i=0;i<t_ptr.length;++i
xmldom.selectSingleNode("/
}
alert(xmldom.xml)
}
I fell this code must work and hope it will be useful to.
rgds
xav
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
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
ASKER
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?
chabaud - I was working on someting similar - I'll check yours out - my XSLT is awful :( - will the atributes be preserved also?
ASKER
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-te mplates/>< /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
<xsl:template match="a">
<first><xsl:copy-of select="@*"/><xsl:apply-te
</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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I actually did it this way
<xsl:template match="a">
<first>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Name">x</xsl:attribu te>
<xsl:apply-templates/>
</first>
</xsl:template>
but the effect is the same
<xsl:template match="a">
<first>
<xsl:copy-of select="@*"/>
<xsl:attribute name="Name">x</xsl:attribu
<xsl:apply-templates/>
</first>
</xsl:template>
but the effect is the same
ASKER
thanks - could you possibly comment on some of the lines and what they do eg what @* means etc
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
@ 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
ASKER
thanks again - look out for more XSLT related questions from me - I am sure ther will be some