使用Splunk-使用xpath从日志文件中的xml数据中提取字段

问题描述 投票:0回答:1

我正在使用Splunk从包含在日志文件中的xml数据中提取许多字段。因此,将搜索限制为主要是xml文件,我以此开始搜索:sourcetype =“此处为类型名称”“ RULE”

此返回:

0123459 TripMessage.createMessage MsgSource <?xml version="1.0" encoding="UTF-8"?>
<tmsTrip xmlns="http://ground.fedex.com/schemas/linehaul/trip" xmlns:ns2="http://ground.fedex.com/schemas/linehaul/TMSCommon">

...

文件很大。这是其中的一部分。

<?xml version="1.0" encoding="UTF-8"?>
<tmsTrip xmlns="http://ground.fedex.com/schemas/linehaul/trip" xmlns:ns2="http://ground.fedex.com/schemas/linehaul/TMSCommon">
   <recordType>PURCHASEDLINEHAUL</recordType>
   <eventType>APPROVE</eventType>
   <tripId>116029927</tripId>
   <legId>104257037</legId>
   <tripNumber>104257037</tripNumber>
   <tripLegNumber>1</tripLegNumber>
   <updatedDateGMT>2020-02-20T21:53:39.000Z</updatedDateGMT>
.... more lines here that are not important
     <purchasedCost>
      <purchasedCostTripSegment>
         <purchCostReference>1587040</purchCostReference>
         <carrier>FXTR</carrier>
         <vendorType>DRAY</vendorType>
         <billingMethod>RULE</billingMethod>
         <carrierTrailerType>PZ1</carrierTrailerType>
         <origin>
            <ns2:numberCode>923</ns2:numberCode>
            <ns2:locAbbr>RLTO</ns2:locAbbr>
            <ns2:address1>330 RESOURCE DRIVE</ns2:address1>
            <ns2:address2>LH PHONE 877-851-3543</ns2:address2>
            <ns2:daylightSavingsFlag>true</ns2:daylightSavingsFlag>
         </origin>

此查询在日志文件中选择xml部件文本,并且提取了一些字段,我可以将其添加到表中。 (不包括源和源类型。)

| xmlkv | table purchCostReference, eventType, carrier, billingMethod

但是需要更多字段作为xml数据内的子元素。其中之一是numberCode。我正在尝试使用xpath提取这些其他字段。

| xmlkv | xpath
"//tmsTrip/purchasedCost/purchasedCostTripSegment/origin/ns2:numberCode" outfield=Origin | table purchCostReference, eventType, carrier, billingMethod, Origin

但是当我将字段添加到表中时,没有返回原始数据。没有错误。 “来源”列为空。enter image description here

UPDATE我认为问题是我需要添加field参数。 xml文件位于日志文本文件中。我限制了搜索以获取xml文件,而不仅限于xml。因此,我认为xpath正在与非xml的其他文本作斗争。

xpath splunk
1个回答
0
投票

没有看到事件数据的其余部分,我不能说为什么xpath命令不起作用。

但是,作为解决方法,请尝试以下方法代替xmlkvxpath命令

| rex field=_raw "numberCode>(?<Origin>\d+)</"

这在混合的文本和xml事件中应该可以正常工作

© www.soinside.com 2019 - 2024. All rights reserved.