Subject: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
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,")")
}
}
Subject: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.
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:
Subject: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
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/)
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, "")