0

I wrote a xquery 3.0 function that stores in a file the content of an attribute. The problem is that one only line is formatted in the file; in other words if the tag where I get the attribute value is for example

...
<tag attr=" example line 1
            example line 2
"/>...

in the file I found

example line 1 example line 2

whereas my desired output is

example line 1`
example line 2

What do you suggest? I'm using existdb. I thank you in advance.

  • In my experience eXist-db preserves whitespace in attributes. Can you post a minimal but complete working example to demonstrate the issue? Also, which version of eXist are you using? – Joe Wicentowski Nov 21 '15 at 03:43
  • I realized that existdb normalizes the content when it stores xml files. This is the reason why there is one line string stored in the file... – Armando Nov 21 '15 at 13:42
  • Sorry, I misunderstood - you need to preserve linebreaks, not just space characters. XML rules for whitespace handling in attributes (see http://usingxml.com/Basics/XmlSpace under "Attribute White Space Handling") state that all whitespace characters are replaced with space characters. eXist will preserve multiple spaces in attributes, and doesn't collapse 1+ to 1. So I think the answer is that XML doesn't preserve linebreaks in attributes. – Joe Wicentowski Nov 21 '15 at 16:57
  • Thanks for your time. – Armando Nov 22 '15 at 15:36

2 Answers2

1

XQuery mimics the behaviour of an XML parser when processing attributes in an element node constructor: it applies attribute value normalization as defined in the XML specification, and this loses newlines. You can force newlines into attribute values by writing them as &#xa;, but generally it's not a good XML design to have attributes where newlines are significant. (XSLT is an example where this rule causes problems, since XSLT puts XPath expressions in attributes and these can often be multi-line).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

Sure! My existdb version is 2.2.

xquery version "3.0";
declare boundary-space preserve;

declare namespace exist = "http://exist.sourceforge.net/NS/exist";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xmldb="http://exist-db.org/xquery/xmldb";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "text";
declare option output:indent "yes";
import module namespace file="http://exist-db.org/xquery/file";




let $log-in := xmldb:login("/db", "admin", "admin")


for $k in (35,36,37)

let $AppletName := fn:doc(fn:concat('/db/project/AppletA_',$k,'.xml'))//APPLET/@NAME/string()
let $collection := fn:concat('xmldb:exist:/db/project/Scripts_',$k)

let $node :=(<APPLET_SCRIPTS>{
                    for $i in fn:doc(fn:concat('/db/project/AppletA_',$k,'.xml'))//APPLET_SERVER_SCRIPT
                        let $MethodName := $i/@NAME
                        let $MethodScript := string($i/@SCRIPT)

                        let $file-name :=  fn:concat($MethodName,'.js')
                        let $store :=  xmldb:store($collection,$file-name, $MethodScript) 

                            return <div>{$MethodScript}</div>
            }</APPLET_SCRIPTS>)

return $node   

A fragment of xml is:

screenshot xml fragment

The content of file created is this one line string:

function Prova() { try { var sVal = TheApplication().InvokeMethod("LookupValue","FS_ACTIVITY_CLASS","CIM_FAC18"); var recordFound = searchRecord(sVal); if(!recordFound) { this.InvokeMethod("NewRecordCustom"); this.BusComp().SetFieldValue("Class",sVal); this.BusComp().WriteRecord(); } } catch(e) { throw(e); } finally { sVal = null; } }