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
Ethan SteinSubject: doc-available not working as expected
Author: Ethan Stein
Date: 14 Dec 2011 11:41 AM
I have the following xquery that uses doc-available to determine if a file exists and use it if it does, or overwrite it if it does not. For some reason, doc-available is always returning false. Anyone understand why?

------------------

XQuery:

declare option ddtek:xml-streaming 'yes';
declare option ddtek:serialize "indent=yes, omit-xml-declaration=no";

(: external reference to XML document :)
declare variable $xPRSXMLInput as document-node() external;

declare variable $logURL := "log.xml";
declare function local:addNode($error as element()) as empty-sequence()
{
let $errLog := if(fn:doc-available($logURL)) then
doc($logURL)/Errors
else ()
let $newLog :=
<Errors
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ErrorNodes.xsd">
{
<DOC_NAME>{$logURL}</DOC_NAME>,
<DOC_AVAILABLE>{fn:doc-available($logURL)}</DOC_AVAILABLE>,
$errLog/*,
$error
}
</Errors>

return (ddtek:serialize-to-url($newLog, $logURL,"")
)
};


(: Retrieves the size of a white space or red/gold space from a secondary file and inserts it into the node :)
declare function local:replaceElms($n as element(), $acctRecNum as xs:string) as element()*
{
let $whitespacemsg := local:addNode(
<MESSAGE_WHITE_SPACE>{
concat("The white space size (for VISA region 01B) for &quot;",
$acctRecNum,
".xml&quot; could not be found. Please check the xDoc output.")
}</MESSAGE_WHITE_SPACE>
)
return <A_DIFFERENT_MESSAGE>This is a white space message{$whitespacemsg}</A_DIFFERENT_MESSAGE>
};

(: Retrieves the size of the message from a secondary file and inserts it into the message node :)
declare function local:processMessageNode($n as element(), $acctRecNum as xs:string) as element()*
{
let $msgErroNode := local:addNode(
<MESSAGE_ERROR>
{$n/MESSAGE_TOOL_PK}
<MESSAGE_MESSAGE>{
concat("The message size (for VISA region 01A) for &quot;",
$acctRecNum, "_",
$n/MESSAGE_TOOL_PK/text(),
".xml&quot; could not be found. Please check the xDoc output.")
}</MESSAGE_MESSAGE>
</MESSAGE_ERROR>
)
return <SOME_OTHER_MESSAGE>this is another message{$msgErroNode}</SOME_OTHER_MESSAGE>
};


(: Calls subfunctions to add specific nodes or data values :)
declare function local:updateNodes($n as element(), $recNumber as xs:string) as element()*
{
if ((local-name($n) eq 'WHITE_SPACE_SIZE') or (local-name($n) eq 'RED_GOLD_SIZE')) then
local:replaceElms($n, $recNumber)
else if (local-name($n) eq 'MESSAGE') then
local:processMessageNode($n, $recNumber)
else
element { name($n) }
{
(
$n/@*,
$n/text(),
for $child in $n/*
return local:updateNodes($child, $recNumber)
)
}

};


<VISA_DATA
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="VISA_DATA.xsd">
{
for $acctRec in $xPRSXMLInput/VISA_DATA/ACCOUNT_RECORD
let $acctRecNumber := $acctRec/ACCOUNT_RECORD_KEY_PK/text()
return local:updateNodes($acctRec, $acctRecNumber)
}
</VISA_DATA>

-------------------

XML input:

<?xml version="1.0" encoding="ISO-8859-1"?>
<VISA_DATA>
<ACCOUNT_RECORD>
<ACCOUNT_RECORD_KEY_PK>43</ACCOUNT_RECORD_KEY_PK>
<WHITE_SPACE_SIZE>0</WHITE_SPACE_SIZE>
<RED_GOLD_SIZE>0</RED_GOLD_SIZE>
<STATEMENT_TOTAL_PAGES>0</STATEMENT_TOTAL_PAGES>
<MESSAGE>
<MESSAGE_ASCII_RECORD_ORDER_NUM>000014</MESSAGE_ASCII_RECORD_ORDER_NUM>
<MESSAGE_TOOL_PK>0318</MESSAGE_TOOL_PK>
<MESSAGE_NUMBER>18</MESSAGE_NUMBER>
<PRIORITY>X</PRIORITY>
<MESSAGE_SIZE>0</MESSAGE_SIZE>
<REGION>
<MESSAGE_REGION>X</MESSAGE_REGION>
<REGION_TEMPLATE>T</REGION_TEMPLATE>
<MESSAGE_CONTENT>X</MESSAGE_CONTENT>
<OPTIONAL_TEXT_2>X</OPTIONAL_TEXT_2>
<OPTIONAL_TEXT_5>X</OPTIONAL_TEXT_5>
<OPTIONAL_TEXT_6>X</OPTIONAL_TEXT_6>
<OPTIONAL_TEXT_7>X</OPTIONAL_TEXT_7>
</REGION>
<MESSAGE_TYPE>X</MESSAGE_TYPE>
<MESSAGE_PACKAGE>X</MESSAGE_PACKAGE>
</MESSAGE>
</ACCOUNT_RECORD>
</VISA_DATA>

--------------------

Resulting log file that shows doc-available is false regardless of whether log exists or not:

<?xml version="1.0" encoding="UTF-8"?>
<Errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ErrorNodes.xsd">
<DOC_NAME>log.xml</DOC_NAME>
<DOC_AVAILABLE>false</DOC_AVAILABLE>
<MESSAGE_ERROR>
<MESSAGE_TOOL_PK>0318</MESSAGE_TOOL_PK>
<MESSAGE_MESSAGE>The message size (for VISA region 01A) for "43_0318.xml" could not be found. Please check the xDoc output.</MESSAGE_MESSAGE>
</MESSAGE_ERROR>
</Errors>

Postnext
Ivan PedruzziSubject: doc-available not working as expected
Author: Ivan Pedruzzi
Date: 14 Dec 2011 12:48 PM

Try with the full URL

declare variable $logURL := "file:///c:/mypath/log.xml";


Ivan Pedruzzi
Stylus Studio Team

Postnext
Ethan SteinSubject: doc-available not working as expected
Author: Ethan Stein
Date: 14 Dec 2011 01:35 PM
Nope, setting it to "file:///c:/log.xml" didn't allow me to update it and it still indicates it is false. I'm using Stylus Studio 2010.


declare option ddtek:xml-streaming 'yes';
declare option ddtek:serialize "indent=yes, omit-xml-declaration=no";

(: external reference to XML document :)
declare variable $xPRSXMLInput as document-node() external;

declare variable $logURL := "file:///c:/log.xml";

declare function local:addNode($error as element()) as empty-sequence()
{
let $errLog := if(fn:doc-available($logURL)) then
doc($logURL)/Errors
else ()
let $newLog :=
<Errors
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ErrorNodes.xsd">
{
<DOC_NAME>{$logURL}</DOC_NAME>,
<DOC_AVAILABLE>{fn:doc-available($logURL)}</DOC_AVAILABLE>,
$errLog/*,
$error
}
</Errors>

return (ddtek:serialize-to-url($newLog, $logURL,""))
};


(: Retrieves the size of a white space or red/gold space from a secondary file and inserts it into the node :)
declare function local:replaceElms($n as element(), $acctRecNum as xs:string) as element()*
{
let $whitespacemsg := local:addNode(
<MESSAGE_WHITE_SPACE>{
concat("The white space size (for VISA region 01B) for &quot;",
$acctRecNum,
".xml&quot; could not be found. Please check the xDoc output.")
}</MESSAGE_WHITE_SPACE>
)
return <A_DIFFERENT_MESSAGE>This is a white space message{$whitespacemsg}</A_DIFFERENT_MESSAGE>
};

(: Retrieves the size of the message from a secondary file and inserts it into the message node :)
declare function local:processMessageNode($n as element(), $acctRecNum as xs:string) as element()*
{
let $msgErroNode := local:addNode(
<MESSAGE_ERROR>
{$n/MESSAGE_TOOL_PK}
<MESSAGE_MESSAGE>{
concat("The message size (for VISA region 01A) for &quot;",
$acctRecNum, "_",
$n/MESSAGE_TOOL_PK/text(),
".xml&quot; could not be found. Please check the xDoc output.")
}</MESSAGE_MESSAGE>
</MESSAGE_ERROR>
)
return <SOME_OTHER_MESSAGE>this is another message{$msgErroNode}</SOME_OTHER_MESSAGE>
};


(: Calls subfunctions to add specific nodes or data values :)
declare function local:updateNodes($n as element(), $recNumber as xs:string) as element()*
{
if ((local-name($n) eq 'WHITE_SPACE_SIZE') or (local-name($n) eq 'RED_GOLD_SIZE')) then
local:replaceElms($n, $recNumber)
else if (local-name($n) eq 'MESSAGE') then
local:processMessageNode($n, $recNumber)
else
element { name($n) }
{
(
$n/@*,
$n/text(),
for $child in $n/*
return local:updateNodes($child, $recNumber)
)
}

};


<VISA_DATA
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="VISA_DATA.xsd">
{
for $acctRec in $xPRSXMLInput/VISA_DATA/ACCOUNT_RECORD
let $acctRecNumber := $acctRec/ACCOUNT_RECORD_KEY_PK/text()
return local:updateNodes($acctRec, $acctRecNumber)
}
</VISA_DATA>

--------------------

Log XML result:

<?xml version="1.0" encoding="UTF-8"?>
<Errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ErrorNodes.xsd">
<DOC_NAME>file:///c:/log.xml</DOC_NAME>
<DOC_AVAILABLE>false</DOC_AVAILABLE>
<MESSAGE_ERROR>
<MESSAGE_TOOL_PK>0318</MESSAGE_TOOL_PK>
<MESSAGE_MESSAGE>The message size (for VISA region 01A) for "43_0318.xml" could not be found. Please check the xDoc output.</MESSAGE_MESSAGE>
</MESSAGE_ERROR>
</Errors>

Posttop
Ivan PedruzziSubject: doc-available not working as expected
Author: Ivan Pedruzzi
Date: 14 Dec 2011 04:26 PM

Starting with Windows Vista, applications are not allow to create files in the system drive root. Create a different location like c:\log
Set the log URL to "file:///c:/log/log.xml"

addNode needs to force expansion of the content you want to serialize to prevent the optimizer to take it away, see version attached

I also added a date-time attribute to each message, in the log.

Hope this helps
Ivan Pedruzzi
Stylus Studio Team


Documentq.xquery

 
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.