<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tojio Lab &#187; XML</title>
	<atom:link href="http://lab.tojio.com/category/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://lab.tojio.com</link>
	<description>"Tojio Labs is the place where little useful insights (and unimportant ones) from our everyday work as an interactive agency found their home."</description>
	<lastBuildDate>Fri, 14 May 2010 10:50:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unique ID for each node in an XML file [en]</title>
		<link>http://lab.tojio.com/2009/03/06/unique-id-for-each-node-in-an-xml-file-en/</link>
		<comments>http://lab.tojio.com/2009/03/06/unique-id-for-each-node-in-an-xml-file-en/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 12:58:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Data Management]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[IDs]]></category>
		<category><![CDATA[nodes]]></category>

		<guid isPermaLink="false">http://lab.tojio.com/?p=65</guid>
		<description><![CDATA[assigning each node of an XML file a unique ID]]></description>
			<content:encoded><![CDATA[<p>Well this is just a little, everyday task: I&#8217;m currently working on the <a href="http://www.sto.com/international" target="_blank">Sto International Website</a> and needed to modify an XML file so that each of its nodes has a unique ID assigned. The fastest way to achieve this is a very simple XSL transformation like the one below:<br />
<code><br />
&lt;?xml version="1.0" encoding="utf-8"?&gt;<br />
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<br />
&nbsp;&nbsp;&nbsp;&nbsp;version="1.0"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:output method="xml" indent="yes" /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This stylesheet just assigns a unique ID <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to each node in the XML tree<br />
&nbsp;&nbsp;&nbsp;&nbsp;--&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:template match="/"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:template&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:template match="*"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:element name=" { name (  )  } "&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:attribute name="nodeID"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:value-of select="generate-id (  ) " /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:attribute&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- copy any other attributes --&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:for-each select="attribute::*"&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:copy /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:for-each&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;!-- and do everything recursively --&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;xsl:apply-templates /&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:element&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/xsl:template&gt;<br />
&lt;/xsl:stylesheet&gt;</p>
<p></code><br />
You can use fairly any XSLT processor you want to let it do the job. In my case (Mac OS X), I just did it on the command line with xsltproc (Syntax: -o output-file.xml stylesheet.xsl input-file.xml):</p>
<pre>Xor:sto02 gertz$ xsltproc -o out.xml idmaker.xsl input.xml</pre>
]]></content:encoded>
			<wfw:commentRss>http://lab.tojio.com/2009/03/06/unique-id-for-each-node-in-an-xml-file-en/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
