xsl-list
[Top] [All Lists]

Re: [xsl] generating ID strings that are both readable and unique

2008-10-14 02:44:08
Dear,

Give a try to use generate-id(), perhaps by using it with concat()

Xmlizer

On Tue, Oct 14, 2008 at 8:25 AM, Trevor Nicholls
<trevor(_at_)castingthevoid(_dot_)com> wrote:
Hi

In this particular application we have a set of XML documents which are
divided into nested sections; each section may (down the track) give rise to
a url. Currently that url is generated by <xsl:number level="multiple"> but
this produces urls that change frequently. Some sections have been given an
ID attribute by the process which originally created the documents, but most
have not.
Additionally, all sections must have exactly one title child, along with
their other content.

The requirement is to process an XML file and generate an ID attribute for
sections which lack them - deriving the ID value from the title so that the
url is comprehensible. Providing we ignore the problem cases, this is a
trivial exercise:

----

 <xsl:variable name="upchars" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
 <xsl:variable name="lochars" select="'abcdefghijklmnopqrstuvwxyz'" />

 <!-- catchall -->
 <xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates />
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:copy-of select="." />
 </xsl:template>

 <xsl:template match="section[(_at_)id]">
  <xsl:copy>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates />
  </xsl:copy>
 </xsl:template>

 <xsl:template match="section">
  <xsl:copy>
    <xsl:attribute name="id">
      <xsl:apply-templates select="title" mode="id" />
    </xsl:attribute>
    <xsl:apply-templates select="@*" />
    <xsl:apply-templates />
  </xsl:copy>
 </xsl:template>

 <xsl:template match="title" mode="id">
  <xsl:value-of select="translate(translate(.,' ','_'),$upchars,$lochars)"
/>
 </xsl:template>

----

The problem cases are
(a) duplicate titles (after the translations) which would lead to duplicate
IDs, and
(b) existing IDs which might also duplicate a title.

If there were no IDs in the document to begin with, I think I could have
solved the first problem by using a key. But the second problem complicates
it, and I haven't got enough experience with keys to figure out how to
adjust the "id" mode title template to take both issues into account.

Can anyone offer some helpful advice here?
XSL 1.0 is preferred, although I would be interested to see how XSL2 might
handle this problem too.

Thanks
Trevor



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



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