xml
  • bash
  • command-line-interface
  • 2011-04-08 4 views 3 likes 
    3

    xmlstarlet을 사용하여 XML 노드를 선택하고 편집하려면 어떻게해야합니까?

    여기 노드를 선택합니다 :

    $ xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value conf/nutch-default.xml 
    <value/> 
    

    이것은 편집하지 않습니다 :

    $ xmlstarlet edit "/configuration/property[name='http.agent.name']"/value -v 'test' conf/nutch-default.xml 
    I/O warning : failed to load external entity "/configuration/property[name='http.agent.name']/value" 
    

    변경 사항을 변경하는 xmlstartlet 명령은 무엇이겠습니까? AFAIK -x는 아직 xmlstartlet에서 지원되지 않습니다.

    conf/nutch-default.xml

    $ xmlstarlet ed --help 
    XMLStarlet Toolkit: Edit XML document(s) 
    Usage: xml ed <global-options> {<action>} [ <xml-file-or-uri> ... ] 
    where 
        <global-options> - global options for editing 
        <xml-file-or-uri> - input XML document file name/uri (stdin otherwise) 
    
    <global-options> are: 
        -P (or --pf)  - preserve original formatting 
        -S (or --ps)  - preserve non-significant spaces 
        -O (or --omit-decl) - omit XML declaration (<?xml ...?>) 
        -N <name>=<value> - predefine namespaces (name without 'xmlns:') 
             ex: xsql=urn:oracle-xsql 
             Multiple -N options are allowed. 
             -N options must be last global options. 
        --help or -h  - display help 
    
    where <action> 
        -d or --delete <xpath> 
        -i or --insert <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> 
        -a or --append <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> 
        -s or --subnode <xpath> -t (--type) elem|text|attr -n <name> -v (--value) <value> 
        -m or --move <xpath1> <xpath2> 
        -r or --rename <xpath1> -v <new-name> 
        -u or --update <xpath> -v (--value) <value> 
          -x (--expr) <xpath> (-x is not implemented yet) 
    
    XMLStarlet is a command line toolkit to query/edit/check/transform 
    XML documents (for more information see http://xmlstar.sourceforge.net/) 
    
    $ xmlstarlet --version 
    1.0.1 
    

    답변

    4

    당신은 변수에 nutch-default.xml에의 전체 내용을 읽어 xmlstarlet와 그 변수의 내용을 편집 할 수 있습니다 그 결과를 nutch-default.xml에 다시 작성하십시오.

    또 다른 방법은 Redirect output from sed 's/c/d/' myFile to myFile에 설명 된 열린 파일 핸들을 사용하는 것입니다.

    xmlstarlet --version # 1.0.6 
    xmlstarlet ed --help | less -Ip 'inplace' 
    
    # 1. 
    # in-place version using xmlstarlet only 
    curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain' 
    xmlstarlet edit -L -u "/configuration/property[name='http.agent.name']"/value -v 'test' nutch-default.xml 
    xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 
    
    
    # 2. 
    # variable version 
    curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain' 
    xmlstr="$(< nutch-default.xml)" # save file contents to variable 
    
    printf '%s\n' "$xmlstr" | 
    xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' > nutch-default.xml 
    
    xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 
    
    
    # 3. 
    # file handle version 
    # cf. https://stackoverflow.com/questions/2585438/redirect-output-from-sed-s-c-d-myfile-to-myfile 
    curl -L -s -o nutch-default.xml 'http://svn.apache.org/viewvc/nutch/branches/branch-1.3/conf/nutch-default.xml?view=co&revision=1079746&content-type=text%2Fplain' 
    exec 3<nutch-default.xml 
    rm nutch-default.xml # prevent open file from being truncated 
    xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' <&3 >nutch-default.xml 
    xmlstarlet sel -t -c "/configuration/property[name='http.agent.name']"/value nutch-default.xml 
    
    +0

    감사합니다! $ sudo port upgrade xmlstarlet을 사용했는데 이제는 기대에 부응하여 -L을 사용했습니다. – simpatico

    1

    내 버전에서 일하고 있어요는 edit 명령에 action 옵션이 필요합니다. 새 값으로 노드를 업데이트하려면, 당신은 -u를 지정, 예 :

    xmlstarlet edit -u "/configuration/property[name='http.agent.name']"/value -v 'test' conf/nutch-default.xml 
    
    +0

    @bmk : 네 말이 맞아! 하지만 이제는 stdout으로 출력됩니다. -L은 파일을 수정해야하지만 제 에디션에서는 수정하지 않아야합니다. 어떻게 그럴 수있어? 출력을 리디렉션하려면> 파일을 비우십시오. – simpatico

    +0

    @simpatico : 내 버전의'xmlstarlet'에는'-L' 옵션이없는 것 같습니다. 나에게 완전한 명령 줄을 줄 수 있니? 그런 다음 내 대답을 업데이트 할 것입니다. – bmk

    +0

    @bmk : 내 버전이 아닙니다. http://stackoverflow.com/questions/1554143/bash-script-to-edit-xml-file을 언급하고 있습니다. 다른 방법으로 업데이트 파일을 수정할 수 있습니까? – simpatico

    2

    문서는 매우 열악합니다. 스택 오버플로에 대한 많은 답변을 읽은 후에 Stackoverflow를 우연히 만났습니다. 결국 네임 스페이스가 정의 된 요소의 값에 대해 "파일 위치 수정 옵션 편집"을위한 솔루션을 파생 시켰습니다. 아래는 XML을 감안할 때 :

    <?xml version="1.0" encoding="UTF-8"?> 
    <config> 
        <cassandra xmlns="http://venus.com/ns/mibs/VENUS-MODE/1.0"> 
        <clusterName>test-cluster</clusterName> 
        <cassandraUsername>simba</cassandraUsername> 
        <cassandraPassword>U2FsdGVkX1/Zc4NAsF59coYZLaCgddJ9b91s016HUbs=</cassandraPassword> 
        <cassandraService>Local</cassandraService> 
        </cassandra> 
        <monit xmlns="http://venus.com/ns/mibs/VENUS-MODE/1.0"> 
        <cpuUsageThreshold>70</cpuUsageThreshold> 
        <cpuUsageThresholdClear>60</cpuUsageThresholdClear> 
        <memoryUsageThreshold>70</memoryUsageThreshold> 
        <memoryUsageThresholdClear>60</memoryUsageThresholdClear> 
        </monit> 
    </config> 
    

    /설정/카산드라를 수정하는 xmlstarlet 명령은 /는 clusterName 요소 값은 다음과 같습니다

    xmlstarlet 에드 -L -N X = "HTTP : // 비너스. COM/NS/MIB에/VENUS-MODE/1.0 "-u "// 설정/X : 카산드라/X : cassandraPassword "-v"test123 "Myfile.xml

    는 & -L 옵션이 선행되어야 에드 기억 -N (네임 스페이스) 옵션. 희망이 사람이 네임 스페이스 문제가있는 파일을 편집 할 수있는 inplace 옵션을 찾는 데 도움이되기를 바랍니다.

    관련 문제