xmlstarlet 与命名空间属性替换

问题描述

我无法用 xmlstarlet 替换属性

狗.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit    http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<rabbit:connection-factory id="mqConnectionFactory"
host="localhost"
port="9999" />
</beans>

已经尝试了以下多种变体:

xmlstarlet ed -N B="http://www.springframework.org/schema/beans" -N R="http://www.springframework.org/schema/rabbit" --update "/B/R:connection-factor/@host"  --value "myserver" dog.xml

我错过了什么?我已经尝试过 beans 和 B:beans for B 以及 R 的许多变体,但我认为没有选择正确的元素。

解决方法

使用完整的命名空间声明,

xmlstarlet ed \
    -N B="http://www.springframework.org/schema/beans" \
    -N rabbit="http://www.springframework.org/schema/rabbit" \
    --update '/B:beans/rabbit:connection-factory/@host' \
    --value "myserver" dog.xml

或者,使用 xmlstarlet 快捷方式,根据 user guide

xmlstarlet ed \
    -N rabbit="http://www.springframework.org/schema/rabbit" \
    --update '/_:beans/rabbit:connection-factory/@host' \
    --value "myserver" dog.xml

发现错别字:您的命令丢失 y 中的最后一个 connection-factory

,

以您的示例解决 failed to load external entity 问题的两种方法:

xmlstarlet edit --update "//@host" --value "myserver" file.xml

xmlstarlet edit --update "//*[local-name()='connection-factory']/@host" --value "myserver" file.xml