Friday 14 December 2012

functx:contains-any-of()



functx:contains-any-of() use when we want to match same string against a no. of patterns.
To write a double quote string we use single quote to bind into a variable

let $a := 'satyam"s'


Read Escape character by using \


--> replace('satyam(kaushik','\(',' ') will return o/p as satyam kaushik.Here we   write \( in second args to read ( beacause it is escapes regular expression ( and read only (.

-->   \ put only in sec arg to escape and not in third arg
-->= sign not involved in special character.Hence = sign has no need to escape.

--> To escape from all regular expressions use following function

The functx:escape-for-regex function escapes a string that you wish to be taken literally rather than treated like a regular expression. This is useful when, for example, you are calling the built-in fn:replace function and you want any periods or parentheses to be treated like literal characters rather than regex special characters.



replace()



 -->fn:replace() always returns a replaced string,it doesn't change actual string.
  Due to above statement we can repace only single pattern by replace. To replace multiple patterns we need functx:replace-multi().

Cannot Update constructed nodes

Marklogic server function cannot update constructed nodes.Nodes which are created by you at run time and no link with database

Thursday 13 December 2012

Seperating transaction with :

Instead of eval( ) transactions can also separated with the help of sign ;

This has various advantages:

1. We can separate transactions in a simple way without using eval()

2. Separating operation transactions on same page before submit and after submit

3. Can set and get session on same page by separating operation transactions.


2. Separating transaction of form before submit and form after submit (operation transactions)

If form redirects to same page and further requires some processing.Then this post processing (i.e. further call to  some other module function,or setting some session value or any other calculation) can be simply carried out to same page by separating transaction of page before submit and page after submit is with the help of ;

Ex-  The page code "ShowChunkList" is something as:


import module namespace CHUNKS = "http://tbc.com/ManageChunks" at "ManageChunks.xqy";
import module namespace USERS = "http://tbc.com/ManageUsers" at "ManageUsers.xqy";
import module namespace BATCHES = "http://tbc.com/ManageBatches" at "ManageBatches.xqy";

declare variable $_User     := xdmp:get-current-user();
declare variable $_UserName := if (starts-with($_User, "COLOSSUS-")) then substring($_User,  10) else $_User;
declare variable $_UserType  := USERS:GetUserTypeByName($_UserName);

declare variable $_UserIsInTraining := USERS:GetIsUserInTraining( USERS:GetUserRecordFromName($_UserName) );
declare variable $_UserIsAnExpert := USERS:GetIsUserAnExpert( USERS:GetUserRecordFromName($_UserName) );
declare variable $_Mode := xdmp:get-request-field("MODE");
declare variable $_DarkBand := "#F1F0F5";
declare variable $_LightBand := "#E9F3F6";
declare variable $_RelatedDocumentColor := "#FFE4E1";

declare variable $_ChunkID := fn:normalize-space(if(xdmp:get-request-field("FindChunk", "")!="") then xdmp:get-request-field("FindChunk", "") else xdmp:get-request-field("ChunkID",""));


declare variable $_BatchFilename := xdmp:get-request-field("BatchFilename");

xdmp:set-response-content-type("text/html")

,



"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>"
,


<html>

<head>
</head>
<body>



          <form  action="ShowChunkList.xqy" method="post">
              <input type="hidden" id="EditedDivID" value="" name="EditedDiv"/>
              <input type="hidden" name="MODE" value="FindChunkList"/>
              <input type="hidden" name="FindChunk" value="{xdmp:get-request-field("FindChunk")}"/>
              <input type="hidden" name="Operation" value="EditDataBlock"/>
           
              <table style="margin:5px" border="0" cellpadding="5" cellspacing="3" width="99%">
               <tr valign="top">
                 <td width="50%">
                 {
                    CHUNKS:LOOP( $DivisionText )
                 }                  
                 </td>
                 <td width="50%">
                   <div id="DataDisplay_1" style="margin:5px;padding:4px;overflow:auto; height:800px">
                   {
                    (
                      $DataDisplayTable
                    ,
                      <input type="submit" value="Edit DataBlock"/>
                    )
                   }
                   </div>
                 </td>
             
               </tr>
       
             </table>
           
            </form>


/body>

</html>


******************************************************************************************
Now i would like to do some function call after submit .Therefore separate operation transaction in same page like this:

xquery version "1.0-ml";

(: ***** OPERATION TRANSACTIONS ***** :)

----------------------------------------------------------------operation after page submit
import module namespace CHUNKS = "http://tbc.com/ManageChunks" at "ManageChunks.xqy";
declare variable $_ID := xdmp:get-request-field("ID", "NONE");
declare variable $_Operation := xdmp:get-request-field("Operation", "NONE");

declare variable $_EditedData :=  xdmp:get-request-field("EditedDiv");

declare variable $_ExistingChunkID := fn:normalize-space(if(xdmp:get-request-field("FindChunk", "")!="") then xdmp:get-request-field("FindChunk", "") else ( ));

if ( $_Operation = "EditDataBlock")
then
  (
  CHUNKS:ChangeDataBlockAndDisplayTable($_ExistingChunkID, $_EditedData)
  ,
  xdmp:add-response-header("X-XSS-Protection", "0")
  )
else ( )

;
----------------------------------------------------------------operations before page submit
import module namespace CHUNKS = "http://tbc.com/ManageChunks" at "ManageChunks.xqy";
import module namespace USERS = "http://tbc.com/ManageUsers" at "ManageUsers.xqy";
import module namespace BATCHES = "http://tbc.com/ManageBatches" at "ManageBatches.xqy";

declare variable $_User     := xdmp:get-current-user();
declare variable $_UserName := if (starts-with($_User, "COLOSSUS-")) then substring($_User,  10) else $_User;
declare variable $_UserType  := USERS:GetUserTypeByName($_UserName);

declare variable $_UserIsInTraining := USERS:GetIsUserInTraining( USERS:GetUserRecordFromName($_UserName) );
declare variable $_UserIsAnExpert := USERS:GetIsUserAnExpert( USERS:GetUserRecordFromName($_UserName) );
declare variable $_Mode := xdmp:get-request-field("MODE");
declare variable $_DarkBand := "#F1F0F5";
declare variable $_LightBand := "#E9F3F6";
declare variable $_RelatedDocumentColor := "#FFE4E1";

declare variable $_ChunkID := fn:normalize-space(if(xdmp:get-request-field("FindChunk", "")!="") then xdmp:get-request-field("FindChunk", "") else xdmp:get-request-field("ChunkID",""));


declare variable $_BatchFilename := xdmp:get-request-field("BatchFilename");

xdmp:set-response-content-type("text/html")

,



"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>"
,
3. Set form request parameters on same page and then get anywhere on same page or another page within application.
 Bu using above approach let say we have a form that has input type="text".After submitting form  is redirecting to same page.Therefore we can set the session for the field value within same page by seperating ;. Therefore we can set and get session value for request parameters on same page with the help of ;

Note: We can set and get session value (i.e. not request parameters)at same page but to set and get the session for request parameter at same page,we have to set the session after submit (if redirects to same page) in separate transaction.

Ex- 

(: ***** PRESERVE SCROLL-TO VALUE ***** :)
try
{
let $ScrollTo := xdmp:get-request-field("ScrollTo", "NONE")
let $Dummy := xdmp:set-session-field("ScrollTo", $ScrollTo )
return
  ( )
}
catch($error)
{
  (: This will happen if the session has timed-out while the page is on-screen,
     taking the user back to the login screen :)
  xdmp:redirect-response( "Default.xqy" )
}


;

(: ***** REGISTER ADMIN USER (create user entry for administrator)  ***** :)

import module namespace USERS = "http://tbc.com/ManageUsers" at "ManageUsers.xqy";

let $User := xdmp:get-current-user()
return
  if ($User = "admin" or $User = "Admin" or $User = "administrator" or $User = "Administrator")
  then
    let $UserID := USERS:GetUserIDByName($User)
    return
      if ($UserID)
      then
        ( )
      else
        USERS:AddAdminUserRecord($User)
  else
    ( )


;
............................................


Here the ScrollTo value is passed by form from same page.








Thursday 18 October 2012

Sometime schema problem of xml document may disturb Programmer


Error:
XDMP-URI: (err:FODC0004) fn:collection("LIVE_CHUNKS ISDA-AMD-AGRT > Conditions Precedent and Secured Par...") -- Invalid URI format: "LIVE_CHUNKS ISDA-AMD-AGRT > Conditions Precedent and Secured Party's / Obligee's Rights and&#10; Remedies."



in /Colossus.xqy, at 2029:80 [1.0-ml]
 
Troubleshooting : Here the problem is in database where a document file  elements text is partitioned into two lines due to which the collection can't access that element.

<DivisionType>
                <ID>2012-01-05T16:08:30</ID>
                <FullName>Conditions Precedent and Secured Party's / Obligee's Rights and
                    Remedies.</FullName>
            </DivisionType>

here text "Conditions Precedent and Secured Party's / Obligee's Rights and
                    Remedies." is partitioning into two seperate line

So the correct way is :

<DivisionType>
                <ID>2012-01-05T16:08:30</ID>
                <FullName>Conditions Precedent and Secured Party's / Obligee's Rights and Remedies.</FullName>
            </DivisionType>

Friday 5 October 2012

Default format in marklogic is .xml

When i change any document in database which is other than .xml format then the document will corrupted.


Reason: when we load any document in database then by default <format> option of xdmp:load saves document into .xml format.Let say  when we load "abc.html" then originally it saves into database in xml format.If we change anything by opening in database explorer and save then it will save into html format and hence undetermined for Marklogic Server.Hence particular document would be corrupted.

Monday 24 September 2012

setting all value in multiselector

Usually Multiselector doesn't have "all" option value..If it happens then then it's better to set value as "All" if <select> tag is defined in one form page and we render the value to some another page or multiple pages.If we defined it in same page and get on same page then we must have to define the value at declaration time.

Now if we get the value from "Select tag" then the value we are getting like this:

"val1 val2 val3"

and this single  must be tokenised to iereate over multiple option values by two approach:
1.Using blank   space " "
2.similar string patern.

First approach: (Best approach)
It is safe to use first approach if option string values doesn't have multiple sapces.Therefore try to avoid spaces in option values at definition  time  till it is not compulsary ( by normalising/trimming/removing spaces see difference in another blog for all three terms ) so that we can easily tokenized by using this approach since after each value there is a space val1 val2 val3 etc.

Second approach:

If it is complulsary to havaing spaces in string and string have some business pattern like (exporting each pdf or xml has an extension .pdf or .xml) therefore we can tokenize over each value as:

for ex-        let $All := "0001012-0000002-KOMMUNEKREDIT.xml 0001012-0000003 KOMMUNE KREDIT.xml"
                  for $Each at $pos in fn:tokenize($All,".xml")[1 to fn:last()-1]
                    let $EachBatch := fn:normalize-space(fn:concat($Each,".xml"))
                    return $EachBatch

and then we also need to concat the value over which each string is tokenised.Here we run the loop to the second last element otherwise result would come as:  >
0001012-0000002-KOMMUNEKREDIT.xml
0001012-0000003-KOMMUNE KREDIT.xml
.xml 

there fore we skip last element.

Third Approach  (Only used in following situation)

But what happens if we have compulsion to have spaces in option values and string doesn't have patterns then we have to append some tag identofire with each option value at declaration time or rendered time to differntiate multiple values.

fn:tokenize

Selecting multiple values from multiselect would return all selected multiple values into a single string as "val1,val2,val3...............................".

Therefore

we should tokenize over this string to iterate over multiple values through ", " like this

fn:tokenize($string,",")

To skip last element in loop

run loop as:

For $x at $pos in <elements>[[1 to last()-1]]
return $x

Tuesday 28 August 2012

Not possible to read a pdf from file system

If we try to read a pdf on file system  without ingesting into database

for $IMPORT in xdmp:filesystem-directory("C:\ABC\0000001\Import")/dir:entry
let $Batchfilename := $IMPORT/dir:filename
let $BatchPathname := $IMPORT/dir:pathname
for $EachBatchPath in xdmp:filesystem-directory($BatchPathname)
let $PDFDocumentPath := xdmp:filesystem-directory($EachBatchPath/dir:entry/dir:pathname)/dir:entry[cts:contains(dir:filename,cts:word-query(".pdf"))]/dir:pathname
 
let $EachInputFileContent := if (xdmp:filesystem-file-exists($PDFDocumentPath) ) then
                                              xdmp:filesystem-file($PDFDocumentPath)
                                            else ( )
 return
  xdmp:save("D:/test", $EachInputFileContent,
          <options xmlns="xdmp:save">
          <output-encoding>utf-8</output-encoding>
          </options>)


throws  error : XDMP-READFILE: $r instance of node()+ -- ReadFile File is not in UTF-8: 

Solution is to establish xcc/mljam connection to access java code to read a pdf



import com.itextpdf.text.pdf.parser.PdfTextExtractor;

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;




public class PDFReaderSample
{
public static void main(String[] args) throws Exception
  {


PdfReader reader = new PdfReader("C:/ABC.pdf");
  int n = reader.getNumberOfPages();
  Rectangle psize = reader.getPageSize(1);
  float width = psize.height();
  float height = psize.width();
 Document document = new Document(new Rectangle(width, height));
  PdfWriter Pdfwriter = PdfWriter.getInstance(document,
new FileOutputStream("D:/test/satyam.pdf"));
 document.open();

 PdfContentByte cb = Pdfwriter.getDirectContent();
 int i = 0;
 int p = 0;
 while (i < n) {
 document.newPage();
 p++;
 i++;
 PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i);
 cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
 if (i < n) {
 i++;
 PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i);
 cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
 }
 BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
 cb.beginText();
 cb.setFontAndSize(bf, 19);
 cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p
+ " of " + ((n / 2) + (n % 2 > 0? 1 : 0)), width / 2, 40, 0);
 cb.endText();
 }
 document.close();

  }
}

Sunday 26 August 2012

xdmp:filesystem-directory


It returns the directory structure along with sub-directories and file with thieir name,path.type etc

 xdmp:filesystem-directory("C:\ABC\0000001\EXPORT")
returns the following file structure

<dir:directory xsi:schemaLocation="http://marklogic.com/xdmp/directory directory.xsd" xmlns:dir="http://marklogic.com/xdmp/directory" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <dir:entry>
    <dir:filename>0000001-0000000-0000002-ISDA-MAG-MC02_2011-06-27_04-59-01-206.xml</dir:filename>
    <dir:pathname>C:\ABC\0000001\EXPORT\0000001-0000000-0000002-ISDA-MAG-MC02_2011-06-27_04-59-01-206.xml</dir:pathname>
    <dir:type>file</dir:type>
    <dir:content-length>75087</dir:content-length>
    <dir:last-modified>2012-08-24T15:38:24+05:30</dir:last-modified>
  </dir:entry>
  <dir:entry>
    <dir:filename>docslist.xml</dir:filename>
    <dir:pathname>C:\ABC\0000001\EXPORT\docslist.xml</dir:pathname>
    <dir:type>file</dir:type>
    <dir:content-length>133</dir:content-length>
    <dir:last-modified>2012-08-24T15:38:24+05:30</dir:last-modified>
  </dir:entry>
</dir:directory>


Points:
->Here dir:directory specify the directory and not considered as the root of document so if we run command  like :  xdmp:filesystem-directory("C:\ColossusImportExport\0000001\EXPORT")/dir:directory
then it returns an empty sequence.here dir:filename is the root of structure.

->the uri path  "C:\ABC\0000001\EXPORT" is case insensitive.

Friday 24 August 2012

To Copy a set of files from a given folder in file structure to another folder on file structure


for $Each in  xdmp:filesystem-directory($InputFileDirectoryPath)/dir:entry
 let $EachInputFilePath := $Each/dir:pathname
 let $EachInputFilename := $Each/dir:filename
 let $EachInputFileContent := if (xdmp:filesystem-file-exists($EachInputFilePath) ) then xdmp:filesystem-file($EachInputFilePath) else ( )
 let $EachOutputFilePath := fn:concat($OutputFileDirectoryPath, $EachInputFilename)
 let $EachOutputFileContent := xdmp:unquote($EachInputFileContent)
 return
  xdmp:save($EachOutputFilePath , $EachOutputFileContent)

Tuesday 24 July 2012

eval function

Note :string in eval write in single quotes otherwise it gives you an error

Friday 20 July 2012

We can not apply comparison operator inside script if that script is embedded in xqy file

We can not apply comparison operator inside script if that script is embedded in xqy file.
Reason: If we give < than xquery parser throw an error like this "unexpected token syntax error, unexpected Junk_, expecting TagQName_" because it  understand < as tag beginning.

Similarly  If we give lt than scripting engine can't understand abd browser will not support.....


Conclusion: Script having comparison operator must be placed in seperate file with .js extension

Friday 29 June 2012

passing a xquery variable value to javascript function.

{&quot; string-value &quot;}
must be necessary when we passed a xquery variable value to javascript function.

 <select name="SelectedUserNameForRelocation" onchange="GetDocumentDivisionTypeForRelocation(&quot;{$_SelectedBatchFilename}&quot;)" 

Tuesday 12 June 2012

Troubleshooting while Configuring Xcc connection

Today I stuck in configuring Marklogic Connection.Actually i change the XCC folder on my file system which contains lib folder in which jar files are placed.So to explicitly tell the change location of xcc folder...i need to click on "Add Files" Button so that i specify the change url otherwise connection cannot be instantiated.