如何生成唯一字符串

问题描述 投票:0回答:4
xslt
4个回答
6
投票

首先,我假设由于某种未知的原因,您无法使用

link
中的绝对URL作为所需的UID——这是最简单、最自然的解决方案。

如果我的假设是正确的,那么:

对于 XSLT 来说这是一项简单的任务

因为OP希望多次转换时生成的id相同,所以不适合使用

generate-id()
函数。

这是生成稳定 id 的一种简单方法

<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}&amp;no_cache={{{$vUid}}}"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(无论多少次):

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

每次都会产生想要的、相同的、正确的结果

<t>
   <a href="1.html&amp;no_cache={1}"/>
   <a>
      <a href="2.html&amp;no_cache={2}"/>
      <b>
         <a href="3.html&amp;no_cache={3}"/>
         <c>
            <a href="4.html&amp;no_cache={4}"/>
         </c>
         <a href="5.html&amp;no_cache={5}"/>
      </b>
      <a href="6.html&amp;no_cache={6}"/>
      <d>
         <a href="7.html&amp;no_cache={7}"/>
      </d>
   </a>
   <a href="8.html&amp;no_cache={8}"/>
   <e>
      <a href="9.html&amp;no_cache={9}"/>
   </e>
   <a href="10.html&amp;no_cache={10}"/>
</t>

请注意:使用

<xsl:number>
来生成 id。

如果同一个链接可以在文档中出现多次,并且我们需要所有出现都使用相同的 id,这里是此问题的解决方案:

<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}&amp;no_cache={{{$vUid}}}"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时

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

产生了想要的正确结果

<t>
   <a href="1.html&amp;no_cache={1}"/>
   <a>
      <a href="2.html&amp;no_cache={2}"/>
      <b>
         <a href="1.html&amp;no_cache={1}"/>
         <c>
            <a href="3.html&amp;no_cache={3}"/>
         </c>
         <a href="2.html&amp;no_cache={2}"/>
      </b>
      <a href="1.html&amp;no_cache={1}"/>
      <d>
         <a href="3.html&amp;no_cache={3}"/>
      </d>
   </a>
   <a href="4.html&amp;no_cache={4}"/>
   <e>
      <a href="2.html&amp;no_cache={2}"/>
   </e>
   <a href="4.html&amp;no_cache={4}"/>
</t>

4
投票

尝试generate-id()。

<xsl:value-of select="generate-id(.)"/>

2
投票

纯 XSLT 是不可能的,但一些替代选项可能是:

  1. 添加扩展命名空间,以便您可以调用非 XSLT 代码:
    <a href="1.html&no_cache={myns:unique_id()}">
    。这将为您提供所需的结果,但确实取决于您用于执行转换的框架的支持。
  2. 使用 JavaScript 将唯一 ID 添加到客户端的链接中。仅当您的客户端启用了 JavaScript 时才有效,但如果您知道会出现这种情况,则可能是可接受的折衷方案。
  3. 在页面上设置 HTTP 标头以防止缓存。从语义的角度来看,这可能是最好的选择,并且您不会冒搜索引擎使用每个唯一 ID 重复抓取您的页面的风险。

0
投票

XSLT 是一种函数式语言,这意味着对于给定的输入,它将始终产生相同的输出,因此根据定义,guid 方法或任何其他随机生成器不会成为设计规范的一部分。如果您受客户端限制,最好的选择是使用与时间相关的方法作为生成 id 的伪随机种子的一部分,但是由于您的目标似乎是强大的脱缓存,因此您应该放弃这种方法,而只专注于应用正确的方法您要保护的资源的反缓存标头。

© www.soinside.com 2019 - 2024. All rights reserved.