我有一個 XML 檔案,我需要從 XML 檔案中提取一個元素(在 Linux 機器上),但不幸的是,機器沒有“xsltproc”命令(我無法安裝它),所以我正在嘗試弄清楚如何使用其他可用的工具(例如 sed 等)進行提取。
以下是 XML 的示例:
<l7:List xmlns:l7="http://ns.l7tech.com/2010/04/gateway-management">
<l7:Name>REVOCATION_CHECK_POLICY List</l7:Name>
<l7:Type>List</l7:Type>
<l7:TimeStamp>2022-05-30T12:36:16.994Z</l7:TimeStamp>
<l7:Link rel="self" uri="https://myhost02.xxxx.com:8443/restman/1.0/revocationCheckingPolicies"/>
<l7:Link rel="template" uri="https://myhost02.xxxx.com:8443/restman/1.0/revocationCheckingPolicies/template"/>
<l7:Item>
<l7:Name>OCSPREVOCATIONVALIDATION</l7:Name>
<l7:Id>a60c5a8714b2e519a6c23192cf09ded5</l7:Id>
<l7:Type>REVOCATION_CHECK_POLICY</l7:Type>
<l7:TimeStamp>2022-05-30T12:36:16.994Z</l7:TimeStamp>
<l7:Link rel="self" uri="https://myhost02.xxxx.com:8443/restman/1.0/revocationCheckingPolicies/a60c5a8714b2e519a6c23192cf09ded5"/>
<l7:Resource>
<l7:RevocationCheckingPolicy id="a60c5a8714b2e519a6c23192cf09ded5" version="20">
<l7:Name>OCSPREVOCATIONVALIDATION</l7:Name>
<l7:DefaultPolicy>true</l7:DefaultPolicy>
<l7:ContinueOnServerUnavailable>false</l7:ContinueOnServerUnavailable>
<l7:DefaultSuccess>false</l7:DefaultSuccess>
<l7:RevocationCheckItems>
<l7:Type>OCSP from URL</l7:Type>
<l7:Url>http://foo.west.dev.xxxx.com:80</l7:Url>
<l7:AllowIssuerSignature>true</l7:AllowIssuerSignature>
<l7:TrustedSigners>3c880ed5addceb2e9ef308074f2c353f</l7:TrustedSigners>
</l7:RevocationCheckItems>
</l7:RevocationCheckingPolicy>
</l7:Resource>
</l7:Item>
</l7:List>
我需要將以下 XML 提取到單獨的變數或檔案中:
<l7:RevocationCheckingPolicy id="a60c5a8714b2e519a6c23192cf09ded5" version="20">
<l7:Name>OCSPREVOCATIONVALIDATION</l7:Name>
<l7:DefaultPolicy>true</l7:DefaultPolicy>
<l7:ContinueOnServerUnavailable>false</l7:ContinueOnServerUnavailable>
<l7:DefaultSuccess>false</l7:DefaultSuccess>
<l7:RevocationCheckItems>
<l7:Type>OCSP from URL</l7:Type>
<l7:Url>http://foo.west.dev.xxxx.com:80</l7:Url>
<l7:AllowIssuerSignature>true</l7:AllowIssuerSignature>
<l7:TrustedSigners>3c880ed5addceb2e9ef308074f2c353f</l7:TrustedSigners>
</l7:RevocationCheckItems>
</l7:RevocationCheckingPolicy>
我可以使用以下方法將檔案“展平”為單個字串:
sed ':a;N;$!ba;s/\n//g' response.xml
但后來我必須嘗試提取我需要的字串(在:
<l7:RevocationCheckingPolicy
和:
</l7:RevocationCheckingPolicy>
包含兩個匹配的字串。
我可以在沒有匹配字串的情況下提取子字串:
sed ':a;N;$!ba;s/\n//g' response.xml | sed -e 's/.*<l7\:RevocationCheckingPolicy\(.*\)<\/l7\:RevocationCheckingPolicy>.*/\1/'
這給了我:
id="a60c5a8714b2e519a6c23192cf09ded5" version="20"> <l7:Name>OCSPREVOCATIONVALIDATION</l7:Name> <l7:DefaultPolicy>true</l7:DefaultPolicy> <l7:ContinueOnServerUnavailable>false</l7:ContinueOnServerUnavailable> <l7:DefaultSuccess>false</l7:DefaultSuccess> <l7:RevocationCheckItems> <l7:Type>OCSP from URL</l7:Type> <l7:Url>http://foo.west.dev.xxxx.com:80</l7:Url> <l7:AllowIssuerSignature>true</l7:AllowIssuerSignature> <l7:TrustedSigners>3c880ed5addceb2e9ef308074f2c353f</l7:TrustedSigners> </l7:RevocationCheckItems>
但是那個 XML 缺少封閉的字串:
<l7:RevocationCheckingPolicy
在開頭和:
</l7:RevocationCheckingPolicy>
我已經看到了一些建議,即在以下內容之前和之后包含封閉字串:
.*
在第二個 sed 中,但是當我嘗試這樣做時,似乎這導致第二個 sed 甚至根本不匹配。
有人可以告訴我如何包含封閉的字串嗎?
謝謝,吉姆
uj5u.com熱心網友回復:
對于它的價值,使用 xml 工具獲取所需的元素作為xmllint.
優點:無論命名空間前綴和 XML 檔案格式如何,都可以作業。
xmllint --xpath '//*[local-name()="RevocationCheckingPolicy"]' test.xml
結果
<l7:RevocationCheckingPolicy id="a60c5a8714b2e519a6c23192cf09ded5" version="20">
<l7:Name>OCSPREVOCATIONVALIDATION</l7:Name>
<l7:DefaultPolicy>true</l7:DefaultPolicy>
<l7:ContinueOnServerUnavailable>false</l7:ContinueOnServerUnavailable>
<l7:DefaultSuccess>false</l7:DefaultSuccess>
<l7:RevocationCheckItems>
<l7:Type>OCSP from URL</l7:Type>
<l7:Url>http://foo.west.dev.xxxx.com:80</l7:Url>
<l7:AllowIssuerSignature>true</l7:AllowIssuerSignature>
<l7:TrustedSigners>3c880ed5addceb2e9ef308074f2c353f</l7:TrustedSigners>
</l7:RevocationCheckItems>
</l7:RevocationCheckingPolicy>
或者,可以使用--shell選項作為
echo 'cat //*[local-name()="RevocationCheckingPolicy"]' | xmllint --shell test.xml
uj5u.com熱心網友回復:
使用sed
$ sed -n '/<l7:RevocationCheckingPolicy/,\|</l7:RevocationCheckingPolicy>|p' input_file > outfile
$ cat outfile
<l7:RevocationCheckingPolicy id=a60c5a8714b2e519a6c23192cf09ded5 version=20>
<l7:Name>OCSPREVOCATIONVALIDATION</l7:Name>
<l7:DefaultPolicy>true</l7:DefaultPolicy>
<l7:ContinueOnServerUnavailable>false</l7:ContinueOnServerUnavailable>
<l7:DefaultSuccess>false</l7:DefaultSuccess>
<l7:RevocationCheckItems>
<l7:Type>OCSP from URL</l7:Type>
<l7:Url>http://foo.west.dev.xxxx.com:80</l7:Url>
<l7:AllowIssuerSignature>true</l7:AllowIssuerSignature>
<l7:TrustedSigners>3c880ed5addceb2e9ef308074f2c353f</l7:TrustedSigners>
</l7:RevocationCheckItems>
</l7:RevocationCheckingPolicy>
uj5u.com熱心網友回復:
建議awk腳本:
awk '/<l7:RevocationCheckingPolicy /,/l7:RevocationCheckingPolicy>/' input.xml
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484392.html
上一篇:LXML-.//{DAV:}responseraiseslxml.etree.XPathEvalError:Invalidexpression
下一篇:如何使用XSLT注釋掉特定元素
