开发者

How to add an unique valued attribute to every tag in the xml using XSL

开发者 https://www.devze.com 2022-12-27 10:57 出处:网络
I want to add a unique attribute say \"ind\" to every tag in the xml. How do i do 开发者_运维技巧it using xsl. It need\'nt be a sequence number. As long it is unique for every tag it is sufficient.Tak

I want to add a unique attribute say "ind" to every tag in the xml. How do i do 开发者_运维技巧it using xsl. It need'nt be a sequence number. As long it is unique for every tag it is sufficient.


Take identity transformation, add template for elements in which add attribute with value generated by generate-id().


Something like this?

It also uses a unique namespace for the attribute we are adding so we don't override any existing attributes with ours if they have the same name.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:mycomp="http://www.myuniquenamespace">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="node()">
  <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="someattr" namespace="http://www.myuniquenamespace">
      <xsl:value-of select="generate-id()"/>
    </xsl:attribute>
    <xsl:apply-templates select="node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@* | text()">
  <xsl:copy />
</xsl:template>

</xsl:stylesheet>

Hope this helps you on your way,

0

精彩评论

暂无评论...
验证码 换一张
取 消