15

私は、libxml が XPath サポートをどのように実装しているかを確認しようとしているので、xmllint を使用してテストすることに意味がありました。ただし、明らかなオプションである --pattern はやや不明瞭であり、次のようなものを使用することになりました。

test.xml: <foo><bar/><bar/></foo>

> xmllint --shell test.xml
/  > dir /foo
ELEMENT foo
/  > dir /foo/*
ELEMENT bar
ELEMENT bar

これは機能しているようで、それは素晴らしいことですが、私はまだ興味があります. xmllint の --pattern オプションは何のためにあり、どのように機能しますか?

満点の例を挙げてください。=)

4

4 に答える 4

7

ヒントは、「パーサーへのリーダー インターフェイスで使用できる」という言葉にあります。xmllint は、 --stream オプションが渡された場合にのみリーダー インターフェイスを使用します。

$ xmllint --stream --pattern /foo/bar test.xml
Node /foo/bar[1] matches pattern /foo/bar
Node /foo/bar matches pattern /foo/bar
于 2012-05-02T19:24:17.170 に答える
3

単純に多数の xml ノードのテキスト値が必要な場合は、次のようなものを使用できます (お使いのバージョンの xmllint で --xpath が利用できない場合)。

./foo.xml:

<hello>
   <world>its alive!!</world>
   <world>and works!!</world>
</hello>

$ xmllint --stream --pattern /hello/world --debug ./foo.xml | grep -A 1 "matches pattern" | grep "#text" | sed 's/.* [0-9] //'
its alive!!
and works!!
于 2013-07-22T15:28:57.243 に答える
3

xmllint(1) の man ページから:

   --pattern PATTERNVALUE
          Used to exercise the pattern recognition engine, which can be
          used with the reader interface to the parser. It allows to
          select some nodes in the document based on an XPath (subset)
          expression. Used for debugging.

XPath のサブセットのみを認識し、その意図はデバッグを支援することです。XPath を完全に理解するライブラリは、libxslt(3) とそのコマンドライン ツール xsltproc(1) です。

libxml の ``pattern'' モジュールは、「ツリー内またはパーサー状態に基づいてノードのパターン表現をコンパイルおよびテストすることを可能にします」。そのドキュメントはhttp://xmlsoft.org/html/libxml-pattern にあります。 html

アリ。

于 2010-02-12T22:57:43.333 に答える