0

When selecting a value for the "base" attribute in the category element, I'd like this value to automatically be inserted in the category element as plain text. For example, in the following code, I'd like the two category elements to include the values Video and Alarms, respectively.

    <metadata>
    <category base="Video"/>
    <category base="Alarms"/>
    </metadata>

What XPath expression allows me to do this? The values for the base attribute are predefined in a separate XML file. I'm using Oxygen version 15.1.

Thanks

I apologize for the confusion. This is my source code:

    <metadata>
    <category base="Video"/>
    <category base="Alarms"/>
    </metadata>

Through a custom XPath action, I'd like source code to resemble this:

  <metadata>
  <category base="Video">Video</category>
  <category base="Alarms">Alarms</category>
  </metadata>

So, I'd like the element to take the value that is assigned to the "base" attribute. Oxygen comes bundled with custom XPath actions that allow you to do this sort of stuff. For example, through a custom action I can automatically populate an element with a specific string of text. However, in the above example, I don't want the category element to take a predefined string value, I want it to automatically take the value given to the "base" attribute. However, if I can't use XPath transform the document as has been stated, then I'm barking up the wrong tree!

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
fabdf
  • 3
  • 3
  • XPath is for searching, it cannot change the document. – choroba Apr 02 '14 at 18:00
  • It is not clear what you are asking. Is the XML an example of your input or your output? If you have a separate XML file, how is that supposed to be referenced? Can you post (and identify) what your XML looks like(input) and what you want to produce(output), and any other reference documents (with descriptions of how they relate or should be used). Also, it is likely that you need XSLT or XQuery to transform/modify your document. XPath cannot modify your document. – Mads Hansen Apr 03 '14 at 01:51
  • I added more information to my original posting. – fabdf Apr 03 '14 at 13:24

1 Answers1

0

You cannot change the xml document with XPath. You can find values of nodes you need with XPath and then create new xml document of same structure (or modify the existing one), that contains values you found. There are numerous ways to achieve that, for example via most of programing languages or xml transformations (XSLT or with XQuery queries).

You can find plenty of examples on the internet where you can find how to use XPath to find nodes you're interested in and then modify them in f.e. Java (found here):

myNodeList = (NodeList) xpath.compile("//MyXPath/text()")
       .evaluate(myXmlDoc, XPathConstants.NODESET);
myNodeList.item(0).setNodeValue("Hi mom!");
Community
  • 1
  • 1
mareckmareck
  • 1,560
  • 13
  • 18