Page 1 of 1
Delete lines containing attribute ?
Posted: Thu Jun 23, 2011 1:03 am
by BobbyBrown
Hello everyone !
First of all, I really searched for that on the internet, for quite a long time actually, but :
I'm a total noob, and this is just a one-shot operation. I don't think I'll have to do it again in the future (not in the close future anyway), that is why I am asking here.
I don't want to learn how to do it, just understanding it would be awesome.
I suspect this is a quite simple operation, but I can't find how to do it...
So please,
here's my problem :
I have a 7k lines xml file that pretty much looks like this :
Code: Select all
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="smsFaust.xsl"?>
<smses count="7035">
<sms protocol="0" address="+33633789410" date="1267742126000" type="1" subject="null" body="blablablablablablabla" toa="0" sc_toa="0" service_center="null" read="1" status="-1" locked="0" contact_name="Faustine" />
...
<sms protocol="0" address="+33616550848" date="1308571303000" type="1" subject="null" body='Merci,je vais voir si ils peuvent acheter "d'occasion"' toa="145" sc_toa="0" service_center="+33689004000" read="1" status="-1" locked="0" contact_name="Dad" />
</smses>
(yes, it's an xml containing all of my sms, extracted from my android phone with the free app "sms backup and restore")
What I would like to do is
delete every line that does not contain the attribute "contact_name=Faustine".
Any idea how ?
Thank you in advance for your answers.
Re: Delete lines containing attribute ?
Posted: Thu Jun 23, 2011 11:08 am
by adrian
Hello,
Assuming you're using Oxygen, you can do this in the Find/Replace dialog: Find -> Find/Replace(CTRL+F).
Text to find: .*
Replace with: <leave empty>
XPath: //sms[@contact_name!='Faustine']
Options:
Regular expression
Dot matches all
Press Find All to test if it matches the elements that you want removed. If the results seem fine, press Replace All and they will be removed.
As you have probably figured out, the XPath does the actual work, it matches all the sms elements which have a contact_name attribute different than Faustine. Note that if they don't have a contact_name attribute they will not be removed.
Note that this doesn't remove the lines entirely, just the sms elements and their entire content, the empty lines will remain there.
To remove all the empty lines(or lines with spaces) use:
Text to find: ^\s*$\n
Replace with: <leave empty>
XPath: <leave empty>
Obviously this could be done in a single find/replace step, but it would complicate the expressions needlessly and make them more difficult to grasp.
If you want to do this repeatedly then it's probably better to write a stylesheet. But since you said it's a one time thing, this is the fastest way.
Regards,
Adrian
Re: Delete lines containing attribute ?
Posted: Thu Jun 23, 2011 12:56 pm
by BobbyBrown
Hey,
thanks! I am actually using oXygen.
I had stumbled upon regular expressions, but didn't know how to use them.
I'll try this when I come back home!
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 12:39 am
by BobbyBrown
Hey,
Thanks again for your quick and complete answer.
That worked perfectly well !
And I even kinda understood the process (digging about in the internet).
(Now if you had some other magic formula to combine my xml with an xls - I read about xtls but well, same as my first post - that would be pretty awesome)
(and I would grant you my full gratitude, and even maybe a little something if you will, and if you pm me a geographical address to reach you)
Thanks again for the awesomeness anyways.
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 11:56 am
by BobbyBrown
Found this :
http://www.dpawson.co.uk/xsl/sect2/onefile.html
But can't seem to do it right...
(sorry for triple posting)
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 12:15 pm
by adrian
Hi,
What do you want to accomplish by combining these files(XML and XSL)?
Can you show me small samples of the two files that you want to combine?
Regards,
Adrian
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 1:15 pm
by BobbyBrown
Just wanted to get one single file instead of two.
Also, (and that would be the last thing I ask for) how could I delete the lines not containing
both the attributes "contact_name=x" and "contact_name=y" please ? I know you gave me the first half just before, so I suppose it's just a matter of changing the xpath ? Tried a couple of things that didn't work.
My xml looks exactly like what I quoted in code style in my first post.
Here's what my xsl looks like :
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://android.riteshsahu.com">
<xsl:template match="/">
<html>
<head>
<style type="text/css">
body
{
font-family:arial,sans-serif;
color:#000;
font-size:13px;
color:#333;
}
table
{
font-size:1em;
margin:0 0 1em;
border-collapse:collapse;
border-width:0;
empty-cells:show;
}
td,th
{
border:1px solid #ccc;
padding:6px 12px;
text-align:left;
vertical-align:top;
background-color:inherit;
}
th
{
background-color:#dee8f1;
}
</style>
</head>
<body>
<h2>SMS Messages</h2>
<table>
<tr>
<th>Type</th>
<th>Number</th>
<th>Contact</th>
<th>Date</th>
<th>Message</th>
</tr>
<xsl:for-each select="smses/sms">
<tr>
<td>
<xsl:if test="@type = 1">
Received
</xsl:if>
<xsl:if test="@type = 2">
Sent
</xsl:if>
</td>
<td><xsl:value-of select="@address"/></td>
<td><xsl:value-of select="@contact_name"/></td>
<td><xsl:value-of select="@date"/><br/><xsl:value-of select="@readable_date"/></td>
<td><xsl:value-of select="@body"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Thank you !
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 3:39 pm
by adrian
Now I understand what you want. You want to transform the XML with the XSL and obtain a HTML document.
Follow these steps to create and execute a transformation scenario in Oxygen:
- Open the XML file and from the main menu invoke Document -> Transformation -> Configure Transformation Scenario(there's a corresponding action in the toolbar).
- Make sure the scenario type is "XML transformation with XSLT" and click 'New' to create a new scenario:
1. Give it an appropriate name
2. Leave the 'XML URL' field to its default(${currentFileURL})
3. In the XSL URL field browse and select your stylesheet.
4. If you have an XSLT 2.0 stylesheet choose from the transformer combo Saxon-PE, otherwise you can leave it to Saxon6.5.5(your stylesheet is v1.0).
5. You can further tune the Output. e.g in the 'Save as' field you can specify: ${cfd}/${cfn}-out.html which translates into <current-file-directory>/<current-filename>-out.xml
The Save as field must refer a single file, NOT an output directory.
6. Enable the Open in browser option if you want to see the result in your web browser.
7. OK in all dialogs.
- Run the transformation scenario: Document -> Transformation -> Apply Transformation Scenario(there's a corresponding action in the toolbar) and you will see the HTML file in your browser.
Regards,
Adrian
Re: Delete lines containing attribute ?
Posted: Fri Jun 24, 2011 6:37 pm
by BobbyBrown
Wow.
Thank you again for your answers, quick and crystal clear.
It worked fine !
Thank you.
Keep being awesome.