ilyaz Posted February 16, 2011 Share Posted February 16, 2011 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: <?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: <?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: <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! Link to comment Share on other sites More sharing options...
JohnBampton Posted February 21, 2011 Share Posted February 21, 2011 Hi, try this: <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 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:apply-templates select="container"></xsl:apply-templates> </data> </xsl:template> <xsl:template match="container"> <container type="{@type}"> <property name="GUID"> <xsl:value-of select="user:GetGUID()"/> </property> <xsl:apply-templates select="property"></xsl:apply-templates> <xsl:apply-templates select="container"></xsl:apply-templates> </container> </xsl:template> <xsl:template match="property"> <xsl:copy-of select="."/> </xsl:template></xsl:stylesheet> Regards, John Bampton. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.