473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

supplying runtime parameter

sp
the xml file i used
<catalog>
<cd>
<title year="1990">Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd>
<title year="1995">Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1995">Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1995">Still got the blues</title>
<artist>Gary Moore</artist>
<price>10.20</price>
</cd>
<cd>
<title year="1998">Eros</title>
<artist>Eros Ramazzotti</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1998">One night only</title>
<artist>Bee Gees</artist>
<price>10.90</price>
</cd>
</catalog>
the output expected
<cd>
<title year="1990">Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
the xsl used
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:for-each select="/catalog/cd[title[@year='1990']]">
<catalog>
<xsl:copy-of select="." />
</catalog>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

in the above xsl i am filtering the xml by hardcoding the value of year
is there any way to pass the filtering parameter as a runtime argument
( i am working in C++ )

by using the transformNode function i am abel to apply the filtering in
XSL but how can i pass the filter parameter runtime

could anyone help in this issue

thanks
praveen

Jan 25 '06 #1
3 1712


sp wrote:
<xsl:for-each select="/catalog/cd[title[@year='1990']]"> in the above xsl i am filtering the xml by hardcoding the value of year
is there any way to pass the filtering parameter as a runtime argument
Yes, your stylesheet can declare a global parameter
<xsl:param name="inputYear" />
then you need to check the API documentation of your XSLT processor on
how to set that parameter before running a transformation e.g. there
will a method alike
xsltProcessor.addParameter('inputYear', '2000')
( i am working in C++ )

by using the transformNode function i am abel to apply the filtering in
XSL but how can i pass the filter parameter runtime


Which processor are you using, some version of Microsoft MSXML?
API docs are here:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/9ddcd728-2646-494a-8fa4-3b68e8c032b7.asp>
Note however that your example does not need a stylesheet at all, a
simple XPath expression evaluation (e.g. selectNodes, selectSingleNode
with MSXML) would give you the result as you do not seem to want to
create any nodes, you simple select one cd element and that can be done
with pure XPath without using XSLT e.g.:
xmlDocument.setProperty('SelectionLanguage', 'XPath');
var someInputYear = '2000';
var cdElement = xmlDocument.selectSingleNode(
'/catalog/cd[title[@year = "' + someInputYear + '"]]');

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 25 '06 #2
This you might find this java utility class helpful ....
I use it to dynamically add a xsl:variable to an xsl transform.
package acme.com;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

public class XSLUtils {
public static InputStream addParameters(InputStream xsl, Map
parameters) {
Document document = null;
try {
document =
DocumentBuilderFactory.newInstance().newDocumentBu ilder().parse(xsl);

Node root = document.getDocumentElement();

Iterator iterator = parameters.keySet().iterator();
while(iterator != null && iterator.hasNext()) {
String name = (String)iterator.next();
String value = (String)parameters.get(name);

Node element = document.createElement("xsl:variable");
NamedNodeMap attributes = element.getAttributes();
Attr nameAttr = document.createAttribute("name");
nameAttr.setNodeValue(name);
attributes.setNamedItem(nameAttr);

Text textNode = document.createTextNode(value);
element.appendChild(textNode);

root.insertBefore(element, root.getFirstChild());
}

// now convert document to an InputStream
return new
ByteArrayInputStream(XMLTransformer.transform(docu ment,
false).getBytes());
} catch(SAXException e) {
LOG.error(e);
} catch(IOException e) {
LOG.error(e);
} catch(ParserConfigurationException e) {
LOG.error(e);
} catch(FactoryConfigurationError e) {
LOG.error(e);
} catch(XMLTransformException e) {
LOG.error(e);
} finally {
StreamUtils.close(xsl);
}

return null;
}
}

Then to use it ...

StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\"
encoding=\"UTF-8\"?>");
sb.append(xml.generateXml()); // result of
xml.generateXml() is a string of XML
xmlStream = new
ByteArrayInputStream(sb.toString().getBytes("UTF-8"));

InputStream xslStream =
getServletContext().getResourceAsStream("/WEB-INF/xsl/joborder-email-template.xsl");
xslStream = XSLUtils.addParameters(xslStream, params); //
xsl:variables added here.

// apply transform to XML to generate HTML
String html = DoWhatYouHaveToTo.transform(xmlStream, xslStream);

// add xsl variable assignments
xslStream = XSLUtils.addParameters(xslStream, params);

// apply transform to XML to generate HTML
String html = XSLTransformer.transform(xmlStream,
xslStream);

Jan 26 '06 #3
hi,

you could use RefleX for this purpose :
http://reflex.gforge.inria.fr/

a very simple script can be done like this :

<?xml version="1.0" encoding="iso-8859-1"?>
<xcl:active-sheet xmlns:xcl="http://www.inria.fr/xml/active-tags/xcl">
<xcl:transform output="/path/to/file.html"
source="/path/to/file.xml" stylesheet="/path/to/stylesheet.xsl">
<xcl:param name="inputYear" value="1990"/>
</xcl:transform>
</xcl:active-sheet>

inside your stylesheet, just declare the parameter :
<xsl:param name="inputYear"/>
and change the hard-coded value where expected :
<xsl:for-each select="/catalog/cd[title[@year=$inputYear]]">

notice that for this example, you don't need a stylesheet, you can get
directly the output like this :
<?xml version="1.0" encoding="iso-8859-1"?>
<xcl:active-sheet xmlns:xcl="http://www.inria.fr/xml/active-tags/xcl">
<xcl:parse name="myCatalog" source="/path/to/file.xml"/>
<xcl:transform output="/path/to/file.html" source="{
$myCatalog/catalog/cd[title/@year = '1990'] }"/>
</xcl:active-sheet>

the difference is that there is no stylesheet specified, so it is a
simple copy of the node selected

notice that such scripts could be launched in batch mode as well as in
Web application, and you can of course pass parameters from the
environment (batch) or from the URL query string (HTTP)

have a look at the RefleX web site

sp wrote:
the xml file i used
<catalog>
<cd>
<title year="1990">Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd>
<title year="1995">Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1995">Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1995">Still got the blues</title>
<artist>Gary Moore</artist>
<price>10.20</price>
</cd>
<cd>
<title year="1998">Eros</title>
<artist>Eros Ramazzotti</artist>
<price>9.90</price>
</cd>
<cd>
<title year="1998">One night only</title>
<artist>Bee Gees</artist>
<price>10.90</price>
</cd>
</catalog>
the output expected
<cd>
<title year="1990">Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
the xsl used
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="/">
<xsl:for-each select="/catalog/cd[title[@year='1990']]">
<catalog>
<xsl:copy-of select="." />
</catalog>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

in the above xsl i am filtering the xml by hardcoding the value of year
is there any way to pass the filtering parameter as a runtime argument
( i am working in C++ )

by using the transformNode function i am abel to apply the filtering in
XSL but how can i pass the filter parameter runtime

could anyone help in this issue

thanks
praveen

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !
Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Raj | last post by:
hi, How to pass a Two Date parameter to a report at runtime using VB .NET 1. How do we pass multiple values eg. Startdate, EndDate to crystal reports parameter. 2. I have a report to...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
1
by: Bill Nguyen | last post by:
Report source is an SQLSERVER 2K store procedure. VB.NET application. Report created by CR 8.5. At runtime, I still had to click "CANCEL" to bypass the parameter prompts before the report display...
12
by: leaf | last post by:
Hi, How to call function at runtime, based on a struct that contains the information for the function call: struct func_to_call { int function_id; // function id to call unsigned int nparams;...
3
by: Developer in California | last post by:
I am working on developing a generic Web framework using Master Pages in ASP.NET 2.0. What I have done is created a PageRenderer class which has a public method which will retrieve the path of the...
2
by: Arif Khan | last post by:
Hi, I want to write down a function entry point logging method, allowing logging of passed in parameters values along with function name and parameter names. I know I can get the parameter name and...
2
by: planetthoughtful | last post by:
Hi All, I have an ASP.NET page that displays a GridView control based on an ObjectDataSource control. I'm wondering if it's possible to base the ObjectDataSource in question on a parameter...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
3
by: =?Utf-8?B?R3JhaGFt?= | last post by:
I've added 2 tracking services to the wf runtime; one is the standard SqlTrackingService: trackingService = new SqlTrackingService(<trackingConnectionString>); <workflow...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.