Subject:ddtekjava write to file only executed if return used? Author:Ethan Stein Date:13 Dec 2011 11:32 PM
I have a ddtekjava function that takes in an element node and writes it to a file. It will then return a string indicating whether the write was successful or not. I have found that it only executes if I then use that string as a variable in my output. Is this expected behavior?
(: Write error nodes to file :)
declare function xbef:writeXmlFile($outFile as xs:string, $rootNode as xs:string,
$elmToWrite as element(), $encoding as xs:string, $indent as xs:string, $append as xs:string) as xs:string external;
<SIMPLE_ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="VISA_DATA.xsd">
{for $acctRec at $index in (1 to 5)
let $nodesToWrite := <NODE_TO_WRITE>HELLO WORLD {$index}</NODE_TO_WRITE>
let $writeRecsToFile := xbef:writeXmlFile("C:\sampleFile.xml", "ROOT_NODE", $nodesToWrite, "UTF-8", "yes", "true")
return
<SOME_NODE>SAMPLE TEXT {$index}</SOME_NODE>
}
</SIMPLE_ROOT>
-----------------
Java Function:
/**
* Writes XML to file system
*
* @param outFile file to write to
* @param rootNode name of root node (used with new file)
* @param nodeToWrite elements to write to file
* @param encoding XML encoding type for output
* @param indent whether to indent or not (valid values are "yes" | "no")
* @param append flag indicating add to end of file or create new one (valid values are "true" | "false"
* @return Message indicating successful write or error
*/
public static String writeXmlFile(String outFile, String rootNode, Element nodeToWrite, String encoding, String indent, String append) {
// Prepare the DOM document for writing
Source source = new DOMSource(errorDoc);
File errorFile = new File(outFile);
errorFOS = new FileOutputStream(errorFile);
Result result = new StreamResult(errorFOS);
// Write the DOM document to the file
TransformerFactory xFormerFactory = TransformerFactory.newInstance();
Transformer xformer = xFormerFactory.newTransformer();
Subject:ddtekjava write to file only executed if return used? Author:Ethan Stein Date:14 Dec 2011 12:03 AM
Ah, I understand. I switched the function to return an empty sequence and then simply changed it to this:
(: Write error nodes to file :)
declare function xbef:writeXmlFile($outFile as xs:string, $rootNode as xs:string,
$elmToWrite as element(), $encoding as xs:string, $indent as xs:string, $append as xs:string) as empty-sequence() external;
let $writeRecsToFile := xbef:writeXmlFile("C:\sampleFile.xml", "ROOT_NODE", $nodesToWrite, "UTF-8", "yes", "true")
return
<SOME_NODE>SAMPLE TEXT {$index}{$writeRecsToFile}</SOME_NODE>
Subject:ddtekjava write to file only executed if return used? Author:Ethan Stein Date:14 Dec 2011 12:36 AM
Okay, I think I understand. I can use the return of ddtek:seralize-to-url(...) and return that as an empty-sequence() in the function. Then I simply include the result.
So I have your function from before:
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">
{
$errLog/*,
$error
}
</Errors>
let $nodeAdded := 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 ($nodeAdded,$n))
The trick was I was missing the () around the return values so it wasn't picking them up. Thanks!