extract from xml file

A

ash

My input file is something like this

<DB>
<E>
<TAG1><![DATA[ "ep87" ]]></TAG1>
<TAG2><![DATA[ "0" ]]></TAG2>
<TAG3><![DATA[ "8.8.8.7" ]]></TAG3>
</E>
<E>
<TAG1><![DATA[ "EP7-1" ]]></TAG1>
<TAG2><![DATA[ "0" ]]></TAG2>
<TAG3><![DATA[ "192.168.5.1" ]]></TAG3>
</E>

</DB>

I want to able to search on string ep87 and get the output like

<E>
<TAG1><![DATA[ "ep87" ]]></TAG1>
<TAG2><![DATA[ "0" ]]></TAG2>
<TAG3><![DATA[ "8.8.8.7" ]]></TAG3>
</E>

What tool or script can I use?
Thank You..
 
P

Patrick TJ McPhee

% My input file is something like this
%
% <DB>
% <E>
% <TAG1><![DATA[ "ep87" ]]></TAG1>

This should be [CDATA[

[...]

% I want to able to search on string ep87 and get the output like
%
% <E>
% <TAG1><![DATA[ "ep87" ]]></TAG1>
% <TAG2><![DATA[ "0" ]]></TAG2>
% <TAG3><![DATA[ "8.8.8.7" ]]></TAG3>
% </E>

% What tool or script can I use?

You could do this with xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="xml"/>

<xsl:param name="ep87" select="'ep87'"/>

<xsl:template match="text()"/>

<xsl:template match="TAG1[contains(., $ep87)]">
<xsl:copy-of select="parent::*"/>
</xsl:template>
</xsl:stylesheet>

most (all?) xslt tools will let you set a parameter from the command-line.
For instance, with xsltproc, you can run the above with

xsltproc --param ep87 "'EP7-1'" query.xsl data.xml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top