[Home] [By Thread] [By Date] [Recent Entries]
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. 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>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>
|

Cart



