Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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 July 19th, 2010, 10:29 AM
Registered User
 
Join Date: Jul 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default Remove Namespaces and Process Optional Elements?!

Hi All!!

Does anyone have a solution to convert the XML below which has optional Property elements and multiple namespaces (I've unsuccessfully tried stripping the namespaces using this approach http://dpawson.co.uk/xsl/sect2/N5536.html#d7035e1753):

Code:
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         UserId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" When="2010-07-07T16:42:26.1735814+01:00">
  <Body Name="ABC">
    <Properties xmlns="http://schemas.someone.com/xyz/1234">
      <Property xsi:type="UniqueID" Name="Field_A">
        <Value>12345678-1234-1234-1234-123456789012</Value>
      </Property>
      <Property xsi:type="List" Name="Field_B">
        <Value name="Default Value">1</Value>
      </Property>
      <Property xsi:type="String" Name="Field_C">
        <Value>Something</Value>
      </Property>
    </Properties>
  </Body>
</Message>
To something like this:

Code:
<Message>
  <Who>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</Who>
  <When>2010-07-07T16:42:26.1735814+01:00</When>
  <Type>ABC</Type>
  <Field_A>12345678-1234-1234-1234-123456789012</Field_A>
  <Field_B>1</Field_B>
  <Field_C>Something</Field_C>
</Message>
I've been banging my head against the walls for days on this!!! Help, Help, Help!!!!
 
Old July 19th, 2010, 10:39 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns1="http://schemas.someone.com/xyz/1234"
  version="1.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="Message">
    <Message>
      <xsl:apply-templates select="@UserId | @When | Body/@Name | Body/ns1:Properties/ns1:Property"/>
    </Message>
  </xsl:template>
  
  <xsl:template match="@*">
    <xsl:element name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="ns1:Property">
    <xsl:element name="{@Name}">
      <xsl:value-of select="ns1:Value"/>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
jgec1981 (July 19th, 2010)
 
Old July 19th, 2010, 10:39 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The FAQ that you've cited contains the answer. If you haven't made it work, you've done something wrong; so show us what you tried, and we can tell you where you went wrong.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 19th, 2010, 10:41 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

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xyz="http://schemas.someone.com/xyz/1234"
                version="1.0">
   <xsl:output indent="yes"/>

    <xsl:template match="Message">
    	<Message>
	    		  <Who>
            <xsl:value-of select="@UserId"/>
         </Who>
	    		  <When>
            <xsl:value-of select="@When"/>
         </When>
	    		  <Type>
            <xsl:value-of select="Body/@Name"/>
         </Type>
         <xsl:for-each select="Body/xyz:Properties/xyz:Property">
         	<xsl:element name="{@Name}"><xsl:value-of select="xyz:Value"/></xsl:element>
         	</xsl:for-each>
    		</Message>
    	</xsl:template>

</xsl:stylesheet>
__________________
/- 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:
jgec1981 (July 19th, 2010)
 
Old July 19th, 2010, 10:50 AM
Registered User
 
Join Date: Jul 2010
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Default

You guys are amazing..... All problems solved and thank you - this has really made my day
 
Old July 19th, 2010, 11:41 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

That output also assumes that the attribute Name contains a legal XML name. In some ways the input XML is a better design. Possibly a more usable schema would be:
Code:
<Message>
  <property name="Who">xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</property >
  <property name="When">2010-07-07T16:42:26.1735814+01:00</property >
  <property name="Type">ABC</property >
 <!-- etc. -->
</Message>
Which is also easier to search and process.
__________________
Joe
http://joe.fawcett.name/




Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove remove elements deep using xsl Prabeen XSLT 1 May 5th, 2009 12:55 AM
Optional element exists if has inner elements 2BOrNot2B XML 0 May 9th, 2008 02:11 PM
Inserting into a tree with optional elements iceandrews XSLT 2 April 24th, 2008 08:59 AM
Mapping of elements from 2 different Namespaces AjayLuthria XSLT 1 April 20th, 2007 07:34 AM
how to control optional elements and attributes NEO1976 XSLT 5 September 4th, 2006 02:58 AM





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