
以下は、在庫追跡用のAdobe XML swidtagの例です。関連情報を解析して新しいテキストファイルに出力するには、bashでxmllintを使用する必要があります。
たとえば、次のように解析したいと思います。
swid:entitlement_required_indicator
swid:product_title
swid:product_version
swid:name
swid:numeric
swid:major
swid:minor
swid:build
swid:review
これを試してみましたが、名前空間を読むことはできません。
xmllint --xpath '//swid:product_version/swid:name/text()' file.xml
私も試しました
xmllint --xpath "//*[local-name1()='product_version']/*[local-name2()='name']/text()" file.xml
しかし、このエラーが発生しました。
xmlXPathCompOpEval: function local-nameame1 not found
XPath error : Unregistered function
XPath error : Stack usage errror
XPath evaluation failure
Creative Suite 5のサンプルマークアップファイル次の例は、Adobe Photoshop CS5のシリアル番号Creative Suite 5 Master Collection(製品ファミリ)の例です。
<?xml version="1.0" encoding="utf-8"?>
<swid:software_identification_tag xsi:schemaLocation="http://standards.iso.org/iso/19770/-2/2008/schema.xsd software_identification_tag.xsd"
xmlns:swid="http://standards.iso.org/iso/19770/-2/2008/schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--Mandatory Identity elements -->
<swid:entitlement_required_indicator>true</swid:entitlement_required_indicator>
<swid:product_title>Acrobat XI Pro</swid:product_title>
<swid:product_version>
<swid:name>1.0</swid:name>
<swid:numeric>
<swid:major>1</swid:major>
<swid:minor>0</swid:minor>
<swid:build>0</swid:build>
<swid:review>0</swid:review>
</swid:numeric>
</swid:product_version>
<swid:software_creator>
<swid:name>Adobe Systems Incorporated</swid:name>
<swid:regid>regid.1986-12.com.adobe</swid:regid>
</swid:software_creator>
<swid:software_licensor>
<swid:name>Adobe Systems Incorporated</swid:name>
<swid:regid>regid.1986-12.com.adobe</swid:regid>
</swid:software_licensor>
<swid:software_id>
<swid:unique_id>CreativeCloud-CS6-Mac-GM-MUL</swid:unique_id>
<swid:tag_creator_regid>regid.1986-12.com.adobe</swid:tag_creator_regid>
</swid:software_id>
<swid:tag_creator>
<swid:name>Adobe Systems Incorporated</swid:name>
<swid:regid>regid.1986-12.com.adobe</swid:regid>
</swid:tag_creator>
<!--Optional Identity elements -->
<swid:license_linkage>
<swid:activation_status>activated</swid:activation_status>
<swid:channel_type>SUBSCRIPTION</swid:channel_type>
<swid:customer_type>RETAIL</swid:customer_type>
</swid:license_linkage>
<swid:serial_number>909702426602037824854600</swid:serial_number>
</swid:software_identification_tag>
答え1
これ議論する非常に啓発的です。
理想的でなくても、少なくとも次のことができるはずです。
xmllint --xpath "//*[local-name()='product_version']/*[local-name()='name']/text()" file.xml
または使用xmlstarlet代わりに:
xmlstarlet sel -t -v //swid:product_version/swid:name file.xml
答え2
ここでドキュメントを試してください。例:
#!/bin/bash
xmllint --shell file.xml <<EOF
setns swid=http://standards.iso.org/iso/19770/-2/2008/schema.xsd
xpath //swid:product_version/swid:name/text()
EOF
xmllint
このパラメータをサポートする将来のバージョンに適用されます--xpath
。
答え3
以前のバージョンを使用xmllint(--xpathはサポートされていません。)より直感的に名前空間とクエリを設定できます(ただし、追加のゴミを削除する必要があります)。
#!/bin/bash
echo 'setns swid=http://standards.iso.org/iso/19770/-2/2008/schema.xsd
cat //swid:product_version/swid:name/text()' | \
xmllint --shell file.xml | egrep -v '^(/ >| -----)'
答え4
Jenkinsシェルスクリプトからpom.xml(maven構成ファイル)を読み取るのに同様の問題がありました。良い結果を得るには、次のようにします。
xmllint --xpath "//swid:software_identification_tag/*[local-name()='product_version']/*[local-name()='name']/text()" file.xml
XMLにこれらの追加があると問題はないようです。
<swid:product_specifics>
<swid:product_version>
...
</swid:product_version>
</swid:product_specifics>
xmllint --xpath "//*[local-name()='product_version']/*[local-name()='name']/text()" file.xml
動作しません
私の場合、pom.xmlに "version"要素がたくさんあったため、特定の要素が必要な場合はパスが正確でなければなりません。