I have an XML file, like this:
<area1>
<settings>
<setting name="to_include">value</setting>
</settings>
</area1>
<area2>
<settings>
<setting name="where_to_include">?????</setting>
</settings>
</area2>
I want to include the value of the setting node "to_include" as value of setting node "where_to_include". How could I do it, in a pure XML way? XInclude? XPointer? XPath? XLink? Other?
I hoped I could do something like this:
<area2>
<settings>
<setting name="where_to_include">
<xi:include href="SAMEFILE" xpointer="xpointer(/areas/area1/settings/setting[@name='debug'])">
<xi:fallback>FALLBACK VALUE</xi:fallback>
</setting>
</settings>
</area2>
But with XInclude I cannot refer to the document itself while parsing it.
I don't quite understand the question, but I think I have an idea what you mean. This may not be what you're after. You mention XPointer and XInclude, which seem like overkill to me.
Here's an XSLT (1.0) solution. The stylesheet takes an XML file like the one you give in the question and plugs
value
in for?????
. (Your XML is, by the way, missing a wrapper element, so it is technically malformed. But I think I understand what you mean. I'll silently addareas
as a document element, and that's how the stylesheet that follows works. You may need to tweak it to fit the XML you're actually working with.)Here is the stylesheet:
The stylesheet essentially just copies its input XML document, except at one point: when dealing with the
setting
underarea2
, it doesn't copy the text but goes back to thearea1
node and copies the text under itssetting
. You can confirm that this works using any XSLT processor, e.g., xsltproc (it sounds like you have some non-trivial XML functionality at your disposal, so this should be pretty straightforward). Just make some changes tovalue
and run the stylesheet on the changed XML document.