|
|
Hi friends,
I have a simple problem. I tried my best but couldn't find a way to do. hope you people will know it. I want to define a vaiable <xsl:variable> or param <xsl:param> in xsl file then I want to access value of this in .cs file of my project by using c#. Will any of you tell me solution of it? I will be really thankful to you people.
Greetings,
Abubakar
|
|
|
|
|
In your xsl, you define a parameter with <xsl:param> and reference it with the $prefix.
So, suppose you wanted a MyName parameter, you would create it like this:
<xsl:param name="MyName" />
Then, you would reference it via:
<xsl:value-of select="$MyName" />
To reference this from your C#, you need to add parameters:
XsltArgumentList args = new XsltArgumentList();
args.AddParam("MyName", string.Empty, "John Doe");
These parameters are then added into the Xsl transform, then you will use:
xslt.Transform(xpathdoc, args, writer);
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
HI,
Thanks for so quick reply. You didn't get my problem. The code which you sent me I already used that code. it is passing values from c# to XSL by using this but not from XSL to C# which I need;
XsltArgumentList args = new XsltArgumentList();
args.AddParam("MyName", string.Empty, "John Doe");
I explain it you in more details and comprehensively; I have a variable in xsl with name 'size' and it has value 35.I just want to use this value in .cs file by using following statement;
Response.Write(size);
output should be;
35
Waiting for your reply,
Regards,
Abubakar
|
|
|
|
|
Read it using an XPath query then. That's the best way to get values out. BTW - you don't need to use xsl to achieve this.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
HI,
It will be really nice if you give me some of examples of using param or variable in .cs file from XSL. I tried my best but couldnot find any way. please give me some practical example in form of code.
Regards,
Abubakar
|
|
|
|
|
Thinking about it, I'm not sure what you are trying to achieve. Why do you have a variable in the XSL file that you need to read? Are you trying to read the result of a value transformation in XML? If you provide your requirements in more depth then perhaps I can help.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I tried but couldn't find a solution. I try to send you code of XSL file. xsl file name is "test.xsl" and param is defined just like that without commas "<xsl:param name="testParam">TestValue</xsl:param>" please send me c# lines of code to get value of this param in .cs file.
Regards,
Abubakar
|
|
|
|
|
Right, suppose that you have the following xslt file:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="testParam">Test value</xsl:param>
</xsl:stylesheet>
To read this in, you could do the following:
public void WriteToConsole()
{
XmlDocument doc = new XmlDocument();
doc.Load("../../MyTest.xslt");
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
string xsltMgr = "xsl";
string xsltUri = "http://www.w3.org/1999/XSL/Transform";
mgr.AddNamespace(xsltMgr, xsltUri);
try
{
XmlNode node = doc.SelectSingleNode("//xsl:param[@name='testParam']", mgr);
Console.WriteLine(node.InnerText);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thanks a lot for your reply it works fine with static values but still I don't solve my problem as I have dynamic values. I am going to send you code of my xsl param if it is possible please send me code again. I will be greatful to you. here is code;
<xsl:param name="size" select="ceiling(count(.//Category[Title=$subtitle]/Category/MetaData))"/>
Waiting for your reply.
Abubakar
|
|
|
|
|
What is setting $subtitle?
You have an XML document which you are processing the xslt against, then you can apply the XPath query that is in the select statement against the xml, passing in the relevant item for $subtitle. This is the quickest and easiest way to get the value out. I already intimated that the XPath would be a better approach for this problem.
The following XPath solves your problem:
private int GetValue(string xmlFile, string title)
{
XPathDocument doc = new XPathDocument(xmlFile);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(string.Format(
"ceiling(count(//Category[Title='{0}']/Category1/MetaData))", title);
object obj = nav.Evaluate(expr);
int output = 0;
if (obj != null)
{
output = Convert.ToInt32(obj);
}
return output;
}
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Abubakar
Did the sample I suggested solve your problem? If so, it would be nice for you to let people know.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I am Sorry, I havn't check it yet, I hope I will check later of the day then I will inform every one.
Thanks,
Abubakar
|
|
|
|
|
Hi Friends,
First of all Sorry for late I was little busy with other projects. I tried the code which you gave me to use variables in .cs file defined in xsl file but still it is not working. it is giving 0. I used the following code please have a nice look on it and give me some suggestions.
OUTPUT of following code is 0.
string URL = Server.MapPath(Request.ApplicationPath + "/xsl/Videos.xsl");
XPathDocument doc = new XPathDocument(URL);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(string.Format("ceiling(count(.//Category[Title='$subtitle']/Category/MetaData))"));
object obj = nav.Evaluate(expr);
int output = 0;
if (obj != null)
{
Response.Write("Something : " + Convert.ToInt32(obj));
}
Regards,
Abubakar
|
|
|
|
|
First of all, you are loading in the XSL file and not the XML file. Try using the code I suggested against the XML file. As I stated before, you should be using the XML as the source - you are using XSL for transformation only.
Secondly, when you do it, you would want to replace $subtitle with the value of the variable that you want to select - take a look back at my code sample to see how to do it.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
HI,
First I tried with your code friend, but it was giving error with 'title'
Second and most important one I don't have any phiysical XML file I am using webservice to fetch data. I am using xsl only to format it. data is infact coming from database exists in another country. I am only using web service method. Hope you will understand my situation now. If you have solution for it please let me know.
Regards,
Abubakar
|
|
|
|
|
Abubakarsb wrote: First I tried with your code friend, but it was giving error with 'title'
Second and most important one I don't have any phiysical XML file I am using webservice to fetch data. I am using xsl only to format it. data is infact coming from database exists in another country. I am only using web service method. Hope you will understand my situation now.
Errm - you do have an XML file even if it's not physically situated on your hard disk. You don't have to load XML directly from a file, you can load it in from a stream. All you have to do is use the XPathDocument constructor that loads a stream.
The principal still holds true. You have an XML file - where you get it doesn't matter - you use XPath to parse it and extract the information. Just use the same value for title that you were going to use in the XSL parameter.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I am sorry friend, as I am a new bird here in xsl and c# I really don't understand what u want to say It will be really nice If you give me some sample for it I am going to send you my code in .cs file and xsl file; Please modify them and send me right ones. It will be really kind to me.
Code in .cs File;
string URL = Server.MapPath(Request.ApplicationPath + "/xsl/Videos.xsl");
XPathDocument doc = new XPathDocument(URL);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr = nav.Compile(string.Format("ceiling(count(.//Category[Title='$subtitle']/Category/MetaData))"));
object obj = nav.Evaluate(expr);
int output = 0;
if (obj != null)
{
Response.Write("Something : " + Convert.ToInt32(obj));
}
Code in xsl File;
<xsl:variable name="size" select="ceiling(count(.//Category[Title=$subtitle]/Category/MetaData))"/>
Regards,
Abubakar
|
|
|
|
|
No. That's not what you want to do. You have the XML file that you are going to transform - that's what goes into XPathDocument. So if you had the following:
Stream xmlStream = GetStreamFromDataSource();
You would then declare XPathDocument like this:
XPathDocument doc = new XPathDocument(xmlStream);
Then, as I've stated so many times, your XPathExpression will not use $subtitle. This is an XSL parameter. If you were looking for books with the title I Claudius, then you would replace this with:
XPathExpression expr = nav.Compile(string.Format("ceiling(count(.//Category[Title='{0}']/Category/MetaData))", "I Claudius"));
This replaces the {0} placeholder with the text I Claudius.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thanks a lot friend now It is giving error on this line
Stream xmlStream = GetStreamFromDataSource();
I am using following code to get values from webservice;
il.co.besttv.mfw32.Front fm = new il.co.besttv.mfw32.Front();
XmlNode resultNode = fm.fetchCategoryFrontXml(customId, guiId, priceId, vodTreeId);
Regards,
Abubakar
|
|
|
|
|
Hi Pete O'Hanlon,
You didn't reply me yet about this line of code
Stream xmlStream = GetStreamFromDataSource();
This is giving a complie time error.
Regards,
|
|
|
|
|
Excuse me, but I give my time freely to this site and don't have time to waste on people who don't want to help themselves. If you think about the code sample for more than a couple of seconds you will see that it can't possibly know about how to get your XML stream, which is why I put a stub method here (cunningly called GetStreamFromDataSource).
I'm sorry, but you are going to have to actually do some work for yourself and figure this one out yourself. I have spent more time on your problem than I normally would have and I've provided code samples for you to look at. At some point you have to engage your brain and actually do some work for yourself.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I am really sorry for wasting your time. by the way I worked on it 3 hours not couple of seconds. Thanks for comments,
Regards,
Abubakar
|
|
|
|
|
Hi all,
I am working on C# windows applcation..
I want a componet, which will generates the waveform as output..
Hope u understood my requirement please help me as soon as possible
i'd surfed alot, and with no result..plz help
pashi
prashanth,
s/w Engineer,
Syfnosys.
|
|
|
|
|