This page was generated
December 30, 2008
5:50 PM
XQuery Function Reference

Module: WordProcessingML Support

The WordProcessingML function module is included in the Toolkit for Word zip package as the following file:

  • zip_dir/xquery/word-processing-ml-support.xqy

where zip_dir is the directory in which the Toolkit for Word was unzipped.

To use the word-processing-ml-support.xqy module in your own XQuery modules, copy it to your App Server root and include a line similar to the following in your XQuery prolog:

import module namespace ooxml="http://marklogic.com/openxml" 
	    at "/word-processing-ml-support.xqy";

The WordProcessingML functions are used to manipulate Microsoft Office Open XML documents.

Function Summary
ooxml:create-paragraph This function creates a w:p element from the specified string.
ooxml:custom-xml This function tags $content with w:customXml and sets the @element value to $tag.
ooxml:custom-xml-highlight This function highlights $hightlight-term in $nodes by applying <w:customXml w:element={$tag-name}> to the term.
ooxml:get-custom-xml-ancestor This function returns the greatest ancestor of the specified node that is either an immediate parent of type w:customXml or w:sdt, a parent of that parent, and so on until the parent is not a w:customXml or w:sdt element.
ooxml:get-paragraph-style-id This function returns the @val attribute for a w:pStyle element (child of w:pPr) from $pstyle.
ooxml:get-paragraph-styles This function returns the paragraph properties.
ooxml:get-run-style-id This function returns the values for the attribute @val for a w:rStyle element (which is a child of a w:rPr element).
ooxml:get-run-styles This function returns paragraph run styles.
ooxml:get-style-definition This function returns the style definition from styles.xml for the given style ID.
ooxml:remove-paragraph-styles This function removes all properties from w:p paragraphs.
ooxml:replace-custom-xml-element This function replaces any @element values for w:customXml with $newtag, where w:customXml[@w:element = $oldtag].
ooxml:replace-paragraph-styles This function sets paragraph properties for paragraphs (w:p) within a block.
ooxml:replace-run-styles This function sets run properties for runs (w:r) within a block.
ooxml:replace-style-definition This function replaces w:style elements (style definitions) in w:styles (the styles.xml file from a .docx package).
Function Detail
ooxml:create-paragraph(
$para as xs:string
)  as  element(w:p)
Summary:

This function creates a w:p element from the specified string.

Parameters:
$para : The text of the paragraph.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
          at "/MarkLogic/openxml/word-processing-ml.xqy";

ooxml:create-paragraph("Hello there everyone.")

=> 
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
   <w:r><w:t>Hello there everyone.</w:t></w:r>
</w:p>
  
  

ooxml:custom-xml(
$content as element(),
$tag as xs:string
)  as  element(w:customXml)?
Summary:

This function tags $content with w:customXml and sets the @element value to $tag.

Parameters:
$content : The content element.
$tag : The name of the custom XML tag.

Usage Notes:

The $content parameter must either be an element of type paragraph (w:p) , run (w:r), field (w:fldSimple,w:fldChar), hyperlink (w:hyperlink), custom xml (w:customXml), structured document tag (w:sdt), table (w:tbl), cel (w:tc), or row (w:tr). If it is not one of these types, the function returns the empty sequence.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

let $para:= 
  <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
       <w:r><w:t>This is my paragraph.</w:t></w:r>
  </w:p>
let $tag := "ABCD"
return ooxml:custom-xml($para,$tag)
=>
<w:customXml w:element="ABCD" 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:p><w:r><w:t>This is my paragraph.</w:t></w:r></w:p>
</w:customXml>

ooxml:custom-xml-highlight(
$nodes as node()*,
$highlight-term as cts:query,
$tag-name as xs:string,
[$attributes as xs:string*],
[$values as xs:string*]
)  as  node()*
Summary:

This function highlights $hightlight-term in $nodes by applying <w:customXml w:element={$tag-name}> to the term. It searches for term within w:t elements within a w:r (runs), wrapping a customXml element around the element the element containing the highlighted match.

Parameters:
$nodes : The paragraph(s) from which to search for the highlighted terms.
$highlight-term : The cts:query used to find the highlight term.
$tag-name : The name used for the w:customXml w:element attribute value.
$attributes (optional): A list of attributes to be added to the w:customXml properties.
$values (optional): The values corresponding to the $attributes.

Usage Notes:

If you specify the $attributes and $values parameters, the length of the list for each parameter must be the same, otherwise an exception is thrown.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $wp := <w:p><w:r><w:t>THIS IS A TEST!</w:t></w:r></w:p>
let $wordquery := cts:word-query("TEST")
return  ooxml:custom-xml-highlight($wp,$wordquery,"MYTAG")
=>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:r>
    <w:t xml:space="preserve">THIS IS A </w:t>
  </w:r>
  <w:customXml w:element="MYTAG">
   <w:r>
     <w:t>TEST</w:t>
   </w:r>
  </w:customXml>
  <w:r>
    <w:t xml:space="preserve">!</w:t>
  </w:r>
</w:p>
  
Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $wp := <w:p><w:r><w:t>THIS IS A TEST!</w:t></w:r></w:p>
let $wordquery := cts:word-query("TEST")
let $attributes := ("author","id")
let $values := ("oslo","1") 
return  ooxml:custom-xml-highlight($wp,$wordquery,"MYTAG", $attributes, $values)
=>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:r>
    <w:t xml:space="preserve">THIS IS A </w:t>
  </w:r>
  <w:customXml w:element="MYTAG">
    <w:customXmlPr>
      <w:attr w:name="author" w:val="oslo"/>
      <w:attr w:name="id" w:val="1"/>
    </w:customXmlPr>
    <w:r>
      <w:t>TEST</w:t>
    </w:r>
  </w:customXml>
  <w:r>
    <w:t xml:space="preserve">!</w:t>
  </w:r>
</w:p>
  

ooxml:get-custom-xml-ancestor(
$doc as element()
)  as  element()?
Summary:

This function returns the greatest ancestor of the specified node that is either an immediate parent of type w:customXml or w:sdt, a parent of that parent, and so on until the parent is not a w:customXml or w:sdt element. If no such ancestor is found, then the empty sequence is returned.

Parameters:
$doc : An element to search.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";
declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $para:= 
 <w:p>
   <w:r><w:t>THIS IS A TEST!</w:t></w:r>
 </w:p>
let $custom := 
  <w:customXml w:element="foo">
    <w:customXml w:element="bar">{$para}</w:customXml>
  </w:customXml>
let $wp1 := $custom//w:p
return  ooxml:get-custom-xml-ancestor($wp1)
=>
<w:customXml w:element="foo" 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
 <w:customXml w:element="bar">
	<w:p><w:r><w:t>THIS IS A TEST!</w:t></w:r></w:p>
 </w:customXml>
</w:customXml>


  

ooxml:get-paragraph-style-id(
$pstyle as element(w:pPr)
)  as  xs:string?
Summary:

This function returns the @val attribute for a w:pStyle element (child of w:pPr) from $pstyle. Returns the empty sequence if no IDs are found.

Parameters:
$pstyle : The w:pPr element.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $pstyle:= 
       <w:pPr>
             <w:pStyle w:val="TestParagraphStyle" />
             <w:ind w:left="1440" />
        </w:pPr>
return ooxml:get-paragraph-style-id($pstyle)

=> TestParagraphStyle

ooxml:get-paragraph-styles(
$paragraph as element(w:p)*
)  as  element(w:pPr)*
Summary:

This function returns the paragraph properties. Returns the empty sequence if no styles are found.

Parameters:
$paragraph : The w:p element(s).

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

let $wp := 
<w:p  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr><w:u/></w:pPr>
  <w:r><w:t>This is a test.</w:t></w:r>
</w:p>
return ooxml:get-paragraph-styles($wp)
=>
<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:u/>
</w:pPr>
  

ooxml:get-run-style-id(
$rstyle as element(w:rPr)
)  as  xs:string?
Summary:

This function returns the values for the attribute @val for a w:rStyle element (which is a child of a w:rPr element). Returns the empty sequence if no IDs are found.

Parameters:
$rstyle : The w:rPr element containing the w:rStyle element.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

let $rstyle := 
<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rStyle w:val="oslo"/><w:u/>
</w:rPr>
return ooxml:get-run-style-id($rstyle)
=>
oslo

ooxml:get-run-styles(
$paragraph as element(w:p)*
)  as  element(w:rPr)*
Summary:

This function returns paragraph run styles. Returns the empty sequence if no styles are found.

Parameters:
$paragraph : The w:p element(s).

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";


let $wp := 
<w:p  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr><w:u/></w:pPr>
  <w:r>
      <w:rPr><w:b/></w:rPr>
      <w:t>This is a test.</w:t>
   </w:r>
  <w:r>
      <w:rPr><w:i/></w:rPr>
      <w:t>This is another.</w:t>
   </w:r>
</w:p>
return ooxml:get-run-styles($wp)
=>
<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:b/>
</w:rPr>
<w:rPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:i/>
</w:rPr>


ooxml:get-style-definition(
$styleid as xs:string,
$styles as element(w:styles)
)  as  element(w:style)?
Summary:

This function returns the style definition from styles.xml for the given style ID.

Parameters:
$styleid : The style ID.
$styles : The root element from the styles.xml file in the .docx package.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";
declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $styleids := "oslo"
let $styles := doc("/Test_docx_parts/word/styles.xml") 
return ooxml:get-style-definitions($styleids, $styles)/w:style
=>
<w:style xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
       	w:type="character" w:customStyle="1" w:styleId="oslo">
     <w:name w:val="oslo"/><w:basedOn w:val="Emphasis"/>
     <w:uiPriority w:val="1"/><w:qFormat/><w:rsid w:val="005B2E7B"/>
       <w:rPr><w:rFonts w:ascii="Bodoni MT Black" w:hAnsi="Bodoni MT Black"/>
       <w:b/><w:sz w:val="32"/></w:rPr>
</w:style>


ooxml:remove-paragraph-styles(
$paragraph as element()
)  as  element()
Summary:

This function removes all properties from w:p paragraphs. Paragraph properties are w:pPr elements and run properties are w:rPr elements.

Parameters:
$paragraph : Any block-level element.

Usage Notes:

This function is useful when you want to insert a paragraph with the Office defaults, or when you want to switch the existing properties for a paragraph.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

let $paragraph:=
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr><w:b/></w:pPr>
     <w:r><w:rPr><w:u/></w:rPr>
             <w:t>This is a test.</w:t>
      </w:r></w:p>
return ooxml:remove-paragraph-styles($paragraph)
=>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:r><w:t>This is a test.</w:t></w:r>
</w:p>


ooxml:replace-custom-xml-element(
$content as element(),
$oldtag as xs:string,
$newtag as xs:string
)  as  element()
Summary:

This function replaces any @element values for w:customXml with $newtag, where w:customXml[@w:element = $oldtag].

Parameters:
$content : The OOXML content.
$oldtag : The old tag for the element name.
$newtag : The new tag for the element name.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $block:=
<w:customXml w:element="ABCD">
    <w:p>
<w:r><w:t>This is my paragraph.</w:t></w:r>
         </w:p>
</w:customXml>
let $origtag := "ABCD"
let $newtag := "EFGH"
return ooxml:replace-custom-xml-element($block,$origtag,$newtag)
=>
<w:customXml w:element="EFGH" 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:p><w:r><w:t>This is my paragraph.</w:t></w:r></w:p>
</w:customXml>

ooxml:replace-paragraph-styles(
$block as element(),
$wpProps as element(w:pPr)?
)  as  element()
Summary:

This function sets paragraph properties for paragraphs (w:p) within a block. If the empty sequence is passed for $wpProps, then the paragraph properties for the $block are removed.

Parameters:
$block : The block containing the paragraph to be updated.
$wpProps : The paragraph properties, as a w:pPr element, to be applied within the paragraph block.

Usage Notes:

If $block already has styles applied, these styles are replaced by the new style parameters. If the empty sequence is passed for $wpProps, the paragraph properties for the $block are removed. If there are no properties currently defined for the paragraph, the $wpProps are applied.

Note that this function does not validate that the w:pPr element passed in is valid, so make sure the element you pass in is valid WordProcessingML markup. If you pass in an invalid element, it could result in a document that cannot be opened in Microsoft Word.


Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";
declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $wp1 := 
<w:p>
     <w:r><w:t>THIS IS A TEST!</w:t></w:r>
</w:p>
let $pPr := <w:pPr><w:b/></w:pPr>
return  ooxml:replace-paragraph-styles($wp1,$pPr)
=>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:pPr>
		<w:b/>
        </w:pPr>
	<w:t>THIS IS A TEST!</w:t>
</w:p>


ooxml:replace-run-styles(
$block as element(),
$wrProps as element(w:rPr)?
)  as  element()
Summary:

This function sets run properties for runs (w:r) within a block. If the empty sequence is passed for $wrProps, then the paragraph properties for the $block are removed.

Parameters:
$block : The block containing the run to be updated.
$wrProps : The run properties, as a w:rPr element, to be applied within the run block.

Usage Notes:

Note that this function does not validate that the w:rPr element passed in is valid, so make sure the element you pass in is valid WordProcessingML markup. If you pass in an invalid element, it could result in a document that cannot be opened in Microsoft Word.

Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";
declare namespace w=
  "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

let $wp1 := 
<w:p>
     <w:r><w:t>THIS IS A TEST!</w:t></w:r>
</w:p>
let $rPr := <w:rPr><w:b/></w:rPr>
return  ooxml:replace-run-styles($wp1,$rPr)
=>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:r>
    <w:rPr>
      <w:b/>
    </w:rPr>
    <w:t>THIS IS A TEST!</w:t>
  </w:r>
</w:p>	


ooxml:replace-style-definition(
$newstyle as element(w:style),
$styles as element(w:styles)
)  as  element(w:styles)
Summary:

This function replaces w:style elements (style definitions) in w:styles (the styles.xml file from a .docx package).

Parameters:
$newstyle : The new style element.
$styles : The old styles element (from styles.xml).

Usage Notes:

If the id for the style $newstyle matches a style definition already present in $styles, the $newstyle will replace the existing style.

If the id for the style $newstyle does not exist in $styles, the $newstyle definition will be appended to the other definitions in $styles.

Note that it is possible within Microsoft Word to create styles with names that differ only in their case that have different style attributes. For example, you can create two styles: "mystyle" and "MYSTYLE", which have completely different style definitions.


Example:
xquery version "1.0-ml";
import module namespace ooxml= "http://marklogic.com/openxml" 
		  at "MarkLogic/openxml/word-processing-ml.xqy";

let $oldstyle := 
 <w:styles 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:style w:type="character" w:customStyle="1" w:styleId="oslo">
     <w:name w:val="oslo"/><w:basedOn w:val="Emphasis"/>
     <w:uiPriority w:val="1"/><w:qFormat/><w:rsid w:val="005B2E7B"/>
       <w:rPr><w:rFonts w:ascii="Bodoni MT Black" w:hAnsi="Bodoni MT Black"/>
       <w:b/><w:sz w:val="32"/></w:rPr>
   </w:style>
 </w:styles>
let $newstyle :=
  <w:style 
   xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
   w:type="character" w:customStyle="1" w:styleId="oslo">
     <w:name w:val="oslo"/><w:basedOn w:val="Emphasis"/>
     <w:uiPriority w:val="1"/><w:qFormat/><w:rsid w:val="005B2E7B"/>
       <w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
       <w:b/><w:sz w:val="32"/></w:rPr>
   </w:style>
return
ooxml:replace-style-definition($newstyle, $oldstyle)
=>
<w:styles 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:style w:type="character" w:customStyle="1" w:styleId="oslo">
    <w:name w:val="oslo"/><w:basedOn w:val="Emphasis"/>
    <w:uiPriority w:val="1"/><w:qFormat/><w:rsid w:val="005B2E7B"/>
    <w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New"/>
      <w:b/>
      <w:sz w:val="32"/>
    </w:rPr>
  </w:style>
</w:styles>