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
Neal  WaltersSubject: Xquery to find sample data file for testing
Author: Neal Walters
Date: 28 Jun 2005 09:30 AM
In a previous post entitled "XQuery on all XML Files in a Directory?",
Ivan helped me use Xquery to read all files in a directory.

Today, I'm tring to find a sample Amazon order that has more one line line item. The following code works, but I have to questions:

declare namespace ext = "FileUtils";
declare function ext:listFiles($path as xs:string) as xs:string* external;
declare variable $dirname as xs:string := "file:///c:/Odimo/Amazon/FileDrop/WorldOfWatches/Order__89/Doc_Original_Backup/";
<OrderReport>
{
for $filename in fn:tokenize(ext:listFiles($dirname), ",")
[fn:matches(., ".xml")]
, $rootel in doc(fn:concat($dirname, "/", $filename))
return
<filename name='{$filename}'>
<OrderID>{$rootel//Message/OrderReport/AmazonOrderID}</OrderID>
<ItemCount>{count($rootel//Message/OrderReport/Item)}</ItemCount>
</filename>
}
</OrderReport>

Question 1:
Is there a way to limit the output to only those files (orders) that have a count >= 2 {count($rootel//Message/OrderReport/Item)?

Question 2:
if I don't put two slashes here:
<OrderID>{$rootel//Message/OrderReport/AmazonOrderID}</OrderID>
I don't get anything. Why doesn't one slash work between $rootel and Message? From Stylus Studio, I can open a source doc, go to tree view,
and generate the following xpath:
/AmazonEnvelope/Message[1]/OrderReport[1]/AmazonOrderID[1]

Thanks!
Neal Walters
http://Biztalk-Training.com




Postnext
Alberto MassariSubject: Xquery to find sample data file for testing
Author: Alberto Massari
Date: 28 Jun 2005 09:47 AM
Hi Neil,

1) to limit the output of a FLWOR block you use the "where" statement, e.g.

for $filename in fn:tokenize(ext:listFiles($dirname), ",")[fn:matches(., ".xml")]
, $rootel in doc(fn:concat($dirname, "/", $filename))
where count($rootel//Message/OrderReport/Item) >=2
return
...

2) $rootel is bound to the return value of fn:doc, that is a document node, so the correct path is $rootel/AmazonEnvelope/Message/...

Hope this helps,
Alberto

Postnext
Neal  WaltersSubject: Xquery to find sample data file for testing
Author: Neal Walters
Date: 29 Jun 2005 11:56 AM
Thanks - the where/count worked great, but...

2) $rootel is bound to the return value of fn:doc, that is a document node, so the correct path is $rootel/AmazonEnvelope/Message/...

I agree what the correct path should be, but it's not working.

return
<filename name='{$filename}'>
<OrderID>{$rootel/Message/OrderReport/AmazonOrderID}</OrderID>
<ItemCount>{count($rootel//Message/OrderReport/Item)}</ItemCount>
</filename>

results in this:

<OrderReport>
<filename name="70435463-83b3b70-83a3ae8-83f7970-86102f8-8608610.xml">
<OrderID/>
<ItemCount>2</ItemCount>
</filename>

same thing with two slashes instead of one on the OrderID:

return
<filename name='{$filename}'>
<OrderID>{$rootel//Message/OrderReport/AmazonOrderID}</OrderID>
<ItemCount>{count($rootel//Message/OrderReport/Item)}</ItemCount>
</filename>

results in this:

<OrderReport>
<filename name="70435463-83b3b70-83a3ae8-83f7970-86102f8-8608610.xml">
<OrderID>
<AmazonOrderID>058-3261266-8080327</AmazonOrderID>
<AmazonOrderID>058-3612269-1080319</AmazonOrderID>
</OrderID>
<ItemCount>2</ItemCount>
</filename>

Input file begins like this:

<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<MessageType>OrderReport</MessageType>
<Message>
<MessageID>1</MessageID>
<OrderReport>
<AmazonOrderID>058-2840493-5587508</AmazonOrderID>
<AmazonSessionID>104-4155613-9935151</AmazonSessionID>

Any ideas?
Thanks,
Neal


Posttop
Alberto MassariSubject: Xquery to find sample data file for testing
Author: Alberto Massari
Date: 01 Jul 2005 04:33 AM
Hi Neal,
>Thanks - the where/count worked great, but...
>
>2) $rootel is bound to the return value of fn:doc, that
>is a document node, so the correct path is
>$rootel/AmazonEnvelope/Message/...
>
>I agree what the correct path should be, but it's not
>working.
>
>return
><filename name='{$filename}'>
><OrderID>{$rootel/Message/OrderReport/AmazonOrderID}</OrderID>
><ItemCount>{count($rootel//Message/OrderReport/Item)}</ItemCount>
></filename>

In my message I suggested you to try this code:

return
<filename name='{$filename}'>
<OrderID>{$rootel/AmazonEnvelope/Message/OrderReport/AmazonOrderID}</OrderID>
<ItemCount>{count($rootel/AmazonEnvelope/Message/OrderReport/Item)}</ItemCount>
</filename>

Can you try it?

Alberto

 
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.