<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Search with XPath in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71699#M17904</link>
    <description>&lt;P&gt;Hi I am having a problem searching an xml formated event. So basically I have an event that looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" ?&amp;gt;
&amp;lt;Products&amp;gt;
  &amp;lt;Product name="CodeAnalyzer" version="2"/&amp;gt;
  &amp;lt;Product name="ScmKitCommon" version="2"&amp;gt;
    &amp;lt;Component name="ScmNantTasks" version="2"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitInternal" version="1"&amp;gt;
    &amp;lt;Component name="ScmToolsProjectConfiguration" version="1"/&amp;gt;
    &amp;lt;Component name="StateObjects" version="1"/&amp;gt;
    &amp;lt;Component name="XsdMaint" version="1"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitProduction" version="2.0.0.9"/&amp;gt;
&amp;lt;/Products&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This event was indexed from a script and not an actual xml file, so I don't know if that makes a difference. What I would like is to list all the product names for this event. I have something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | dedup 1 host | xpath "//Products/Product/@name" outfield=name | table name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But all it lists is CodeAnalyzer. I also changed the commands.conf file in the search app to but nothing changed:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; [xpath]
 supports_multivalues = true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Actually when I tried an xpath that is completely wrong, I still got the same result. What am I missing, how is xpath supposed to be used?&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Sat, 16 Oct 2010 04:38:31 GMT</pubDate>
    <dc:creator>gallantalex</dc:creator>
    <dc:date>2010-10-16T04:38:31Z</dc:date>
    <item>
      <title>Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71699#M17904</link>
      <description>&lt;P&gt;Hi I am having a problem searching an xml formated event. So basically I have an event that looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" ?&amp;gt;
&amp;lt;Products&amp;gt;
  &amp;lt;Product name="CodeAnalyzer" version="2"/&amp;gt;
  &amp;lt;Product name="ScmKitCommon" version="2"&amp;gt;
    &amp;lt;Component name="ScmNantTasks" version="2"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitInternal" version="1"&amp;gt;
    &amp;lt;Component name="ScmToolsProjectConfiguration" version="1"/&amp;gt;
    &amp;lt;Component name="StateObjects" version="1"/&amp;gt;
    &amp;lt;Component name="XsdMaint" version="1"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitProduction" version="2.0.0.9"/&amp;gt;
&amp;lt;/Products&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This event was indexed from a script and not an actual xml file, so I don't know if that makes a difference. What I would like is to list all the product names for this event. I have something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | dedup 1 host | xpath "//Products/Product/@name" outfield=name | table name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But all it lists is CodeAnalyzer. I also changed the commands.conf file in the search app to but nothing changed:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; [xpath]
 supports_multivalues = true
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Actually when I tried an xpath that is completely wrong, I still got the same result. What am I missing, how is xpath supposed to be used?&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 16 Oct 2010 04:38:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71699#M17904</guid>
      <dc:creator>gallantalex</dc:creator>
      <dc:date>2010-10-16T04:38:31Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71700#M17905</link>
      <description>&lt;P&gt;i used your example (saved it as new.xml) to run a quick check:  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;xpath new.xml "//Products/Product/@name"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;here is the result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;mismatched tag at line 13, column 4, byte 462:
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitProduction" version="2.0.0.9"/&amp;gt;
  &amp;lt;/Product&amp;gt;
===^
&amp;lt;/Products&amp;gt;
 at /System/Library/Perl/Extras/5.10.0/darwin-thread-multi-2level/XML/Parser.pm line 187
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Your xml is wrong, and xpath cannot parse it, and hence you get  nothing from splunk, same as if you were running an xpath that is completely wrong..&lt;/P&gt;

&lt;P&gt;remove the last &lt;CODE&gt;&amp;lt;/Product&amp;gt;&lt;/CODE&gt; right before the     &lt;CODE&gt;&amp;lt;/Products&amp;gt;&lt;/CODE&gt; line and test your xpath once again:&lt;BR /&gt;
in other words your xml should be:  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" ?&amp;gt;
&amp;lt;Products&amp;gt;
  &amp;lt;Product name="CodeAnalyzer" version="2"/&amp;gt;
  &amp;lt;Product name="ScmKitCommon" version="2"&amp;gt;
    &amp;lt;Component name="ScmNantTasks" version="2"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitInternal" version="1"&amp;gt;
    &amp;lt;Component name="ScmToolsProjectConfiguration" version="1"/&amp;gt;
    &amp;lt;Component name="StateObjects" version="1"/&amp;gt;
    &amp;lt;Component name="XsdMaint" version="1"/&amp;gt;
  &amp;lt;/Product&amp;gt;
  &amp;lt;Product name="ScmKitProduction" version="2.0.0.9"/&amp;gt;
&amp;lt;/Products&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;~&lt;BR /&gt;
Hope this helps &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;BR /&gt;
.gz&lt;/P&gt;

&lt;P&gt;ps: here is the result for the above xpath in the cli:  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;gzaimi@bigmac ~/Testing/logs xpath new.xml "//Products/Product/@name"
Found 4 nodes:
-- NODE --
 name="CodeAnalyzer"-- NODE --
 name="ScmKitCommon"-- NODE --
 name="ScmKitInternal"-- NODE --
 name="ScmKitProduction"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 16 Oct 2010 05:34:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71700#M17905</guid>
      <dc:creator>Genti</dc:creator>
      <dc:date>2010-10-16T05:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71701#M17906</link>
      <description>&lt;P&gt;If your XML is always broken in the same way, it would be possible to fix it with &lt;CODE&gt;rex&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;I haven't tested this, but this should get you started:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex mode=sed "s/(&amp;lt;Product [^&amp;gt;]+\/&amp;gt;)\s*&amp;lt;\/Product&amp;gt;/$1/g" | xpath "//Products/Product/@name" outfield=name | ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;However, I suppose the question should be asked, do you ever have any other "name" fields within your data?  If not, a much simplier approach would be this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; ... | rex max_match=100 "&amp;lt;Product name=([\"'])(?&amp;lt;name&amp;gt;\w+(\1)" | table name
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You may need to use something more complicated that "&lt;CODE&gt;\w+&lt;/CODE&gt;", but that's the idea.&lt;/P&gt;</description>
      <pubDate>Sat, 16 Oct 2010 08:17:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71701#M17906</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2010-10-16T08:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71702#M17907</link>
      <description>&lt;P&gt;Good find, but that was just my mistake when I posted the question. I removed a bunch of other Product nodes to shorten the example and was a little careless apparently.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Oct 2010 00:14:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71702#M17907</guid>
      <dc:creator>gallantalex</dc:creator>
      <dc:date>2010-10-19T00:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71703#M17908</link>
      <description>&lt;P&gt;Thanks for the suggestion. I got the results I was looking for using this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex max_match=100 "&amp;lt;Product name=(?&amp;lt;name&amp;gt;.*?) version" | table name host
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I tried to use xpath again and figured out the problem. First of all the xml tag was causing the xpath expression to fail. Once I removed it from my scripts output, my xpath expressions worked at times.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;?xml version="1.0" ?&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Secondly, only xpath expressions that began with '//' worked. So something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | xpath "//Products/Product/@name"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;would get me the right attribute stored in xpath field. But&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | xpath "/Products/Product/@name"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;would not work even though Products is the first element. Also when I use the default value, it seems to overwrite the field even though it exists. Well, I just glad it finally works.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Oct 2010 00:40:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71703#M17908</guid>
      <dc:creator>gallantalex</dc:creator>
      <dc:date>2010-10-19T00:40:23Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71704#M17909</link>
      <description>&lt;P&gt;again, did you try to run xpath on your command line? (not within splunk?)&lt;BR /&gt;
If you want, paste the entire xml, and i can run the test on it, but if it works on cli, it should, and will work in splunk..&lt;/P&gt;</description>
      <pubDate>Tue, 19 Oct 2010 08:40:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71704#M17909</guid>
      <dc:creator>Genti</dc:creator>
      <dc:date>2010-10-19T08:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: Search with XPath</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71705#M17910</link>
      <description>&lt;P&gt;My xpath was correct and the data was structured correctly as well. I used outside programs to double check my xpaths and xml data. That was never the problem.&lt;BR /&gt;
But your responses made me try xpath again and I figured out what the problem was. I will edit my response with what I found out.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Oct 2010 21:52:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-with-XPath/m-p/71705#M17910</guid>
      <dc:creator>gallantalex</dc:creator>
      <dc:date>2010-10-19T21:52:40Z</dc:date>
    </item>
  </channel>
</rss>

