如何使用xmllint从具有命名空间的XML中基于另一个节点的值从一个节点获取值

时间:2019-03-26 00:27:37

标签: xml xmllint

我有一个具有以下名称空间的xml文件(“ test.xml”)

    <?xml version='1.0' encoding='utf-8'?>
    <domain xmlns="http://xmlns.oracle.com/weblogic/domain" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/security/wls http://xmlns.oracle.com/weblogic/security/wls/1.0/wls.xsd http://xmlns.oracle.com/weblogic/domain http://xmlns.oracle.com/weblogic/1.0/domain.xsd http://xmlns.oracle.com/weblogic/security/xacml http://xmlns.oracle.com/weblogic/security/xacml/1.0/xacml.xsd http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator http://xmlns.oracle.com/weblogic/security/providers/passwordvalidator/1.0/passwordvalidator.xsd http://xmlns.oracle.com/weblogic/security http://xmlns.oracle.com/weblogic/1.0/security.xsd">
      <name>domain1</name>
      <domain-version>12.2.1.3.0</domain-version>
      <security-configuration>
        <name>domainname</name>
        <realm>
          <sec:authentication-provider xsi:type="wls:default-authenticatorType">
            <sec:name>DefaultAuthenticator</sec:name>
          </sec:authentication-provider>
          <sec:credential-mapper xsi:type="wls:default-credential-mapperType">
            <sec:name>DefaultCredentialMapper</sec:name>
          </sec:credential-mapper>
          <sec:cert-path-provider xsi:type="wls:web-logic-cert-path-providerType">
            <sec:name>WebLogicCertPathProvider</sec:name>
          </sec:cert-path-provider>
          <sec:cert-path-builder>WebLogicCertPathProvider</sec:cert-path-builder>
          <sec:name>myrealm</sec:name>
        </realm>
        <default-realm>myrealm</default-realm>
        <credential-encrypted>{AES}abcdef</credential-encrypted>
        <node-manager-username>weblogic</node-manager-username>
        <node-manager-password-encrypted>{AES}abcdef</node-manager-password-encrypted>
        <enforce-valid-basic-auth-credentials>false</enforce-valid-basic-auth-credentials>
      </security-configuration>
      <server>
        <name>AdminServer</name>
        <listen-address></listen-address>
      </server>
      <server>
        <name>DEV1</name>
        <ssl>
          <enabled>false</enabled>
        </ssl>
        <log>
          <file-name>logs/DEV1.log</file-name>
          <rotation-type>bySize</rotation-type>
          <number-of-files-limited>true</number-of-files-limited>
          <file-count>5</file-count>
          <file-min-size>500</file-min-size>
          <rotate-log-on-startup>true</rotate-log-on-startup>
        </log>
        <machine>Machine-0</machine>
        <listen-port>8080</listen-port>
        <listen-port-enabled>true</listen-port-enabled>
        <cluster xsi:nil="true"></cluster>
        <web-server>
          <web-server-log>
            <file-name>logs/access.log</file-name>
            <rotation-type>bySize</rotation-type>
            <number-of-files-limited>true</number-of-files-limited>
            <file-count>5</file-count>
            <file-min-size>500</file-min-size>
            <rotate-log-on-startup>true</rotate-log-on-startup>
            <logging-enabled>true</logging-enabled>
          </web-server-log>
        </web-server>
        <server-debug>
          <debug-scope>
            <name>weblogic.jdbc</name>
            <enabled>false</enabled>
          </debug-scope>
          <debug-jdbcucp>false</debug-jdbcucp>
        </server-debug>
        <listen-address></listen-address>
        <java-compiler>javac</java-compiler>
        <server-start>
          <arguments>-Xms16G -Xmx16G -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -Xloggc:GC_DEV1.log -XX:+DisableExplicitGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NumberOfGCLogFiles=20 -XX:GCLogFileSize=10240K -XX:+UseGCLogFileRotation -Dcom.conceptwave.appName=ABC</arguments>
        </server-start>
        <client-cert-proxy-enabled>false</client-cert-proxy-enabled>
        <server-diagnostic-config>
          <diagnostic-context-enabled>false</diagnostic-context-enabled>
          <wldf-diagnostic-volume>Low</wldf-diagnostic-volume>
        </server-diagnostic-config>
        <max-concurrent-new-threads>500</max-concurrent-new-threads>
        <max-concurrent-long-running-requests>500</max-concurrent-long-running-requests>
      </server>
      <server>
        <name>DEV2</name>
        <ssl>
          <enabled>false</enabled>
        </ssl>
        <log>
          <file-name>logs/DEV1.log</file-name>
          <rotation-type>bySize</rotation-type>
          <number-of-files-limited>true</number-of-files-limited>
          <file-count>5</file-count>
          <file-min-size>500</file-min-size>
          <rotate-log-on-startup>true</rotate-log-on-startup>
        </log>
        <machine>Machine-0</machine>
        <listen-port>8080</listen-port>
        <listen-port-enabled>true</listen-port-enabled>
        <cluster xsi:nil="true"></cluster>
        <web-server>
          <web-server-log>
            <file-name>logs/access.log</file-name>
            <rotation-type>bySize</rotation-type>
            <number-of-files-limited>true</number-of-files-limited>
            <file-count>5</file-count>
            <file-min-size>500</file-min-size>
            <rotate-log-on-startup>true</rotate-log-on-startup>
            <logging-enabled>true</logging-enabled>
          </web-server-log>
        </web-server>
        <server-debug>
          <debug-scope>
            <name>weblogic.jdbc</name>
            <enabled>false</enabled>
          </debug-scope>
          <debug-jdbcucp>false</debug-jdbcucp>
        </server-debug>
        <listen-address></listen-address>
        <java-compiler>javac</java-compiler>
        <server-start>
          <arguments>-Xms16G -Xmx16G</arguments>
        </server-start>
        <client-cert-proxy-enabled>false</client-cert-proxy-enabled>
        <server-diagnostic-config>
          <diagnostic-context-enabled>false</diagnostic-context-enabled>
          <wldf-diagnostic-volume>Low</wldf-diagnostic-volume>
        </server-diagnostic-config>
        <max-concurrent-new-threads>500</max-concurrent-new-threads>
        <max-concurrent-long-running-requests>500</max-concurrent-long-running-requests>
      </server>
      <embedded-ldap>
        <name>asdasd</name>
        <credential-encrypted>{AES}Casdasdasdv</credential-encrypted>
      </embedded-ldap>
      <configuration-version>12.2.1.3.0</configuration-version>
      <app-deployment>
        <name>asda</name>
        <target>DEV1</target>
        <module-type>war</module-type>
        <source-path>asdasd</source-path>
        <security-dd-model>DDOnly</security-dd-model>
        <staging-mode xsi:nil="true"></staging-mode>
        <plan-staging-mode xsi:nil="true"></plan-staging-mode>
        <cache-in-app-directory>false</cache-in-app-directory>
      </app-deployment>
      <machine>
        <name>Machine-0</name>
        <node-manager>
          <nm-type>Plain</nm-type>
          <listen-address>localhost</listen-address>
          <listen-port>5556</listen-port>
          <debug-enabled>false</debug-enabled>
        </node-manager>
      </machine>
      <admin-server-name>AdminServer</admin-server-name>
      <jdbc-system-resource>
        <name>asdasd</name>
        <target></target>
        <descriptor-file-name>jdbc/asdasdasd.xml</descriptor-file-name>
      </jdbc-system-resource>
      <jdbc-system-resource>
        <name>asdasd</name>
        <target></target>
        <descriptor-file-name>jdbc/asdasd.xml</descriptor-file-name>
      </jdbc-system-resource>
      <jdbc-system-resource>
        <name>asdasd</name>
        <target>DEV1</target>
        <descriptor-file-name>jdbc/asdasd.xml</descriptor-file-name>
      </jdbc-system-resource>
    </domain>

使用xmllint,我想通过提供服务器arguments来获取name 例如

如果/ domain / server / name为“ DEV1”,则输出为

-Xms16G -Xmx16G -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -Xloggc:GC_DEV1.log -XX:+DisableExplicitGC -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:NumberOfGCLogFiles=20 -XX:GCLogFileSize=10240K -XX:+UseGCLogFileRotation -Dcom.conceptwave.appName=ABC

如果/ domain / server / name为“ DEV2”,则输出为

-Xms16G -Xmx16G
  1. 我尝试在下面设置名称空间,但不起作用

echo -e 'setns x=http://xmlns.oracle.com/weblogic/domain\ncat /x:domain/x:server[/x:name[text()="DEV1"]]/x:server-start/x:arguments/text()' | xmllint --shell test.xml

它给出输出/ > / > / >

  1. 我也尝试过使用sed和xpath,如下所示,仍然无法正常工作

cat test.xml | sed '2 s/xmlns=".*"//g' | xmllint --xpath '//domain/server[name[text()="DEV1"]]/server-start/arguments/text()' -

这会给出所有namespace not defined错误以及正确的arguments

1 个答案:

答案 0 :(得分:0)

改用local-name()

xmllint --xpath '//*[local-name()="server"][*[local-name()="name"]="DEV1"]//*[local-name()="arguments"]/text()' test.xml

xmllint --xpath '//*[local-name()="server"][*[local-name()="name"]="DEV2"]//*[local-name()="arguments"]/text()' test.xml