Generate GUID in XSLT

旧巷老猫 提交于 2019-12-06 22:29:29

问题


I need to generate a GUID with XSLT and if needed C#, does anyone know how to best do this?

It is to generate unique IDs for HTML items.


回答1:


The XSLT generate-id function returns a string that uniquely identifies a node in the document. Note these warnings from the spec:

An implementation is under no obligation to generate the same identifiers each time a document is transformed. There is no guarantee that a generated unique identifier will be distinct from any unique IDs specified in the source document.

However, if all you need is to uniquely identify each element in your output, then generate-id is sufficient.




回答2:


C# provides a handy Guid.NewGuid() static method. I'd expect any XSLT implementation would heavily leverage some system-specific component since Guids are often generated in part based on hardware/MAC address/etc. on the underlying machine.




回答3:


I ended up just using an extension method and wrapping Guid.NewGuid() in a static method, then calling this from my XSLT, it was easy enough once I figured out how extension methods work.




回答4:


With C#, it can be achieved easily with Script Blocks Using msxsl:script.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:user="urn:my-scripts">
  <msxsl:script language="C#" implements-prefix="user">
  <![CDATA[
  public string getguid(){
     return Guid.NewGuid().ToString();
  }
  ]]>
  </msxsl:script>
  <xsl:template match="data">
    <Guid><xsl:value-of select="user:getguid()"/></Guid>
  </xsl:template>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/5494175/generate-guid-in-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!