xsl-list
[Top] [All Lists]

Re: [xsl] Best way to represent an item and its context?

2011-09-15 12:09:20
Costello, Roger L. wrote:
Hi Folks,

Consider this XML document:

<?xml version="1.0"?>
<Book>
       <title>GML</title>
       <author>Ron Lake</author>
       <date>2004</date>
</Book>

Suppose that while processing that XML document I get to the author element:

       <author>Ron Lake</author>

I want to store this element and its context in a variable. The context of 
author is the entire document.

So I want a function that takes as input the author element and the Book 
element and returns a representation of the element and its context:

You said "The context of author is the entire document" so why is the Book _element_ the context, why not the root node selected with "/" or "root(Book/author)"?


item-in-context :: Item ->  Context ->  Item-in-Context

Read as: "The function item-in-context has two arguments -- an item (element) and a 
context -- and it returns a representation of the element and its context."

Here I invoke that function and store the result into a variable:

<xsl:template match="/">

       <xsl:variable name="v1" select="f:item-in-context(Book/author, Book)" />

</xsl:template>

I have various functions which operate on that Item-in-Context variable. For 
example, one function returns the item's parent:

parent :: Item-in-Context ->  Item

Here I invoke the parent function and store the result into a second variable:

<xsl:template match="/">

       <xsl:variable name="v1" select="f:item-in-context(Book/author, Book)" />

       <xsl:variable name="v2" select="f:parent($v1)" />

</xsl:template>

What's the best way to represent an item and its context?

I don't see why you need your functions and your "context" as in the XSLT/XPath tree data model you are always able to navigate with XPath e.g. you can access the parent node of a node $foo with $foo/parent::node() respectively $foo/.. and you are always able to access the root element with e.g. $foo/ancestor::*[last()] and the document node with e.g. root($foo).

As for representing the association between two nodes, well the only data structure XPath 2.0 offers is a sequence so you could use
  <xsl:sequence select="$Item, $Context"/>
in your function item-in-context to return a sequence.


--

        Martin Honnen --- MVP Data Platform Development
        http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--