I want to transform an XML document into HTML. Some XML elements have links to others documents like:
In the H
XSLT is a functional languages which means for a given input it will always product the same output, so by definition a guid method or any other random generator would not be part of the design spec. Your best bet if you're client bound is to use a time-related method as part of a pseudo-random seed for generate-id, however as your goal appears to be strong decaching you should abandon this and just focus on applying the correct anti-cache headers to the resources you're trying to protect.
To start with, I assume that due to some unknown reason you cannot use the absolute URL in the link
as the required UID -- this is the simplest and most natural solution.
In case my assumption is correct, then:
This is an easy task for XSLT.
Because the OP wants the generated ids to be the same when the transformation is performed several times, it isn't appropriate to use the generate-id()
function.
Here is one simple way of producing stable ids:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link[@href]">
<xsl:variable name="vUid">
<xsl:number level="any" count="link[@href]"/>
</xsl:variable>
<a href="{@href}&no_cache={{{$vUid}}}"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document (regardless how many times):
<t>
<link href="1.html"/>
<a>
<link href="2.html"/>
<b>
<link href="3.html"/>
<c>
<link href="4.html"/>
</c>
<link href="5.html"/>
</b>
<link href="6.html"/>
<d>
<link href="7.html"/>
</d>
</a>
<link href="8.html"/>
<e>
<link href="9.html"/>
</e>
<link href="10.html"/>
</t>
the wanted, same, correct result is produced every time:
<t>
<a href="1.html&no_cache={1}"/>
<a>
<a href="2.html&no_cache={2}"/>
<b>
<a href="3.html&no_cache={3}"/>
<c>
<a href="4.html&no_cache={4}"/>
</c>
<a href="5.html&no_cache={5}"/>
</b>
<a href="6.html&no_cache={6}"/>
<d>
<a href="7.html&no_cache={7}"/>
</d>
</a>
<a href="8.html&no_cache={8}"/>
<e>
<a href="9.html&no_cache={9}"/>
</e>
<a href="10.html&no_cache={10}"/>
</t>
Do note: The use of <xsl:number>
to produce the id.
If the same link can occur several times in the document and we need all occurences to use the same id, here is the solution for this problem:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kHrefByVal" match="link/@href" use="."/>
<xsl:variable name="vUniqHrefs" select=
"//link/@href
[generate-id()
=
generate-id(key('kHrefByVal',.)[1])
]
"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link[@href]">
<xsl:variable name="vthisHref" select="@href"/>
<xsl:variable name="vUid">
<xsl:for-each select="$vUniqHrefs">
<xsl:if test=". = $vthisHref">
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<a href="{@href}&no_cache={{{$vUid}}}"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<t>
<link href="1.html"/>
<a>
<link href="2.html"/>
<b>
<link href="1.html"/>
<c>
<link href="3.html"/>
</c>
<link href="2.html"/>
</b>
<link href="1.html"/>
<d>
<link href="3.html"/>
</d>
</a>
<link href="4.html"/>
<e>
<link href="2.html"/>
</e>
<link href="4.html"/>
</t>
the wanted, correct result is produced:
<t>
<a href="1.html&no_cache={1}"/>
<a>
<a href="2.html&no_cache={2}"/>
<b>
<a href="1.html&no_cache={1}"/>
<c>
<a href="3.html&no_cache={3}"/>
</c>
<a href="2.html&no_cache={2}"/>
</b>
<a href="1.html&no_cache={1}"/>
<d>
<a href="3.html&no_cache={3}"/>
</d>
</a>
<a href="4.html&no_cache={4}"/>
<e>
<a href="2.html&no_cache={2}"/>
</e>
<a href="4.html&no_cache={4}"/>
</t>
It's not possible with pure XSLT, but some alternative options might be:
<a href="1.html&no_cache={myns:unique_id()}">
. This will give you the result you're after, but does depend on support from the framework you're using to perform the transformation.try generate-id().
<xsl:value-of select="generate-id(.)"/>
here is a further explanation: http://www.w3schools.com/XSL/func_generateid.asp