【问题标题】:Why does XInclude escape < and > as XML/HTML character entities, &lt; and &gt;?为什么 XInclude 转义 < 和 > 作为 XML/HTML 字符实体,< 和 >?
【发布时间】:2021-12-09 13:08:52
【问题描述】:

我有一个非常简单的 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">
                <xi:include href="TABLE.xml" parse="text" />
            </sim_bridge>
        </configuration>
    </application>
</configuration>

表格是:

<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"/>

但是,当我运行 xmllint 时,输出不包含 &lt;&gt; 字符:

<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>

有什么办法可以解决这个问题并让输出文件的格式正确吗?

【问题讨论】:

    标签: xml xmllint xinclude


    【解决方案1】:

    如果你改变了

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

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

    并通过使用单个根元素包装其元素来使 TABLE.xml 格式正确,

    <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>
    

    然后运行,

    xmllint --xinclude --format try.xml
    

    您会看到包含的文件为 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>
    

    根据要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多