简体   繁体   English

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

[英]How to get value from a node based on another node value from an XML with namespace using xmllint

I have an xml file ("test.xml") with namespace as below 我有一个带有名称空间的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>

Using xmllint, I want to get arguments by providing the server name eg 使用xmllint,我想通过提供服务器name来获取arguments ,例如

if /domain/server/name is "DEV1" then output is 如果/ 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

if /domain/server/name "DEV2" then output is 如果/ domain / server / name为“ DEV2”,则输出为

-Xms16G -Xmx16G
  1. I tried setting the namespace with below, but doesnt work 我尝试在下面设置名称空间,但不起作用

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

It gives output / > / > / > 它给输出/ > / > / >

  1. I also tried using sed and xpath as below, still doesnt work 我也尝试使用sed和xpath如下,仍然无法正常工作

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

This gives all the namespace not defined errors along with the correct arguments 这给出了所有namespace not definednamespace not defined错误以及正确的arguments

想通了使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM