I need to transform a bunch of XMLs that have the following structure: each has an element called "container" of type "Box" which has a sub-element "container" of type "Folder" and it has another sub-element of type "Document". Each container has some properties, so a typical XML looks like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<container type="Box" >
<property name="BoxPrName1">BoxPrName1val</property>
<property name="BoxPrName2">BoxPrName2val</property>
<container type="Folder">
<property name="FoldPrName1">FoldPrName1val</property>
<property name="FoldPrName2">FoldPrName2val</property>
<container type="Document">
<property name="DocPrName1">DocPrName1val</property>
<property name="DocPrName2">DocPrName2val</property>
</container>
</container>
</container>
</data>
In this example there is only one child for each parent but I can also have multiple folders in one box and multiple docs in one folder.
All I need is to add to each container a property called GUID. I know how to generate the GUID itself using a script, but I am having trouble modifying all the containers. Instead, I only modify the top one. So the result has to look like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<container type="Box" >
<property name="GUID">GUIDval1</property>
<property name="BoxPrName1">BoxPrName1val</property>
<property name="BoxPrName2">BoxPrName2val</property>
<container type="Folder">
<property name="GUID">GUIDval2</property>
<property name="FoldPrName1">FoldPrName1val</property>
<property name="FoldPrName2">FoldPrName2val</property>
<container type="Document">
<property name="GUID">GUIDval3</property>
<property name="DocPrName1">DocPrName1val</property>
<property name="DocPrName2">DocPrName2val</property>
</container>
</container>
</container>
</data>
I use the following stylesheet:
Code:
<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 System.Guid.NewGuid().ToString();
}
]]>
</msxsl:script>
<xsl:template match="data">
<data>
<xsl:for-each select="container">
<container>
<xsl:copy-of select="node()"/>
<property name="GUID">
<xsl:value-of select="user:GetGUID()"/>
</property>
</container>
</xsl:for-each>
</data>
</xsl:template>
</xsl:stylesheet>
which only adds a GUID to the top Box, but ignores Folders and Documents. How do I have to modify it to get what I need? Thanks!