Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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!
Reply With Quote
  #2 (permalink)  
Old February 17th, 2011, 06:57 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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?
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
Old February 22nd, 2011, 05:10 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
ilyaz (February 23rd, 2011)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:30 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.