[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Name authority and ID/IDREF

Subject: Re: Name authority and ID/IDREF
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 29 Sep 2009 08:17:11 -0400
Re:  Name authority and ID/IDREF
At 2009-09-28 22:02 -0600, Eric wrote:
I am trying to use ID/IDREF to create what amounts to a name authority
file within my XML.  The goal is to allow one section of the XML to
contain commonly used data (such as names of people), and allow the
other sections to access that information, and the information
contained within, via the ID ref.  I'm including an example of what
I'm trying to do after my signature.

I'm using XSLT 1.0, though a 2.0 solution would be welcome as well.
Apologies: I am definitely a novice, so this may be really simple.  I
spent a fair amount of time attempting to find discussion of this on
the archives of the list, but never really knew what terms would make
a good search string, and so could easily have missed the discussion,
if it exists.  Thanks in advance for any help!

An example is below of using the id() function ... since you have a DTD. A second example doesn't use the id() function, in case you don't have a DTD, but in the interest of brevity it only supports zok= being of type IDREF and not IDREFS ... not sure if that was a specific requirement of yours or just habitual typing that made it plural.


Note that when using keys in XSLT 1.0, the reliance is on the *name* of the attribute and not the type of the attribute. When using ID/IDREFS, at least the ID attribute declarations must be processed in the reading of the source tree.

I hope this helps.

. . . . . . . . . Ken

t:\ftemp>type eric.xml
<!DOCTYPE test
[
<!ELEMENT test (foo*, bar*) >
<!ELEMENT foo (testa, testb) >
<!ATTLIST foo
    ID    ID    #REQUIRED>

<!ELEMENT bar (testc, testd) >
<!ATTLIST bar
    zok    IDREFS         #REQUIRED>
<!ELEMENT testa ( #PCDATA )>
<!ELEMENT testb ( #PCDATA )>
<!ELEMENT testc ( #PCDATA )>
<!ELEMENT testd ( #PCDATA )>
]>
<test>
    <foo ID="ack">
        <testa>blah</testa>
        <testb>blah2</testb>
    </foo>
    <foo ID="grr">
        <testa>argh</testa>
        <testb>argh2</testb>
    </foo>

    <bar zok="ack">
        <testc>blah3</testc>
        <testd>blah4</testd>
    </bar>
</test>

t:\ftemp>type eric.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:apply-templates select="//bar"/>
</xsl:template>

<xsl:template match="bar">
  <xsl:for-each select="id(@zok)/*">
    <xsl:value-of select="."/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
  <xsl:for-each select="*">
    <xsl:value-of select="."/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


t:\ftemp>xslt eric.xml eric.xsl blah blah2 blah3 blah4

t:\ftemp>type eric2.xml
<test>
    <foo ID="ack">
        <testa>blah</testa>
        <testb>blah2</testb>
    </foo>
    <foo ID="grr">
        <testa>argh</testa>
        <testb>argh2</testb>
    </foo>

    <bar zok="ack">
        <testc>blah3</testc>
        <testd>blah4</testd>
    </bar>
</test>

t:\ftemp>type eric2.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:key name="ids" match="*[@ID]" use="@ID"/>

<xsl:template match="/">
  <xsl:apply-templates select="//bar"/>
</xsl:template>

<xsl:template match="bar">
  <xsl:for-each select="key('ids',@zok)/*">
    <xsl:value-of select="."/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
  <xsl:for-each select="*">
    <xsl:value-of select="."/>
    <xsl:text>
</xsl:text>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


t:\ftemp>xslt eric2.xml eric2.xsl blah blah2 blah3 blah4

t:\ftemp>


-- Upcoming hands-on code list, UBL, XSLT, XQuery and XSL-FO classes. Interested in other classes? http://www.CraneSoftwrights.com/s/i/ Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.