Hi Betty,
You might consider using the "render:custom" tag rather than overriding the
generic-elem function. If the "render:custom" element does not offer you
enough granularity and you would like go beyond the rendering of a single XML
element by tagname, then you may want to go the path of overriding the
functions of standard.xqy directly. I've included an explanation on the usage
of each of these below. They are preferable to editing the
"render:generic-elem" directly. Feedback about the explanations would be much
appreciated - they may possibly get included in future documentation. Let me
know what you think.
Cheers,
David
Overriding a Function from "standard.xqy"
You can override a function in standard.xqy by editing the variable for the
related function in config.xqy.
For example, say I want to entirely change the function for the footer. I find
the following lines in config.xqy:
declare variable $footer :=
xdmp:function(
fn:QName(
"http://marklogic.com/appservices/component", "footer"));
And I change it to:
declare variable $footer :=
xdmp:function(
fn:QName(
"http://mycompany.com/custom-app", "custom-footer"),
"/lib/extension.xqy");
Then, add the "custom-footer" function in "/lib/extension.xqy". The function
"asc:footer" in standard.xqy will no longer be used.
Custom Rendering
On the "Content" screen of the Applicaiton Builder, you are able to specify how
certain XML elements get translated into XHTML (ex: each element <section>
should be rendered as a <div>). Sometimes you want to have more control over
how this XHTML gets rendered. The "render:custom" tag allows you to do just
that.
In /application/lib/config.xqy, you will find a variable named $TRANSFORMS.
Within the element $TRANSFORMS, you will find elements with the namespace
prefix "render". If you see the XML QName that you are interested in as one of
the "render" elements (based on the @match-ns/@match-name attributes), then you
can change the element tag name to "render:custom". If you do not see such an
element, then you can add your own "render:custom" element as a sibling of the
"render" elements.
Each element with QName "render:custom" must have a child element with QName
"render:apply" that has the attributes @ns (function namespace), @name
(function name), and @at (library module) to specify the function name and
library module for the custom rendering.
Let's step through a basic example.
I have used entity enrichment to mark up the text of my documents, and one of
the marked up items are URLs which get marked up with the namespace
"http://marklogic.com/entity" and tag name "url". An example is:
<e:url xmlns:e="http://marklogic.com/entity">http://marklogic.com</e:url>
On the "Content" page, we specified that this QName should be rendered as a
span, so we end up with the following element within the $TRANSFORMS variable
in config.xqy:
<render:generic-elem match-name="url"
match-ns="http://marklogic.com/entity" tag="span"
xmlns:render="http://marklogic.com/renderapi"/>
This "render" element will cause the example URL above to be rendered as:
<span>http://marklogic.com</span>
However, we want the URL to be rendered with an "a" tag and @href attribute,
like this:
<a href="http://marklogic.com">Mark Logic Corporation</a>
To do this, we replace the "render:generic-elem" element with the
"render:custom" element below:
<render:custom match-name="url" match-ns="http://marklogic.com/entity"
xmlns:render="http://marklogic.com/renderapi">
<render:apply ns="http://mycompany.com/custom-app" name="link-render"
at="/lib/extension.xqy" />
</render:custom>
Notice that we no longer need the @tag attribute.
Within the file "/lib/extension.xqy" (which is a file that was added after the
project was compiled), we have the function custom:link-render (where the
namespace prefix "custom" corresponds to the namespace
"http://mycompany.com/custom-app"). This function looks like this:
declare function custom:link-render($context as node(), $matchedrule as
element(), $rules as element()+, $params as xs:string*)
as element(a)
{
element {QName("http://www.w3.org/1999/xhtml","a")} {
attribute href { concat(if (not(starts-with($context/string(),
"http://"))) then "http://" else (), $context/string()) },
map:get($URL-MAP, $context/string())
}
};
Parameters:
$context - the XML element to be transformed Example:
<e:url xmlns:e="http://marklogic.com/entity">http://marklogic.com</e:url>
$matchedrule - the element with QName "render:custom" that represents the
custom transformation Example:
<render:custom match-name="url" match-ns="http://marklogic.com/entity"
xmlns:render="http://marklogic.com/renderapi">
<render:apply ns="http://marklogic.com/appservices/component"
name="link-render" at="/lib/extension.xqy" />
</render:custom>
$rules - a sequence of all the elements from the $TRANSFORMS variable with
namespace "render" that refer to rules for tranformation
$params - this variable must be included in the function signature. However,
for the purposes of extending a generated application, this parameter should
not be used within the function itself.
David Amusin
Software Engineer
Mark Logic Corporation
999 Skyway Road
Suite 200
San Carlos, CA 94070
+1 650 207 2308 Cell
+1 650 655 2310 Fax
[email protected]
www.marklogic.com
This e-mail and any accompanying attachments are confidential. The information
is intended solely for the use of the individual to whom it is addressed. Any
review, disclosure, distribution, or use of this e-mail communication by others
is strictly prohibited. If you are not the intended recipient, please notify
us by returning this message to the sender and delete all copies. Thank you
for your cooperation.
> ________________________________________
> From: [email protected]
> [[email protected]
> ] On Behalf Of Betty Harvey [[email protected]]
> Sent: Tuesday, June 30, 2009 1:27 PM
> To: General Mark Logic Developer Discussion
> Subject: [MarkLogic Dev General] Best Approach for Formatting
> Content From Application Builder
>
> I have started building an application using Application Builder.
> For most of my elements modifying the <render:generic-elem> tag along
> with CSS modifications work.
>
> There are some cases where I need more granularity in formatting.
> The documentation doesn't specify how to modify the <item-detail>
> component in the config.xqy file:
>
> <item-detail>
> <render:generic-elem match-name="Revision" match-ns="" tag="tr"
> xmlns:render="http://marklogic.com/renderapi"/>
> </item-detail>
>
> I need to do more manipulation of what goes into table cells that our
> outside the sequential flow of the information.
>
> Any advice/hints/tips is appreciated!
>
> Thanks!
>
>
> /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
> Betty Harvey | Phone: 410-787-9200 FAX: 9830
> Electronic Commerce Connection, Inc. |
> [email protected] | Washington,DC XML Users Grp
> URL: http://www.eccnet.com | http://www.eccnet.com/xmlug/
> /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\/\/
> _______________________________________________
> General mailing list
> [email protected]
> http://xqzone.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general