Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - [Newbie] One XPath expression for inline and referenced elements

 
Thread Tools Search this Thread
Old 05-17-2006, 09:32 AM   #1
Default [Newbie] One XPath expression for inline and referenced elements


Hi,

I'd like to model the following using schema definition (XSD) and I'd like
to express paths using (XPath).

1. Is it possible to define a xsd for rootBook? As you can see, a <rootBook>
can contain inline <chapter> definition and references to a loose <chapter>
outside of <rootBook>.

<rootBook>
<chapter id="1">
<paragraph>Para 1</paragraph>
<paragraph>Para 2</paragraph>
<paragraph>Para 3</paragraph>
</chapter>
<chapter ref="2"/>
</rootBook>

<chapter id="2">
<paragraph>Para 4</paragraph>
<paragraph>Para 5</paragraph>
<paragraph>Para 6</paragraph>
</chapter>

2. And then how do I define "one" XPath expression that caters for both
inline and external chapters?

expression: //rootBook/chapter[@id="1"]/*
returns:
/rootBook[1]/chapter[1]/paragraph[1] - Para 1
/rootBook[1]/chapter[1]/paragraph[2] - Para 2
/rootBook[1]/chapter[1]/paragraph[3] - Para 3

I want this expression to return me the following to, when the queried id is
"2":
/rootBook[1]/chapter[2]/paragraph[1] - Para 4
/rootBook[1]/chapter[2]/paragraph[2] - Para 5
/rootBook[1]/chapter[2]/paragraph[3] - Para 6

Many thanks.

-rach




Rach
  Reply With Quote
Old 05-17-2006, 12:48 PM   #2
Andy Dingley
 
Posts: n/a
Default Re: One XPath expression for inline and referenced elements


Rach wrote:

> 2. And then how do I define "one" XPath expression that caters for both
> inline and external chapters?


You don't? As anyone who has tried to manipulate RDF through
XSLT can tell you, this is a painful process. Actually for your
example it should work fine - but it gets _very_ tiresome for RDF.

The trick is to use
<xsl:key name="ext-chapters" match="/chapter" use="@id" />

Then reference them via the id value, using the ref attribute of the
referring chapter


As originally stated, your example is trivial. If you already have the
id value, then just locate them with //chapter [@id = $id-requested]

However I think what you're really after is how to process a set of
chapters and find their content, transparently whether they're in-line
or referenced. So give this a try


<xsl:key name="ext-chapters" match="//chapter" use="@id" />

<xsl:template name="output-chapter" >
<xslaram name="chapter" select="/.." />
<xsl:apply-templates select="$chapter [@id] /* |
key('ext-chapters', $chapter/@ref )/* " />
</xsl:template>


<xsl:template name="iterate-chapters" >
<xsl:for-each select="//rootBook/chapter " >
<xsl:call-template name="output-chapter" >
<xsl:with-param name="chapter" select="." />
</xsl:call-template>
</xsl:for-each>
</xsl:template>

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump