Link to home
Start Free TrialLog in
Avatar of da4thrza
da4thrza

asked on

Create Elements Using XPath

Hi,

Lets say i have an XML File:

<Books>
   <Book>
         <Author>Reza</Author>
         <Title>My Book</Title>
   </Book>
<Books>

I want to be able to programmatically add additional elements to the Book node, such as Publisher,  so that it looks like
           
<Books>
   <Book>
         <Author>Reza</Author>
         <Title>My Book</Title>
         <Publisher>My Publisher</Publisher>
   </Book>
<Books>  

I want to be able to pass in the XPath /Books/Book/Publisher and have it create this element for me automatically.

I would offer more points, but this is the last of them.

Thanks,
Reza
Avatar of Wayne Bradney
Wayne Bradney
Flag of United States of America image

Reza,

You don't mention _how_ you're trying to create the element, but if you're referring to XSLT there's no easy way to do this. XPath expressions can't be stored in XSLT variables (at least they can't be treated like XPath expressions that way).

If you're not talking about XSLT, then, sure you can. Load your document into a DOM Document. As long as your XPath expression is as simple as the one you give in your example, you should be able to break it down in code and step down into the DOM Document until you reach the right spot, and CreateElement right there. I'm not sure which language you're using, so I won't give an example, but it should be fairly straightforward. If you want to support any arbitrary XPath expression, however, this will require a lot of work!

Regards,
WMB
ASKER CERTIFIED SOLUTION
Avatar of devplayer04
devplayer04

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of da4thrza
da4thrza

ASKER

Wow, thats great, thanks alot!
Hi devplayer04,

I am having some trouble converting your code into c++, i know you already have the points but is there anyway you can convert this code to c++ for me? I am using c++ with msxml.

Thanks in advance
Reza