Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Filtering with xsl on more parameters [Thread Next] Re: Filtering with xsl on more parametersTo: NULL Date: 11/21/2008 4:57:00 PM peter.dewitte@g... wrote: > I want to filter dynamically the information out of subnode with id: > id1 and id3. An other time I will filter the information of subnode > with id: id2, id5 and id3. Is there a way to filter it with xsl, > without adapting your xsl filter. At the moment I filter like this: > > <?xml version="1.0" ?> > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ > Transform"> > <xsl:param name="filter">id1</xsl:param> > <mainNode> > <xsl:for-each select="mainNode/subnode[$filter]"> > <someInfo><xsl: value-of select="someInfo"/></somInfo> > </xsl:for-each> > </mainNode> Sorry, the above does not make much sense as an XSLT stylesheet as you have not shown any xsl:template and as <xsl:for-each select="mainNode/subnode[$filter]"> looks wrong too. Do you have <xsl:for-each select="mainNode/subnode[@id = $filter]"> instead? > Is there enyone who can help me to solve my (little) problem? Which XSLT processor are you using? How do you run the transformation? With XslCompiledTransform and if you run the transformation with code you could pass in a node-set as the parameter value. Here is an example, the C# code to run the transformation is static void Main(string[] args) { Test(new string[] { "id1", "id3" }); Test(new string[] { "id2", "id4", "id5" }); } static void Test(string[] ids) { XslCompiledTransform proc = new XslCompiledTransform(); proc.Load(@"..\..\XSLTFile1.xslt"); XsltArgumentList paramList = new XsltArgumentList(); paramList.AddParam("ids", "", MakeIdsNodeSet(ids)); proc.Transform(@"..\..\XMLFile1.xml", paramList, Console.Out); Console.WriteLine(); Console.WriteLine(); } static XPathNodeIterator MakeIdsNodeSet(IEnumerable<string> ids) { XmlDocument doc = new XmlDocument(); using (XmlWriter writer = doc.CreateNavigator().AppendChild()) { writer.WriteStartElement("root"); foreach (string id in ids) { writer.WriteElementString("id", id); } writer.WriteEndElement(); } return doc.DocumentElement.CreateNavigator().Select("id"); } the stylesheet is <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="ids" select="/.."/> <xsl:output method="xml" indent="yes"/> <xsl:template match="mainNode"> <xsl:copy> <xsl:apply-templates select="subnode[@id = $ids]"/> </xsl:copy> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet> the XML is taken from your post, the output of the C# program is <?xml version="1.0" encoding="ibm850"?> <mainNode> <subnode id="id1"> <someInfo>information_1</someInfo> </subnode> <subnode id="id3"> <someInfo>information_3</someInfo> </subnode> </mainNode> <?xml version="1.0" encoding="ibm850"?> <mainNode> <subnode id="id2"> <someInfo>information_2</someInfo> </subnode> <subnode id="id4"> <someInfo>information_4</someInfo> </subnode> <subnode id="id5"> <someInfo>information_5</someInfo> </subnode> </mainNode> so you can see that the stylesheet outputs those someInfo elements for which the id attribute value was passed in as a parameter. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
