xmlxmllintxinclude

Why does XInclude escape < and > as XML/HTML character entities, &lt; and &gt;?


I have a very simple XML file:

<?xml version="1.0" encoding="UTF-8"?>

<!-- System configuration file -->
<!-- Minimum viable product of HIL simulator -->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xi="http://www.w3.org/2001/XInclude"
                xsi:schemaLocation="configuration.xsd">

    <application name="bridge" id="k1">
        <schedule>
            <process cpus="7" policy="SCHED_FIFO" priority="40"/>
        </schedule>
        <configuration>
            <sim_bridge servo_type="kDuplex">
                <xi:include href="TABLE.xml" parse="text" />
            </sim_bridge>
        </configuration>
    </application>
</configuration>

The table is:

<point surface_angle="-18.2" servo_angle="14.9"/>
<point surface_angle="-7.7" servo_angle="4.48"/>
<point surface_angle="-3.4" servo_angle="0.05"/>
<point surface_angle="2.1" servo_angle="-5.36"/>
<point surface_angle="12.2" servo_angle="-15.31"/>

However, when I run xmllint the output doesn't contain the < and > characters:

<application name="bridge" id="k1">
    <schedule>
        <process cpus="7" policy="SCHED_FIFO" priority="40"/>
    </schedule>
    <configuration>
        <sim_bridge servo_type="kDuplex">
            &lt;point surface_angle="-18.2" servo_angle="14.9"/&gt;
            &lt;point surface_angle="-7.7" servo_angle="4.48"/&gt;
            &lt;point surface_angle="-3.4" servo_angle="0.05"/&gt;
            &lt;point surface_angle="2.1" servo_angle="-5.36"/&gt;
            &lt;point surface_angle="12.2" servo_angle="-15.31"/&gt;
        </sim_bridge>
    </configuration>
</application>

Is there any way to fix this and have the output file with correct format?


Solution

  • If you change

    <xi:include href="TABLE.xml" parse="text" />
    

    to

    <xi:include href="TABLE.xml" parse="xml" xpointer="xpointer(/r/point)" />
    

    and make TABLE.xml be well-formed by wrapping its elements with a single root element,

    <r>
      <point surface_angle="-18.2" servo_angle="14.9"/>
      <point surface_angle="-7.7" servo_angle="4.48"/>
      <point surface_angle="-3.4" servo_angle="0.05"/>
      <point surface_angle="2.1" servo_angle="-5.36"/>
      <point surface_angle="12.2" servo_angle="-15.31"/>
    </r>
    

    then run,

    xmllint --xinclude --format try.xml
    

    you'll see your included file as XML,

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- System configuration file -->
    <!-- Minimum viable product of HIL simulator -->
    <configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="configuration.xsd">
      <application name="bridge" id="k1">
        <schedule>
          <process cpus="7" policy="SCHED_FIFO" priority="40"/>
        </schedule>
        <configuration>
          <sim_bridge servo_type="kDuplex">
    
            <point surface_angle="-18.2" servo_angle="14.9"/>
            <point surface_angle="-7.7" servo_angle="4.48"/>
            <point surface_angle="-3.4" servo_angle="0.05"/>
            <point surface_angle="2.1" servo_angle="-5.36"/>
            <point surface_angle="12.2" servo_angle="-15.31"/>
    
          </sim_bridge>
        </configuration>
      </application>
    </configuration>
    

    as requested.