문제

쉘 스크립팅을 수행할 때 일반적으로 데이터는 csv와 같은 한 줄 레코드 파일에 있습니다.이 데이터를 처리하는 것은 정말 간단합니다. grep 그리고 sed.하지만 XML을 자주 처리해야 하므로 명령줄을 통해 해당 XML 데이터에 대한 액세스를 스크립트로 작성하는 방법이 정말 필요합니다.최고의 도구는 무엇입니까?

도움이 되었습니까?

해결책

나는 xmlstarlet이 이런 종류의 일에 꽤 능숙하다는 것을 알았습니다.

http://xmlstar.sourceforge.net/

대부분의 배포판 리포지토리에서도 사용할 수 있습니다.소개 튜토리얼은 다음과 같습니다.

http://www.ibm.com/developerworks/library/x-starlet.html

다른 팁

몇 가지 유망한 도구:

  • 노코기리:XPath 및 CSS 선택기를 사용하여 Ruby에서 HTML/XML DOM 구문 분석

  • 히프리코:더 이상 사용되지 않음

  • fxgrep:자체 XPath와 유사한 구문을 사용하여 문서를 쿼리합니다.SML로 작성되므로 설치가 어려울 수 있습니다.

  • LT XML:다음을 포함하여 SGML 도구에서 파생된 XML 도구 키트 sggrep, sgsort, xmlnorm 다른 사람.자체 쿼리 구문을 사용합니다.문서는매우 공식적인.C로 작성되었습니다.LT XML 2는 XPath, XinClude 및 기타 W3C 표준의 지원을 주장합니다.

  • xmlgrep2:XPath를 사용한 간단하고 강력한 검색.xml :: libxml 및 libxml2를 사용하여 Perl로 작성되었습니다.

  • XQ샤프:XPath의 확장인 XQuery를 지원합니다..NET Framework용으로 작성되었습니다.

  • xml-coreutils:GNU coreutils와 동등한 Laird Breyer의 툴킷입니다.흥미롭게 논의 수필 이상적인 툴킷에 무엇이 포함되어야 하는지에 대해 알아보세요.

  • xmldiff:두 개의 XML 파일을 비교하는 간단한 도구입니다.

  • xmltk:debian, ubuntu, fedora 또는 macports에 패키지가 없는 것 같고 2007년 이후 릴리스가 없으며 이식 불가능한 빌드 자동화를 사용합니다.

xml-coreutils는 가장 잘 문서화되어 있고 가장 UNIX 지향적인 것 같습니다.

도 있습니다 xml2 그리고 2xml 쌍.일반적인 문자열 편집 도구에서 XML을 처리할 수 있습니다.

예.q.xml:

<?xml version="1.0"?>
<foo>
    text
    more text
    <textnode>ddd</textnode><textnode a="bv">dsss</textnode>
    <![CDATA[ asfdasdsa <foo> sdfsdfdsf <bar> ]]>
</foo>

xml2 < q.xml

/foo=
/foo=   text
/foo=   more text
/foo=   
/foo/textnode=ddd
/foo/textnode
/foo/textnode/@a=bv
/foo/textnode=dsss
/foo=
/foo=    asfdasdsa <foo> sdfsdfdsf <bar> 
/foo=

xml2 < q.xml | grep textnode | sed 's!/foo!/bar/baz!' | 2xml

<bar><baz><textnode>ddd</textnode><textnode a="bv">dsss</textnode></baz></bar>

추신또한 있다 html2 / 2html.

Joseph Holsten의 훌륭한 목록에 Perl 라이브러리 XML::XPath와 함께 제공되는 xpath 명령줄 스크립트를 추가합니다.XML 파일에서 정보를 추출하는 좋은 방법:

 xpath -q -e '/entry[@xml:lang="fr"]' *xml

xmllint를 사용할 수 있습니다.

xmllint --xpath //title books.xml

대부분의 배포판과 함께 번들로 제공되어야 하며 Cygwin과도 번들로 제공됩니다.

$ xmllint --version
xmllint: using libxml version 20900

보다:

$ xmllint
Usage : xmllint [options] XMLfiles ...
        Parse the XML files and output the result of the parsing
        --version : display the version of the XML library used
        --debug : dump a debug tree of the in-memory document
        ...
        --schematron schema : do validation against a schematron
        --sax1: use the old SAX1 interfaces for processing
        --sax: do not build a tree but work just at the SAX level
        --oldxml10: use XML-1.0 parsing rules before the 5th edition
        --xpath expr: evaluate the XPath expression, inply --noout

NetBSD xmltools의 xmlsed 및 xmlgrep도 있습니다!

http://blog.huoc.org/xmltools-not-dead.html

Windows에서 솔루션을 찾고 있다면 Powershell에는 XML 읽기 및 쓰기 기능이 내장되어 있습니다.

테스트.xml:

<root>
  <one>I like applesauce</one>
  <two>You sure bet I do!</two>
</root>

파워셸 스크립트:

# load XML file into local variable and cast as XML type.
$doc = [xml](Get-Content ./test.xml)

$doc.root.one                                   #echoes "I like applesauce"
$doc.root.one = "Who doesn't like applesauce?"  #replace inner text of <one> node

# create new node...
$newNode = $doc.CreateElement("three")
$newNode.set_InnerText("And don't you forget it!")

# ...and position it in the hierarchy
$doc.root.AppendChild($newNode)

# write results to disk
$doc.save("./testNew.xml")

testNew.xml:

<root>
  <one>Who likes applesauce?</one>
  <two>You sure bet I do!</two>
  <three>And don't you forget it!</three>
</root>

원천: https://serverfault.com/questions/26976/update-xml-from-the-command-line-windows

정확히 무엇을 하고 싶은지에 따라 다릅니다.

XSLT가 좋은 방법일 수도 있지만 학습 곡선이 있습니다.노력하다 xsltproc 매개변수를 전달할 수 있습니다.

또한 있습니다 saxon-lint 명령줄에서 XPath 3.0/XQuery 3.0을 사용할 수 있는 기능이 있습니다.(다른 명령줄 도구는 XPath 1.0을 사용합니다.)

예:

http/html:

$ saxon-lint --html --xpath 'count(//a)' http://stackoverflow.com/q/91791
328

XML :

$ saxon-lint --xpath '//a[@class="x"]' file.xml

XQuery가 좋은 솔루션이 될 수 있습니다.(상대적으로) 배우기 쉽고 W3C 표준입니다.

나는 추천하고 싶다 XQ샤프 명령행 처리기의 경우.

나는 처음에 사용했다 xmlstarlet 아직도 사용하고 있어요.쿼리가 어려워지면 XML이 필요합니다. xpath2 그리고 x쿼리 내가 의지하는 기능 지원 자델 http://www.videlibri.de/xidel.html

JEdit에는 XML 문서에 대한 쿼리 기능을 제공하는 "XQuery"라는 플러그인이 있습니다.

명령줄은 아니지만 작동합니다!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top