Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 16th, 2011, 04:11 PM
Authorized User
 
Join Date: Dec 2010
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default Adding element at different levels of hierarchy

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!
 
Old February 17th, 2011, 06:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You best bet is rather than using xsl:for-each to write templates instead.

Start with the identity template:

Code:
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>
Then write another template to handle <container> elements:

Code:
<xsl:template match="container">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <property name="GUID"><xsl:value-of select="user:GetGUID()"/></property>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old February 22nd, 2011, 05:03 PM
Authorized User
 
Join Date: Dec 2010
Posts: 16
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Sam, thanks, it mostly worked. Here's what my s/s looks like now:
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="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="container">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <property name="GUID"><xsl:value-of select="user:GetGUID()"/></property>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
The resulting XML has all the pieces, but the GUID property has extra stuff in it that I do not need:

Code:
<data>
  <container type="Box">
    <property name="GUID" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">1a07c598-36d0-432e-a63b-607867ae720e</property>
...
How can the
Code:
xmlns:msxsl="urn:schemas...
be removed?

thanks much
 
Old February 22nd, 2011, 05:10 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You'll want the exclude-prefixes attribute on the xsl:stylesheet element.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
ilyaz (February 23rd, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a newline at the end of each element ehsansad XSLT 9 October 22nd, 2010 09:45 AM
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Adding a new Element monuindia2002 XML 2 March 13th, 2006 11:59 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
Adding an element to a binded DropDownlist mahulda ASP.NET 1.0 and 1.1 Basics 3 March 10th, 2004 03:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.