Subject: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?
(: 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 "",
$acctRecNum,
".xml" 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 "",
$acctRecNum, "_",
$n/MESSAGE_TOOL_PK/text(),
".xml" 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>
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>
(: 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>
(: 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 "",
$acctRecNum,
".xml" 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 "",
$acctRecNum, "_",
$n/MESSAGE_TOOL_PK/text(),
".xml" 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>
Subject: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.