2016-03-21 23 views
0


特定のコンテンツに対して特定のXMLファイルを解析する必要があります。残念ながら私はxmllintをxpathなしで自分のシステムに持っているだけです(私は他のソースをインストール/更新することはできません)。 XMLが含まれます:最後にxmlをbashで解析する

<?xml version="1.0"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
    <SOAP-ENV:Body> 
    <CreateIncidentResponse xmlns="http://schemas.hp.com/SM/7" xmlns:cmn="http://schemas.hp.com/SM/7/Common" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" message="Success" returnCode="0" schemaRevisionDate="2016-02-16" schemaRevisionLevel="2" status="SUCCESS" xsi:schemaLocation="http://schemas.hp.com/SM/7 /Incident.xsd"> 
     <model> 
     <keys> 
      <IncidentID type="String">IM0832268</IncidentID> 
     </keys> 
     <instance recordid="IM0832268 - Paul test 3 incident via soap" uniquequery="number=&quot;IM0832268&quot;"> 
      <IncidentID type="String">IM0832268</IncidentID> 
      <Category type="String">request for change</Category> 
      <OpenTime type="DateTime">2016-03-18T16:06:28+00:00</OpenTime> 
      <OpenedBy type="String">Harlass, Alexander</OpenedBy> 
      <Priority type="String">4</Priority> 
      <Urgency type="String">medium</Urgency> 
      <UpdatedTime type="DateTime">2016-03-18T16:06:28+00:00</UpdatedTime> 
      <AssignmentGroup type="String">TS3-AOS</AssignmentGroup> 
      <Description type="Array"> 
      <Description type="String">RH test incident description via soap row 1</Description> 
      <Description type="String">RH test incident description via soap row 2</Description> 
      </Description> 
      <Contact type="String">Harlass, Rudolf</Contact> 
      <Title type="String">Paul test 3 incident via soap</Title> 
      <TicketOwner type="String">INTEGRATION.OVO</TicketOwner> 
      <UpdatedBy type="String">INTEGRATION.OVO</UpdatedBy> 
      <Status type="String">Open</Status> 
      <Area type="String">it products</Area> 
      <Subarea type="String">utilization</Subarea> 
      <ProblemType type="String">request for change</ProblemType> 
      <Impact type="String">low</Impact> 
      <Service type="String">PI Automation and Orchestration Service</Service> 
      <VIP type="Boolean">false</VIP> 
      <TargetResolutionDate type="DateTime">2016-03-25T15:00:00+00:00</TargetResolutionDate> 
      <SOD type="String">OML</SOD> 
      <SourceId type="String">4711</SourceId> 
      <UserIncident type="Boolean">false</UserIncident> 
      <AlertId type="String">4712</AlertId> 
      <MonitoredId type="String">MI4713</MonitoredId> 
     </instance> 
     </model> 
     <messages> 
     <cmn:message type="String">Audit Record successfully recorded and added.</cmn:message> 
     </messages> 
    </CreateIncidentResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

を私はこのような出力必要があります:

Create SUCCESS 
Messages: 
    Audit Record successfully recorded and added. 
Incident ID: IM0832268 
    Status: Open 
    Severity: 4 
    Brief Description: RH test incident description via soap row 1 
    Opened by: integration.ovo 
    Opened time: March 20, 2016 11:54:08 PM CET 

を私は出力を含む文字列を作成する方法を知っていますが、残念ながら、私はそれはありませんよsedまたは類似のツールに精通している。
xmlから必要な文字列を抽出する方法についてのヘルプがあれば幸いです。事前に
おかげで

+0

システムに[xsltproc](http://xmlsoft.org/XSLT/xsltproc.html)がありますか? – Tomalak

+0

残念ながら、 – user3322838

+0

(このコメントに従ってください。これは長期的な解決策ではありません)何もインストールすることはできませんが、通常はバイナリ(&依存関係)をコンパイルしてパスに書き込み権限があります。最悪の場合、 '/ tmp'は読み書き可能です。 'xmllint'の新しいバージョンをそのパスにコピーしてそこから実行することができます。 – anishsane

答えて

1

ほとんどのシステムはpythonまたはperlまたは実際のXML処理能力を持っているいくつかの他の言語が含まれています。これは、bashのXMLの大部分からきれいにフォーマットされたレポートを生成しようとするより良い解決策を生むでしょう。これを言いましたが、このデータをbashで抽出するためのアイディアがいくつかあります。あなたはこのようawkを使用して値を取得することができます

<IncidentID type="String">IM0832268</IncidentID> 

(あなたのデータを仮定すると、ファイルにdata.xmlと呼ばれている):

awk -F'[<>]' '/IncidentID/ {print $3}' data.xml 

TJE awkフィールドを設定します-F'[<>]'のような文字列が与えられ

区切り文字は<または>のいずれかにする必要があります。したがって、指定した行は次のようにフィールドに分割されます。

あなたは、これらは常に同じになります知っている場合は、あなただけの最初のものを取ることができ

IM0832268 
IM0832268 

| 1 | 2      | 3  | 4  | 5 | 
| |IncidentID type="String"|IM0832268|/IncidentID| | 

上記の例では、実際には2つの行を(2個のIncidentIDタグがあなたのデータであるため)を返します:

awk -F'[<>]' '/IncidentID/ {print $3; exit}' data.xml 

は次のような行から属性を抽出するには:

<CreateIncidentResponse xmlns="http://schemas.hp.com/SM/7" xmlns:cmn="http://schemas.hp.com/SM/7/Common" xmlns:xmime="http://www.w3.org/2005/05/xmlmime" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" message="Success" returnCode="0" schemaRevisionDate="2016-02-16" schemaRevisionLevel="2" status="SUCCESS" xsi:schemaLocation="http://schemas.hp.com/SM/7 /Incident.xsd"> 

あなたは、まずこのように、属性ごとに1行に分割することができます

grep '<CreateIncidentResponse' data.xml | tr ' ' '\n' 

あなたを与えるだろうどの:あなたは、属性値を抽出するためにawkに渡すことができます

<CreateIncidentResponse 
xmlns="http://schemas.hp.com/SM/7" 
xmlns:cmn="http://schemas.hp.com/SM/7/Common" 
xmlns:xmime="http://www.w3.org/2005/05/xmlmime" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
message="Success" 
returnCode="0" 
schemaRevisionDate="2016-02-16" 
schemaRevisionLevel="2" 
status="SUCCESS" 
xsi:schemaLocation="http://schemas.hp.com/SM/7 
/Incident.xsd"> 

を。

grep '<CreateIncidentResponse' data.xml | tr ' ' '\n' | 
awk -F'"' '/message/ {print $2}' 

もたらすであろう:

Success 

がうまくいけば、これはあなたが始めるのに十分です 例えば、 message属性の値を取得します。

+0

+1の最初の段落に+1します。残りの答えに表示された脆弱なハックで正しい最初の段落を壊すために-1を返します。 – kjhughes

+0

「脆弱なハック」、私はそれが好きです。私はTシャツを注文する必要があると思う。 – larsks

+1

:-)「私の脆弱なハッキングは、あなたのプロダクションコードよりも堅牢です。 – kjhughes