Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Filtering with xsl on more parameters

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent