xml - Ansible - XML 命名空间

标签 xml xpath ansible

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <servers>
     <server>
       <id>nexus-dev-snapshots</id>
       <username>user</username>
       <password>hahah</password>
     </server>
     <server>
        <id>nexus-dev-releases</id>
        <username>user</username>
        <password>hahah</password>
     </server>
   </servers>

   <pluginGroups></pluginGroups>
   <proxies></proxies>
   <mirrors></mirrors>
   <profiles></profiles>
</settings>

我正在尝试使用ansible向镜像添加新元素,但收到错误消息,指出我的路径不正确,我相信这与命名空间有关,因为没有它们它就可以工作。希望能得到一些帮助,谢谢。

 - name: Add a new mirror to the mirrors element
  hosts: localhost
  connection: local
  tasks:
    - name: Add new elements
      xml:
        path: settings.xml
        xpath: /xsi:settings/xsi:mirrors/xsi:mirror/xsi:id
        value: 1
        namespaces: 
          xmlns: http://maven.apache.org/SETTINGS/1.0.0
          xsi: http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd
      xml:
        path: settings.xml
        xpath: /xsi:settings/xsi:mirrors/xsi:mirror/xsi:name
        value: 2
        namespaces:
          xmlns: http://www.w3.org/2001/XMLSchema-instance 
          xsi: http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd
      xml:
        path: settings.xml
        xpath: /xsi:settings/xsi:mirrors/xsi:mirror/xsi:url
        value: 3
        namespaces: 
          xmlns: http://maven.apache.org/SETTINGS/1.0.0
          xsi: http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd

最终结果:

<mirrors>
    <mirror>
      <id>1</id>
      <name>2</name>
      <url>3</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>

最佳答案

您的文档位于 http://maven.apache.org/SETTINGS/1.0.0 命名空间中。

你的路径应该反射(reflect)出这一点

  tasks:
    - name: Add new elements
      xml:
        path: settings.xml
        xpath: /m:settings/m:mirrors/m:mirror/m:id
        value: 1
        namespaces: 
          m: http://maven.apache.org/SETTINGS/1.0.0 

The XML document reads

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0">
  <!-- ... -->
</settings>

这意味着settings元素及其包含的所有内容都属于http://maven.apache.org/SETTINGS/1.0.0 (除非它显式声明了不同的 namespace ,这在您的文档中没有发生)。这称为“默认命名空间”。

由于它是默认命名空间,因此它在 XML 中没有前缀。但 XPath 通常需要一个前缀才能工作 - 您可以自由选择任何前缀并将其关联到 http://maven.apache.org/SETTINGS/1.0.0。为了简洁起见,我选择了 m:

关于xml - Ansible - XML 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59738794/

相关文章:

java - 如何在 Wildfly 中为 LDAP 扩展模块启用调试日志记录

java - 检查可点击的网络元素时出错

c# .net xpath 遍历时不挑选元素

shell - Ansible playbook shell 输出

Ansible - 防止剧本同时执行

amazon-web-services - Ansible CloudFormation 模块看不到 S3 对象

java - 定义一个并发哈希集spring bean?

java - 习 :include in xml file within jar file does not work in WildFly

c# - 如何在 XmlTextWriter 中设置 Settings 属性,以便我可以将每个 XML 属性写在自己的行中?

python - 用lxml解析html(标签h3)