Fetch records selectively from XML file by passing dynamic value through XSL

This code snippet called on Page_Load shows how to selectively pick records from a XML file based on a dynamic value passed through XSL and doing a transformation in C# . Needs a little more fine tuning.... XsltArgumentList does the trick!

string node = Request.QueryString["country"];
if (node!=null)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(Server.MapPath("users.xml"));
XslTransform docXsl = new XslTransform();
docXsl.Load(Server.MapPath("users.xsl"));

XsltArgumentList xslarg = new XsltArgumentList();
xslarg.AddParam("country", "", node);

docXsl.Transform(docXml, xslarg, Response.Output);
}
else
{
Response.Write("Parameter missing");
}
}



users.xml



<users>
<user>
<name>Anil</name>
<city>Hyderabad</city>
<country>India</country>
</user>
<user>
<name>Moe</name>
<city>New York</city>
<country>USA</country>
</user>
</users>


users.xsl


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="country"></xsl:param>
<xsl:template match="users">

<html><head>
<title>Users</title></head>
<body>

<xsl:for-each select="user">
<xsl:if test="country=$country">

<b><xsl:value-of select="city"></xsl:value-of></b>


</xsl:if>
</xsl:for-each>

</body>
</html>

</xsl:template>
</xsl:stylesheet>



I found the sample on Paging Through XML Records using ASP.NET and XSLT useful.

Comments

Popular posts from this blog

HOW TO Filter Products on Open Food Facts by Country (India) Using DuckDB & Superset

HOW TO Spot AI Trickery in Images