xsl-list
[Top] [All Lists]

Re: [xsl] Name authority and ID/IDREF

2009-09-29 08:21:58
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="*[(_at_)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(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>