XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
rajat nairSubject: Using same xquery for 2 xml files
Author: rajat nair
Date: 30 Jan 2008 04:55 PM
Originally Posted: 30 Jan 2008 04:54 PM
Hi,
I have 2 xml files,both in oag format and with non overlapping tags.I wanted to use a single xquery file and do something like this,

<PurchaseOrderLine>
<LineNumber>
{doc('file:///c:/Tp_request.xml')/TradePowerRequest/RequestDetail/ItemLineNumber/text()}
</LineNumber>
{
for $RequestDetail in doc('file:///c:/Tp_request.xml')/TradePowerRequest/RequestDetail
return
<items>
{
if( $RequestDetail/ItemNumber/@source = "UI" ) then
<item1>

{doc('file:///c:/AwailResponse.xml')/DetailAvailability/DisplaySequence/text()}

</item1>

else
()
}
</items>
}
</PurchaseOrderLine>
Is this possible to run the same xquery for 2 different xmls and produce a merged new xml?.

Rajat

Postnext
Minollo I.Subject: Using same xquery for 2 xml files
Author: Minollo I.
Date: 31 Jan 2008 09:04 AM
Absolutely, it is possible. Your XQuery looks syntactically fine at a first glance; are you having specific problems?

Postnext
rajat nairSubject: Using same xquery for 2 xml files
Author: rajat nair
Date: 31 Jan 2008 12:29 PM
Originally Posted: 31 Jan 2008 10:02 AM
Hi,
Thanks for the prompt reply,i m using java code that was generated from stylus studio enterprise edition from this xquery.The probelm is how do i pass/bind 2 incoming xml's to the XqPreparedExpression variable.I have attached my java code along with this post.Is is possible to pass more than 2 incoming xmls to get a single outgoing xml too??

public class tradetest3docs {
static String theDocumentBytes;
static String td;
static String t1d;
//private static Logger logger = Logger.getLogger(Quadesptest.class);
public static final String OAG_NAMESPACE_URI = "http://www.openapplications.org/oagis/9";
public static final Namespace OAG_NAMESPACE = Namespace.getNamespace("oag", OAG_NAMESPACE_URI);
public static final String ESB_BODY_XPATH_EXP = "/esb:Envelope/esb:Body";

public static void main(String[] args) throws Exception {

String xqueryUri = "file:///c:/Tp3.xquery";
Reader xqueryReader = null;
XQConnection xqconnection = null;
XQPreparedExpression xqExpr = null;

try {

System.out.println();
System.out.println("XQuery starting.");

xqueryReader = new FileReader(new File(new URI(xqueryUri)));
File fxml = new File("C:\\Tp_request.xml");
File f2xml = new File("C:\\AwailResponse.xml");

Configuration config = getConfiguration();
XQDataSource dataSource = new SaxonXQDataSource(config);
xqconnection = dataSource.getConnection();

XQStaticContext context = xqconnection.getStaticContext();
context.setBindingMode(XQConstants.BINDING_MODE_DEFERRED);
context.setBaseURI(xqueryUri);
xqconnection.setStaticContext(context);
XMLOutputter out = new XMLOutputter(org.jdom.output.Format
.getPrettyFormat());
SAXBuilder builder = new SAXBuilder(false);
org.jdom.Document doc = builder.build(new FileReader(fxml));
org.jdom.Document doc1 = builder.build(new FileReader(f2xml));

theDocumentBytes = out.outputString(doc);

System.out.println("substring"+theDocumentBytes);

xqExpr = xqconnection.prepareExpression(xqueryReader);
xqExpr.bindDocument(XQConstants.CONTEXT_ITEM, theDocumentBytes,xqconnection.createDocumentType());

Properties prop = new Properties();

String str = xqExpr.executeQuery().getSequenceAsString(prop).toString();

td = out.outputString(ProcessDocument(str));
System.out.println("The String document is :" +td );
System.out.println("XQuery finished.");
} finally {
if (xqueryReader != null) xqueryReader.close();
if (xqExpr != null) xqExpr.close();
if (xqconnection != null) xqconnection.close();
}
}

private static Configuration getConfiguration() {

Configuration config = new Configuration();
config.getSystemURIResolver().setRecognizeQueryParameters(true);
config.setValidation(false);
config.setStripsWhiteSpace(Whitespace.IGNORABLE);
return config;
}
public static org.jdom.Document ProcessDocument(String s) throws Exception{
SAXBuilder builder1 = new SAXBuilder(false);
org.jdom.Document document = builder1.build(new StringReader(s));
return document;
}
private static void setNamespace(Element elem, Namespace namespc){
List content = elem.getChildren();
Iterator iterator = content.iterator();

while (iterator.hasNext()) {
Element currElem = (Element)iterator.next();
currElem.addNamespaceDeclaration(namespc);
currElem.setNamespace(namespc);
// logger.debug("Element Name: "+ currElem.getName());
setNamespace(currElem, namespc);

}
}}

Regards
Rajat

Postnext
Minollo I.Subject: Using same xquery for 2 xml files
Author: Minollo I.
Date: 31 Jan 2008 12:00 PM
You will need to use one or two external variables if you want to bind data from your Java application.

I find it more clear in this case to just use two external variables and not the static default input; so, I would change the query into:

declare variable $input1 as document-node(element(*, xs:untyped)) external;
declare variable $input2 as document-node(element(*, xs:untyped)) external;

<PurchaseOrderLine>
<LineNumber>
{ $input1/TradePowerRequest/RequestDetail/ItemLineNumber/text() }
</LineNumber>
{
for $RequestDetail in $input1/TradePowerRequest/RequestDetail
return
<items>
{
if($RequestDetail/ItemNumber/@source = "UI") then
<item1>
{ $input2/DetailAvailability/DisplaySequence/text() }
</item1>
else ()
}
</items>
}
</PurchaseOrderLine>

...and the Java code into... (btw, note that you don't need to use jdom at all; it's actually a much more performing and scalable solution not using it)

import com.ddtek.xquery3.*;
import com.ddtek.xquery3.xqj.DDXQDataSource;
import com.ddtek.xquery3.xqj.DDXQJDBCConnection;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class test {

public static void main(String[] args) throws Exception {

Writer outWriter = null;
String xqueryUri = "file:///c:/Tp3.xquery";
Reader xqueryReader = null;
XQConnection xqconnection = null;
XQPreparedExpression xqExpr = null;


try {

System.out.println();
System.out.println("XQuery starting.");

xqueryReader = new FileReader(new File(new URI(xqueryUri)));

DDXQDataSource dataSource = new DDXQDataSource();
dataSource.setOptions("serialize=indent=yes");
dataSource.setBaseUri(xqueryUri);
xqconnection = dataSource.getConnection();

// default input data type - this declares that the default context node is a document.
XQItemType type = xqconnection.createElementType(null, XQItemType.XQBASETYPE_UNTYPED);
XQItemType documentType = xqconnection.createDocumentElementType(type);
XQStaticContext context = xqconnection.getStaticContext();
context.setContextItemStaticType(documentType);
context.setBindingMode(XQConstants.BINDING_MODE_DEFERRED);
xqconnection.setStaticContext(context);

outWriter = new OutputStreamWriter(System.out);

xqExpr = xqconnection.prepareExpression(xqueryReader);

xqExpr.bindDocument(new QName("input1"), new StreamSource(new File("C:\\Tp_request.xml")));
xqExpr.bindDocument(new QName("input2"), new StreamSource(new File("C:\\AwailResponse.xml")));
xqExpr.executeQuery().writeSequenceToResult(new StreamResult(outWriter));

outWriter.flush();
System.out.println();
System.out.println("XQuery finished.");
} finally {
if (outWriter!=null) outWriter.flush();
if (xqueryReader != null) xqueryReader.close();
if (xqExpr != null) xqExpr.close();
if (xqconnection != null) xqconnection.close();
}
}
}

I'm also attaching the code for your convenience.


DocumentTp3.xquery
Modified XQuery

Documenttest(1).java
The Java file

Posttop
rajat nairSubject: Using same xquery for 2 xml files
Author: rajat nair
Date: 31 Jan 2008 01:33 PM
that works !!! ty for the prompt reply

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.