471,595 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

Using xpath with xslt ?

Hi,

I would like to use xpath with xslt. I know that this
should be possible, but I dont get it ! Here is what I
have tried so far:

====================================
my xml file:
====================================

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
<Category ID="01">
<MainCategory>XML1</MainCategory>
<Description>This is a list my XML
articles.</Description>
<Active>true</Active>
</Category>
<Category ID="02">
<MainCategory>XML2</MainCategory>
<Description>This is a list my XML
articles.</Description>
<Active>true</Active>
</Category>
</CategoryList>
====================================

my xsl file:

====================================

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="strXPath" select="Category"/>
<xsl:template match="/">
<xsl:apply-templates select="$strXPath">
<xsl:sort select="category" order="descending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Category">
<b>
<xsl:value-of select="MainCategory"/><br/>
</b>
</xsl:template>
</xsl:stylesheet>

====================================

my code:

====================================

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="true" %>

<script runat="server">
void Page_Load(object sender, System.EventArgs e){
if(!Page.IsPostBack){
XPathDocument xpathDoc = new XPathDocument
(Server.MapPath("categories.xml"));

XslTransform xslt = new XslTransform();
xslt.Load( Server.MapPath("categories.xsl") );

XsltArgumentList arguments = new XsltArgumentList();

string xpath="CategoryList/Category[@ID='01']";
arguments.AddParam( "strXPath", "", "xpath");

XmlTextWriter writer = new XmlTextWriter
(Response.Output);
xslt.Transform( xpathDoc, arguments, writer, null);
}
}
</script>

Thanks in advance, Sonu Kapoor
Nov 12 '05 #1
1 1374
Sonu Kapoor wrote:
I would like to use xpath with xslt. I know that this
should be possible, but I dont get it ! Here is what I
have tried so far:


In any compiled language you can't pass part of source code as a
parameter and expect it to run. XSLT isn't different, there is no
dynmaic XPath evaluation in XSLT.
Instead, select nodes you want to process using XPathNavigator and pass
them to XSLT as a parameter.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sebastian Petzelberger | last post: by
1 post views Thread by Oleg Paraschenko | last post: by
1 post views Thread by Sergey Dubinets | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.