Re: [xsl] foreign keys in a xml-database

Subject: Re: [xsl] foreign keys in a xml-database
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Fri, 03 May 2002 21:20:25 +0200
ChivaBaba@xxxxxxx wrote:
Hi folks,

does anybody know, whether there is a technique of defining foreign keys in a xml-database by xml or xsl??

Well, xml-database appears to be the wrong term here.

Assuming that you mean files with XML content, yes,
you can store stuff in a second file and refer to
parts of it during an XSL transformation. There is
no formal declaration of a foreign key in the sense
this term is used for RDBMS.

As example, lets say you have several documents like
this:

<primary-stuff>
  <some-stuff>
     <stuff-ref>a</stuff-ref>
     <stuff-ref>c</stuff-ref>
  </some-stuff>
  <some-more-stuff>
     <stuff-ref>b</stuff-ref>
     <stuff-ref>c</stuff-ref>
  </some-more-stuff>
</primary-stuff>

and the file where the referenced stuff is really stuffed,
i mean stored:
<stuff-base>
   <stuff><name>a</name><value>This is <b>a</b> stuff</value></stuff>
   <stuff><name>b</name><value>Some of <i>b</i> stuff</value></stuff>
   <stuff><name>c</name><value>c value</value></stuff>
</stuff-base>

You can pull in stuff values from the stuff base
during a transformation with the document() function.
The following will replace the stuff-ref elements
with the value of the referenced stuff in the output,
if properly applied:

 <xsl:template match="stuff-ref">
   <xsl:copy-of select="document('stuff-base.xml')/stuff-base/stuff[name=current()]/value"/>
 <xsl:template>

If your stuff base becomes large, you can define a key for
faster access. A key in XSLT is a similar, though not quite
the same as a primary or foreign key for RDBMS.
  <xsl:key name="stuff-ref" match="stuff" use="name"/>

 <xsl:template match="stuff-ref">
   <xsl:for-each select="document('stuff-base.xml')">
     <xsl:copy-of select="key('stuff-ref',.)/value"/>
   </xsl:for-each>
 <xsl:template>

The for-each may look like a loop, but it is actually
only used to change the context document for the key.

For further information and examples, look into your
favorite XSLT book, the XSLT spec, the mailing list
archive or the web in general for document() and xsl:key.

J.Pietschmann


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread