1

want to use a simple tool (now, I use "xmllint") to parse a xml file, which use 'xinclude' in multiple levels. There are multiple elements in single file.

then my application can parse the result xml file ea, (there is NO xinclude in it)

the command line is like

xmllint input.xml --xinclude --output output.xml --noent

found this article : XML XInclude and two elements with the same name

have tried parse="text", it does NOT work as expectation.

input.xml

<?xml version="1.0"?>
<Tag1 name="nameABC" xmlns:xi="http://www.w3.org/2003/XInclude">
  <Setting name="setting1">111</Setting>
  <xi:include href="shared.3.xml" parse="text" />
</Tag1>

shared.3.xml

<Setting name="shared_3___1">3_1</Setting>
<Setting name="shared_3___2">3_2</Setting>
<Setting name="shared_3___3">3_3</Setting>

xmllint result is

<?xml version="1.0"?>
<Tag1 xmlns:xi="http://www.w3.org/2003/XInclude" name="nameABC">


  <Setting name="setting1">111</Setting>

  &#xD;
&#xD;
  &lt;Setting name="shared_3___1"&gt;3_1&lt;/Setting&gt;&#xD;
  &lt;Setting name="shared_3___2"&gt;3_2&lt;/Setting&gt;&#xD;
  &lt;Setting name="shared_3___3"&gt;3_3&lt;/Setting&gt;&#xD;


</Tag1>

my expectation is

<?xml version="1.0"?>
<Tag1 xmlns:xi="http://www.w3.org/2003/XInclude" name="nameABC">

  <Setting name="setting1">111</Setting>

  <Setting name="shared_3___1">3_1</Setting>
  <Setting name="shared_3___2">3_2</Setting>
  <Setting name="shared_3___3">3_3</Setting>
</Tag1>

any idea??? thanks a lot

Cannot put mutiple tags ('Setting') to one wrapper tag ('Settings'), because one file can be included by lots of files. Don't want to parse 'Settings' in so many palaces.

Community
  • 1
  • 1
Akira Yuki
  • 11
  • 3

3 Answers3

1

try replacing parse="text" with parse="xml" in input.xml

"When parse="xml" (which is a default, by the way), the referenced document is fetched, is parsed as XML, and replaces the xi:include element in the source infoset along with its descendants."

"When parse="text", the referenced document is fetched and treated as plain text, replacing xi:include element in the same way."

https://msdn.microsoft.com/en-us/library/aa302291.aspx

0

Depending on what your toolbox looks like, maybe https://github.com/dret/XIPr can help. It is an implementation of XInclude in XSLT, meaning that you can process XML containing XInclude by running the XIPr XSLT code. In essence, you transform XML containing XInclude into XML where those inclusions are resolved.

Keep in mind that XIPr uses XSLT 2.0, meaning that you cannot use it if all you have is an XSLT 1.0 processor.

If you have any issues with XIPr itself, please use its GitHub page to discuss those. Thank you.

dret
  • 531
  • 3
  • 7
0

For input file:

<?xml version="1.0"?>
<Tag1 xmlns:xi="http://www.w3.org/2003/XInclude" name="nameABC">
  <Setting name="setting1">111</Setting>
  <xi:include href="shared.3.xml" parse="text"/>
</Tag1>

Externally referenced files:

<Setting name="shared_3___1">3_1</Setting>
<Setting name="shared_3___2">3_2</Setting>
<Setting name="shared_3___3">3_3</Setting>

Run command:

$ xmllint --xinclude file.xml | xmlstarlet unesc | xml fo

<?xml version="1.0"?>
<Tag1 xmlns:xi="http://www.w3.org/2003/XInclude" name="nameABC">
  <Setting name="setting1">111</Setting>
  <Setting name="shared_3___1">3_1</Setting>
  <Setting name="shared_3___2">3_2</Setting>
  <Setting name="shared_3___3">3_3</Setting>
</Tag1>
focog77269
  • 104
  • 5