无法使用xml标记属性的xmlstarlet更新xml文件

时间:2017-06-21 01:58:05

标签: xml xml-parsing xmlstarlet

由于标记属性xmlns(名称空间)

,我无法更新以下xml文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<advertiserFeatures
xmlns:doc="http://api.homeaway.com/service/v1"
xmlns="http://api.homeaway.com/domain/v0" etag="33054400302" url="/advertisers/0024/e74510aa-d4f0-4509-8c48-7c101e6f021c/features">
<parentEntity href="/advertisers/0024/e74510aa-d4f0-4509-8c48-7c101e6f021c" rel="parentEntity"/>
<advertiseVasCart>true</advertiseVasCart>
<agentAssistedCheckout>true</agentAssistedCheckout>
<dcfEnabled>false</dcfEnabled>
<enhancedDistribution>false</enhancedDistribution>
<inserted>2016-02-12T15:38:58.977Z</inserted>
<travelerFeeEnabled>true</travelerFeeEnabled>
<updated>2016-02-12T15:38:58.977Z</updated>

如果删除该属性,我可以使用以下命令进行更新

xmlstarlet ed -u "/advertiserFeatures/advertiseVasCart" -v "false" advertiserFeatures.xml > advertiserFeatures1.xml

知道我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您需要将前缀绑定到默认命名空间URI,然后在XPath中使用该前缀来处理相应命名空间中的元素:

xmlstarlet ed -N x="http://api.homeaway.com/domain/v0" \
    -u "/x:advertiserFeatures/x:advertiseVasCart" \
    -v "false" advertiserFeatures.xml > advertiserFeatures1.xml

进一步阅读:xmlstarlet documentation on 'Namespaces and default namespace'