Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 21st, 2008, 12:57 AM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default Filtering XML data

I have the following xml that I want to filter based on specific user's change. One way I solve it to is load the string as xml and perform the filtering. I want to accomplish the ff:

string Filter(string xml,string username);

string Filter(string xml,string[] usernames);

XML sample:
<p>The quick <del user=\"user1\">brown</del><ins user=\"user1\">red</ins><ins user=\"user2\">blue</ins> <del user=\"user3\">fox</del><ins user=\"user3\">red</ins> jumps over the <del user=\"user2\">moon</del><ins user=\"user2\">sun</ins></p>

Sample output Filter(xml,'user1');
<p>The quick <del user=\"user1\">brown</del><ins user=\"user1\">red</ins>fox jumps over the moon</p>

Is this easier to implement using XSLT?How could I accomplish this?
__________________
\"Dont you ever give up!\"
Reply With Quote
  #2 (permalink)  
Old August 21st, 2008, 03:16 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, you can certainly accomplish this in XSLT.

But it might be as simple in whatever programming language you are using (is it C#? I can't quite tell).

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old August 21st, 2008, 03:37 AM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default

Yeah, I already have a working solution for this in C#(As I mentioned,using XML document). Just curious how easily it can be done via XSLT. Can you at least show me simple transformations here?

"Dont you ever give up!"
Reply With Quote
  #4 (permalink)  
Old August 21st, 2008, 04:28 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You didn't mention you had a solution in C# at all.

basically in XSLT you would start with an identity template:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Then add a template to ignore the fields you didn't want to process:

<xsl:template match="*[@user!=$user]"></xsl:template>

This assumes you pass the user in as a parameter to the XSLT template:

<xsl:param name="user"/>

You pass parameters into an XslTransform by passing an XsltArgumentList instance to the Transform method.




/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old August 24th, 2008, 09:04 PM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default

Thanks Sam.
I can't seem to use variable inside the match attribute value in XSL:template node. I tried using xsl:if but was only successful in filtering for a singe user. Problem I have is passing multiple values for multiple users and using them for filtering.

This makes me think, XSL is not the right approach to this. In case this is possible, will I gain performance benefits in terms of code execution?

Let me know your thoughts..
Reply With Quote
  #6 (permalink)  
Old August 25th, 2008, 02:26 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Its very hard to say which will give you the best performance - but if you don't know how to write it in one way then try the other - if the performance then isn't good enough you can look at the other way:

foreach(string user in usernames)
{
  foreach(XmlNode node in doc.SelectNodes("//*[@user='" + user + "']"))
  {
    node.ParentNode.RemoveNode(node);
  }
}

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #7 (permalink)  
Old August 25th, 2008, 03:12 AM
Authorized User
 
Join Date: Jun 2005
Location: , , Philippines.
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default

Ok, thanks a lot.
Part me says, this is a perfect job for XSL transformation but since I don't know how to do it there then I'l just stick to my first solution.

Thanks again Sam for the quick response..I really appreciate it.
Reply With Quote
  #8 (permalink)  
Old August 25th, 2008, 03:24 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I can't seem to use variable inside the match attribute value in XSL:template node

That's a restriction in XSLT 1.0 that is lifted in XSLT 2.0.

If you're stuck with 1.0 you can do

<xsl:template match="*[@user]">
  <xsl:if test="@user != $user">
    ...

To answer your general concerns, what you are doing is well within the capabilities of XSLT (even version 1.0); your problems are merely the beginner's difficulties that occur when learning any new language.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Filtering BlkR XML 1 June 12th, 2008 05:25 PM
Help with filtering xml jconroy XSLT 6 April 6th, 2008 11:33 PM
filtering xml in using XSLT venjamin XSLT 1 October 20th, 2006 04:21 AM
Filtering an xml with xslt Tomi XSLT 4 September 5th, 2006 06:29 AM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM



All times are GMT -4. The time now is 10:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.