cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From christoph <e0528...@student.tuwien.ac.at>
Subject Re: defining parameters from xml
Date Tue, 09 Oct 2007 18:36:22 GMT
Hi,
thanks for the great explanation. Thats exactly what I was looking for.

For other people who use this:
var firstnames = xquery.getElementsByTagName('firstname'); 
should be 
var firstnames = xml.getElementsByTagName('firstname'); 

everything else works like a charme
regards christoph

On Sunday 07 October 2007 23:43:27 Josh2007 wrote:
> You can use flowscript to extract firstname and lastname from your xml and
> send their values back to your sitemap as parameters.
>
> If you query your database and get an xml with firstname and lastname:
>
>
>             <map:match pattern="query-database">
>                 <map:generate type="xquery" src="your-xquery-file.xq"/>
>                 <map:serialize type="xml"/>
>             </map:match>
>
> then you can use a flowscript to extract the required values from the xml:
>
> in your sitemap, you create your map:flow
> <map:flow language="javascript">
>         <map:script src="dataXMLtoXmap.js"/>
>     </map:flow>
>
> you also need to call the required function in your flowscript from your
> pipeline:
>
> <map:match pattern="get-firstname-lastname-in-xmap">
>                 <map:call function="getFirstnameLastnameInXmap"/>
>             </map:match>
>
> Then you create your flowscript named "dataXMLtoXmap.js" in your directory:
>
> // loadDocument() reads in an XML file and returns a DOM Document.
> function loadDocument(uri) {
> var parser = null;
> var source = null;
> var resolver = null;
> try {
> parser =
> cocoon.getComponent(Packages.org.apache.excalibur.xml.dom.DOMParser.ROLE);
> resolver =
> cocoon.getComponent(Packages.org.apache.cocoon.environment.SourceResolver.R
>OLE); source = resolver.resolveURI(uri);
> var is = new Packages.org.xml.sax.InputSource(source.getInputStream());
> is.setSystemId(source.getURI());
> return parser.parseDocument(is);
> } finally {
> if (source != null) resolver.release(source);
> if (parser != null) cocoon.releaseComponent(parser);
> if (resolver != null) cocoon.releaseComponent(resolver);
> }
> }
>
> // getText() to get text of the node
> function getText(node){
>    var text = "";
>    if(node.getFirstChild() != null) text =
> node.getFirstChild().getNodeValue();
>    return text;
> }
>
> function getFirstnameLastnameInXmap() {
>   // retrieve the XML from the pipeline
>   var xml= loadDocument("cocoon:/query-database");
>
>   // get firstname
>   var firstnames = xquery.getElementsByTagName('firstname'); // get all
> firstnames
>   var firstname = firstnames.item(firstnames.getLength() - 1); // get only
> the first firstname
>
>   // get lastname
>   var lastnames = xquery.getElementsByTagName('lastname'); // get all
> lastnames
>   var lastname = lastnames.item(lastnames.getLength() - 1); // get only the
> first lastname
>
>   // get required content (nodeValue)
>   var firstnameContent = getText(firstname);
>   var lastnameContent = getText(lastname);
>
>   // return to chosen sitemap with firstname, lastname as parameters for
> further sitemap processing
>   var parameters = null;
>   parameters = {
>       "firstname": firstnameContent,
>       "lastname": lastnameContent,
>     };
>   cocoon.sendPage("insertSQL", parameters);
> }
>
> Then, in your sitemap insertSQL:
>
> <map:match pattern="insertSQL">
>    <map:generate type="jx" src="jx/insertArtistTrackSQL.jx">
>      <map:parameter name="firstname" value="{flow-attribute:firstname}"/>
>      <map:parameter name="lastname" value="{flow-attribute:lastname}"/>
>    </map:generate>
>  <map:serialize type="xml" />
>  </map:match>
>
> Now, each time you will match the pattern "get-firstname-lastname-in-xmap",
> it will call the flowscript which in return will read "query-database" to
> get the xml content, extract the required values and send them to your
> sitemap insertSQL.
>
> Regards,
>
> Josh
>
> Christoph Klocker-2 wrote:
> > Hi,
> > how can I define a parameter from an xml.
> >
> > lets say I query  a database which delivers me a firstname an a lastname,
> > so I
> > would get.
> > <xml>
> >   <firstname>alex</firstname>
> >  <lastname>foo</lastname>
> > </xml>
> >
> > How do I set this names as parameters (global or locally in a sitemap) so
> > I
> > could use it like
> >
> > <map:match pattern="insertSQL">
> >    <map:generate type="jx" src="jx/insertArtistTrackSQL.jx">
> >      <map:parameter name="firstname"
> > value="cocoon.parameters.firstname"/> <map:parameter name="lastname"
> > value="cocoon.parameters.lastname"/> </map:generate>
> >  <map:serialize type="xml" />
> >  </map:match>
> >
> > thanx
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> > For additional commands, e-mail: users-help@cocoon.apache.org



Mime
View raw message