Delete lines containing attribute ?
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Delete lines containing attribute ?
Post 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 :
(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.
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>
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.
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: Delete lines containing attribute ?
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
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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Re: Delete lines containing attribute ?
Post 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!
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!
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Re: Delete lines containing attribute ?
Post 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.
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.
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Re: Delete lines containing attribute ?
Post 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)
http://www.dpawson.co.uk/xsl/sect2/onefile.html
But can't seem to do it right...
(sorry for triple posting)
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: Delete lines containing attribute ?
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
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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Re: Delete lines containing attribute ?
Post 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 :
Thank you !
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>
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: Delete lines containing attribute ?
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
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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 6
- Joined: Wed Jun 22, 2011 1:16 am
Re: Delete lines containing attribute ?
Post by BobbyBrown »
Wow.
Thank you again for your answers, quick and crystal clear.
It worked fine !
Thank you.
Keep being awesome.
Thank you again for your answers, quick and crystal clear.
It worked fine !
Thank you.
Keep being awesome.
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service