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
Ram BalSubject: Xquery How to
Author: Ram Bal
Date: 14 Nov 2007 02:04 PM
I would like to know how to write a xquery for the following scenario.
Read a xml file. Trim the .xml extension and place the name of the file as the first element. Subsequently get the following elemnts and place them.
Say this xml is in a file by name File1.xml. I have made a small snippet of a large xml file.
<?xml version="1.0"?>
<!DOCTYPE DataObject SYSTEM "DataObject.dtd">
<DataObject
SuperClass="AMSDocComponent">
<Attributes>
<Attribute
Name="DOC_COMP_MOD_FL">
<DataType
DataType="Integer"
Size="8"
/>
</Attribute>
</Attributes>
</DataObject>

The Xquery should
1. Read the file1.xml
2.Truncate the .xml and put <file1> as the first tag.
3.Read the suprclass and put the name as the next tag.
4. Get the name of the attribute and the data type and size

The file should look like
<file1>
<AMSDOCComponent/>
<DOC_COMP_MOD_FL>Integer(8)</DOC_COMP_MOD_FL>
</file1>

Could you tell me how to write a xquery for this.
Thanks
Ram

Postnext
(Deleted User) Subject: Xquery How to
Author: (Deleted User)
Date: 15 Nov 2007 03:24 AM
Hi Ram,
which version of Stylus Studio are you using, and which XQuery processor are you planning to use?

Thanks,
Alberto

Postnext
Ram BalSubject: Xquery How to
Author: Ram Bal
Date: 15 Nov 2007 10:02 AM
I would like to use the built in processor and the version is Evaluation Version 2007 XML professional Suite Release2

Thanks
Ramani

Postnext
(Deleted User) Subject: Xquery How to
Author: (Deleted User)
Date: 15 Nov 2007 12:53 PM
Hi Ram,
this should be a good starting point:

declare variable $input_file := "file1.xml";

let $xml := doc($input_file)
return
element {substring-before($input_file, ".")}
{
element {$xml/DataObject/@SuperClass}{},
for $attribute in $xml/DataObject/Attributes/Attribute
return
element {$attribute/@Name}
{
concat($attribute/DataType/@DataType,"(",$attribute/DataType/@Size,")")
}
}

Hope this helps,
Alberto

Postnext
Ram BalSubject: Xquery How to
Author: Ram Bal
Date: 15 Nov 2007 02:13 PM
Thanks for the help. I am getting QName error.It is not able to figure out the name of the xml file from the path.
I did the following.
declare variable $input_file := "file:///C:/FilesForReverseEngg/DataObjects/Financial/AP/AD_DOC_ACTG.xml";


let $xml := doc($input_file)
return
element {substring-before($input_file, ".")}
{
element {$xml/DataObject/@SuperClass}{},
for $attribute in $xml/DataObject/Attributes/Attribute
return
element {$attribute/@Name}
{
concat($attribute/DataType/@DataType,"(",$attribute/DataType/@Size,")")
}
}

If I have to read several xml files from a directory and do the above processing how can this be done. That is it should read file1.xml from the directory and output to output1.xml.The second file to output2.xml.

Thanks
Ram

Postnext
(Deleted User) Subject: Xquery How to
Author: (Deleted User)
Date: 15 Nov 2007 03:32 PM
Hi Ram,
unfortunately none of the XQuery processors that shipped with Stylus Studio 2007 Release 2 had the capability of saving XML fragments to arbitrary URLs. So, if you have to solve your problem now, you will have to switch to XSLT 2.0 and Saxon, using this sample stylesheet:

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<xsl:for-each select="collection('file:///C:/testcase?select=file*.xml;recurse=no')">
<xsl:variable name="input_xml" select="."/>
<xsl:variable name="filename_with_ext" select="tokenize(document-uri($input_xml), '/')[last()]"/>
<xsl:variable name="url" select="concat('file:///C:/testcase/Out/',replace($filename_with_ext,'^file(.*)','output$1'))"/>
<xsl:result-document method="xml" href="{$url}" indent="yes">
<xsl:element name="{substring-before($filename_with_ext, '.')}">
<xsl:element name="{$input_xml/DataObject/@SuperClass}"/>
<xsl:for-each select="$input_xml/DataObject/Attributes/Attribute">
<xsl:element name="{@Name}">
<xsl:value-of select="concat(DataType/@DataType,'(',DataType/@Size,')')"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:result-document>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Hope this helps,
Alberto

Postnext
Minollo I.Subject: Xquery How to
Author: Minollo I.
Date: 15 Nov 2007 03:41 PM
BTW, this is becoming possible in Stylus Studio v2008/DataDirect XQuery 3.1, available in about three weeks from now.

Look for ddtek:serialize-to-url() in the DataDirect XQuery 3.1 documentation as soon as the product goes public!

Postnext
Ram BalSubject: Xquery How to
Author: Ram Bal
Date: 15 Nov 2007 03:40 PM
Thanks so much.
Ram

Postnext
Ram BalSubject: Xquery How to
Author: Ram Bal
Date: 15 Nov 2007 05:17 PM
In xquery how do we loop through the directory of all xml files and write the output in seperate files. For example if the directory contains 5 files, I would like to loop thrpogh the files and write the contents in 5 different files. Is there a way to do this instead of XSLT

Postnext
Minollo I.Subject: Xquery How to
Author: Minollo I.
Date: 15 Nov 2007 05:24 PM
It will be possible using DataDirect XQuery 3.1, as I mentioned. As soon as 3.1 is public, I'll add your question to our section of trips & tricks (http://www.xquery.com/tips_and_tricks/)

Posttop
(Deleted User) Subject: Xquery How to
Author: (Deleted User)
Date: 16 Nov 2007 02:28 AM
Originally Posted: 16 Nov 2007 02:29 AM
This is the solution using DataDirect XQuery 3.1

for $input_xml in collection("file:///C:/testcase?select=file*.xml;recurse=no")
let $filename_with_ext := tokenize(fn:document-uri($input_xml), "/")[last()]
let $url := concat("file:///C:/testcase/Out/",replace($filename_with_ext,'^file(.*)','output$1'))
let $xml :=(
element {substring-before($filename_with_ext, ".")}
{
element {$input_xml/DataObject/@SuperClass}{},
for $attribute in $input_xml/DataObject/Attributes/Attribute
return
element {$attribute/@Name}
{
concat($attribute/DataType/@DataType,"(",$attribute/DataType/@Size,")")
}
}
)
return ddtek:serialize-to-url($xml, $url, "")

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.