Xmlstarlet选择节点并添加子节点

时间:2019-04-22 09:57:52

标签: xml xmlstarlet

我试图自动将新的子节点添加到nexus-core-feature-3.16.1-02.xml中,因为我们使用的某些插件无法与“ box”解决方案一起使用。 要点是,当发布新版本的nexus-container时,我有一个bash脚本,该脚本运行新的test-container版本并将新的default-xml文件复制到适当的文件夹中。 下一步是编辑此文件并添加一些新功能以使插件正常工作。当完成的旧容器停止时,用新的xml替换旧的容器,然后用映射的nexus-data和default-xml启动docker-container。 为了使该插件正常工作,我需要对default-xml文件进行一些更改。我想为此使用xmlstarlet。 将此添加到“ nexus-core-feature”部分:

<feature version="1.0.9" prerequisite="false" dependency="false">nexus-repository-apt</feature>
  </feature>

这到文件xml-file的末尾

<feature name="nexus-repository-apt" description="net.staticsnow:nexus-repository-apt" version="1.0.10">
     <details>net.staticsnow:nexus-repository-apt</details>
     <bundle>mvn:net.staticsnow/nexus-repository-apt/1.0.10</bundle>
     <bundle>mvn:org.apache.commons/commons-compress/1.18</bundle>
     <bundle>mvn:org.tukaani/xz/1.8</bundle>
</feature>
</features>

所以我曾经在Google和SO呆了一段时间,但我仍然陷于困境。 例如,这种情况:How to insert a new element under another with xmlstarlet?

做类似的事情似乎很简单,我尝试过:

xmlstarlet ed -s /features/feature/feature -t elem -n featureTMP -v "nexus-apt-repositroy" \
    -i //featureTMP -t attr -n "version" -v "1.0.9" \
    -i //featureTMP P -t attr -n "prerequisite" -v "false" \
    -i //featureTMP -t attr -n "dependency" -v "false" \
    -r //featureTMP -v feature nexus-core-feature-3.16.1-02-features.xml

我怀疑我的错误是(现在是)节点路径。

下一步是检查节点

xmlstarlet sel -t -c "/" nexus-core-feature-3.16.1-02-features.xml

输出是整个xml文件,看起来很好

<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0" name="nexus-core-feature">
    <feature name="nexus-core-feature" description="org.sonatype.nexus.assemblies:nexus-core-feature" version="3.16.1.02">
        <details>org.sonatype.nexus.assemblies:nexus-core-feature</details>
        <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-audit-plugin</feature>
        <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-blobstore-tasks</feature>
        <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-ssl-plugin</feature>
        <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-coreui-plugin</feature>
        <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-repository-httpbridge</feature>
...

但是当我尝试进入节点时,结果始终为空:

xmlstarlet sel -t -c "/features" nexus-core-feature-3.16.1-02-features.xml

使用属性选择器,它仍然为空:

xmlstarlet sel -t -c "/features/feature[@name="nexus-core-feature"]" nexus-core-feature-3.16.1-02-features.xml

试图在XPath在线测试器中以及在测试器内部进行检查,一切正常。

接下来,我在本文中使用更简单的示例:https://unix.stackexchange.com/questions/386965/insert-custom-xml-tag-into-a-xml-file-in-a-bash-script

并试图浏览arctilce中的示例文件,这似乎很好。

xmlstarlet sel -t -c "/server-groups" file.xml

输出:

<server-groups>
  <server-group name="main-server-group" profile="full">
    <jvm name="default">
      <heap size="64m" max-size="512m"/>
      <jvm-options>
        <option value="somevalue"/>
      </jvm-options>
    </jvm>
    <socket-binding-group ref="full-sockets"/>
  </server-group>
</server-groups>

下一步

xmlstarlet sel -t -c "/server-groups/server-group/jvm" file.xml

输出:

<jvm name="default">
      <heap size="64m" max-size="512m"/>
      <jvm-options>
        <option value="somevalue"/>
      </jvm-options>
    </jvm>

这对我来说非常令人困惑...为什么相同的方法不适用于nexus-xml文件?更复杂的\奇怪的结构?会为您提供任何建议

1 个答案:

答案 0 :(得分:0)

  

为什么相同的方法不适用于nexus-xml文件?

这是因为您的nexus XML文件位于默认名称空间(http://karaf.apache.org/xmlns/features/v1.4.0)中。

如果您使用的是xmlstarlet 1.0.5或更高版本,则可以在XPath中使用_:来匹配任何名称空间。否则,您必须将名称空间绑定到带有-N的前缀。 See here了解更多信息。

这是您第一次尝试的更新示例:

xmlstarlet ed -s /_:features/_:feature -t elem -n featureTMP -v "nexus-repository-apt" \
    -i //featureTMP -t attr -n "version" -v "1.0.9" \
    -i //featureTMP -t attr -n "prerequisite" -v "false" \
    -i //featureTMP -t attr -n "dependency" -v "false" \
    -r //featureTMP -v feature nexus-core-feature-3.16.1-02-features.xml

这是使用-N ...

的替代方法
xmlstarlet ed -N f="http://karaf.apache.org/xmlns/features/v1.4.0" -s /f:features/f:feature -t elem -n featureTMP -v "nexus-repository-apt" \
    -i //featureTMP -t attr -n "version" -v "1.0.9" \
    -i //featureTMP -t attr -n "prerequisite" -v "false" \
    -i //featureTMP -t attr -n "dependency" -v "false" \
    -r //featureTMP -v feature nexus-core-feature-3.16.1-02-features.xml    

这两个都产生以下输出:

<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0" name="nexus-core-feature">
  <feature name="nexus-core-feature" description="org.sonatype.nexus.assemblies:nexus-core-feature" version="3.16.1.02">
    <details>org.sonatype.nexus.assemblies:nexus-core-feature</details>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-audit-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-blobstore-tasks</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-ssl-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-coreui-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-repository-httpbridge</feature>
    <feature version="1.0.9" prerequisite="false" dependency="false">nexus-repository-apt</feature>
  </feature>
</features>

可以通过相同的方式添加第二个feature,但是您也可以通过将xmlstarlet中的XSLT与tr command一起使用来简化整个过程。 (无论如何,我认为这很简单。)

XSLT (test.xsl)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:f="http://karaf.apache.org/xmlns/features/v1.4.0"
  xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"
  exclude-result-prefixes="f">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="f:features">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <feature name="nexus-repository-apt" description="net.staticsnow:nexus-repository-apt" version="1.0.10">
        <details>net.staticsnow:nexus-repository-apt</details>
        <bundle>mvn:net.staticsnow/nexus-repository-apt/1.0.10</bundle>
        <bundle>mvn:org.apache.commons/commons-compress/1.18</bundle>
        <bundle>mvn:org.tukaani/xz/1.8</bundle>
      </feature>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="f:feature[@name='nexus-core-feature']">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <feature version="1.0.9" prerequisite="false" dependency="false">nexus-repository-apt</feature>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

xmlstarlet命令

xmlstarlet tr test.xsl nexus-core-feature-3.16.1-02-features.xml

输出

<features xmlns="http://karaf.apache.org/xmlns/features/v1.4.0" name="nexus-core-feature">
  <feature name="nexus-core-feature" description="org.sonatype.nexus.assemblies:nexus-core-feature" version="3.16.1.02">
    <details>org.sonatype.nexus.assemblies:nexus-core-feature</details>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-audit-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-blobstore-tasks</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-ssl-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-coreui-plugin</feature>
    <feature version="3.16.1.02" prerequisite="false" dependency="false">nexus-repository-httpbridge</feature>
    <feature version="1.0.9" prerequisite="false" dependency="false">nexus-repository-apt</feature>
  </feature>
  <feature name="nexus-repository-apt" description="net.staticsnow:nexus-repository-apt" version="1.0.10">
    <details>net.staticsnow:nexus-repository-apt</details>
    <bundle>mvn:net.staticsnow/nexus-repository-apt/1.0.10</bundle>
    <bundle>mvn:org.apache.commons/commons-compress/1.18</bundle>
    <bundle>mvn:org.tukaani/xz/1.8</bundle>
  </feature>
</features>
相关问题