Subject: [xsl] generating ID strings that are both readable and unique From: "Trevor Nicholls" <trevor@xxxxxxxxxxxxxxxxxx> Date: Tue, 14 Oct 2008 19:25:49 +1300 |
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[@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
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XML Namespaces and <xsl:o, Alejandro | Thread | Re: [xsl] generating ID strings tha, Xmlizer |
Re: [xsl] using position() inside M, Eric Scheid | Date | Re: [xsl] generating ID strings tha, Xmlizer |
Month |