1

I need to generate unique IDs for elements across multiple XML files using XSLT. Each of the elements in question is already provided an ID that is gaurenteed to be unique within its own file, but can (and will) overlap with the IDs of nodes in other files.

My solution has been to concat the file path to the beginning of the existing ID, but this poses 2 problems:

  1. Publishing the filepath is a less than ideal solution, and may pose a legal/security risk.
  2. It makes for some very long, and verbose IDs filled with illegal characters (slashes, anyone?). The output is an HTML page, and many of the computers accessing it are old, and run ancient picky browsers.

This is the code I'm running with, currently:

<xsl:template match="box">
    <div class="box">
        <xsl:attribute name="id">
            <xsl:value-of select="$file_path" /><xsl:value-of select="./@id" />
        </xsl:attribute>

        ... blah blah content blah ...
    </div>
</xsl:template>

$file_path is passed in as a param automatically by the system.

So, yes, while this (technically) works, it is far from up to snuff. What I'd like to do is hash the variable $file_path before jamming together with the other ID. It doesn't have to be super secure, just obfuscated enough so that it doesn't resemble a path, is totally alpha-numeric, and is, preferably, shorter (bonus points if all hashes are the same length).

Finally (and this is the clincher) it almost certainly has to work in XSLT 1.0. It may be possible to persuade the powers that be to upgrade, but for all intents and purposes I need to know if this can be done without XSLT 2.

Sandy Gifford
  • 7,219
  • 3
  • 35
  • 65
  • Maybe this helps: http://stackoverflow.com/questions/4509662/how-to-generate-unique-string – Mark Veenstra Sep 11 '13 at 19:37
  • @MarkVeenstra Close, but if I'm understanding that answer correctly, that just puts an iterator before the ID. If I have two documents each with an elements with the same ID, and they both happen to be the *nth* element with an ID (quite likely given the scheme the system I'm working with uses for assigning IDs) then there will be overlap. – Sandy Gifford Sep 11 '13 at 19:47
  • 1
    Pass in some parameter, like a GUID and use that – Kevin Brown Sep 12 '13 at 00:20
  • @KevinBrown Hmmm, interesting. I COULD hash the URI outside of the XSLT, or use the file system's built in ID system (as I think you're suggesting). That could totally work! Submit it as an answer and I'll pick it. – Sandy Gifford Sep 12 '13 at 14:28

1 Answers1

1

Using an xsl:param, pass into the transform some unique key like a GUID. It would give you unique id's.

Kevin Brown
  • 8,805
  • 2
  • 20
  • 38