Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 21st, 2008, 12:57 AM
Authorized User
 
Join Date: Jun 2005
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!\"
 
Old August 21st, 2008, 03:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old August 21st, 2008, 03:37 AM
Authorized User
 
Join Date: Jun 2005
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!"
 
Old August 21st, 2008, 04:28 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old August 24th, 2008, 09:04 PM
Authorized User
 
Join Date: Jun 2005
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..
 
Old August 25th, 2008, 02:26 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old August 25th, 2008, 03:12 AM
Authorized User
 
Join Date: Jun 2005
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.
 
Old August 25th, 2008, 03:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.