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 November 6th, 2007, 11:41 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Prefix and Namespace to XML

I'm trying to take an XML file from a client that has no namespace definitions and convert it to one that does. This is so I can convince BT2006 to actually read the file.

I have multiple namespaces that I need to assign. I've seen it said several times "modified identity transform" but I'm really not wrapping my head around this idea. This is probably not the best first XSL project I could have picked, but I didn't get to pick it. Can anyone help me with this, please?

The client's XML (greatly reduced):
<Participants>
  <Participant>
    <insuranceStartDate>2006-06-20T00:00:00.0000000-04:00</insuranceStartDate>
    <insuranceEndDate>2006-08-31T00:00:00.0000000-04:00</insuranceEndDate>
    <serialNumber>1111111</serialNumber>
    <medicalNotes />
    <personChangeCode>C</personChangeCode>
    <programType>Program</programType>
    <OrganizationName>Organization</OrganizationName>
    <benefitAmount>0</benefitAmount>
    <person>
      <FirstName>FName</FirstName>
      <MiddleName>MName</MiddleName>
      <LastName>LName</LastName>
      <SuffixName />
      <GenderCode>F</GenderCode>
      <BirthDate>1986-02-04T00:00:00.0000000-05:00</BirthDate>
      <PhoneNumber />
      <EmailAddress />
      <CitizenshipCountry>USA</CitizenshipCountry>
    </person>
  </Participant>
</Participants>

What I need:

<ns0:Participants xmlns:ns0="http://COINS_SCHEMAS.Participants">
  <ns1:Participant xmlns:ns1="http://COINS_SCHEMAS.participantType">
    <insuranceStartDate>2006-06-20T00:00:00.0000000-04:00</insuranceStartDate>
    <insuranceEndDate>2006-08-31T00:00:00.0000000-04:00</insuranceEndDate>
    <serialNumber>1111111</serialNumber>
    <medicalNotes />
    <personChangeCode>C</personChangeCode>
    <programType>Program</programType>
    <OrganizationName>Organization</OrganizationName>
    <benefitAmount>0</benefitAmount>
    <ns2:person xmlns:ns2="http://COINS_SCHEMAS.personType">
      <FirstName>FName</FirstName>
      <MiddleName>MName</MiddleName>
      <LastName>LName</LastName>
      <SuffixName />
      <GenderCode>F</GenderCode>
      <BirthDate>1986-02-04T00:00:00.0000000-05:00</BirthDate>
      <PhoneNumber />
      <EmailAddress />
      <CitizenshipCountry>USA</CitizenshipCountry>
    </ns2:person>
  </ns1:Participant>
</ns0:Participants>

Alternatively, and probably belonging in a different forum, if there was a way to make BizTalk not worry about the prefix/namespace definitions on the records I wouldn't need to do the transform.
Reply With Quote
  #2 (permalink)  
Old November 6th, 2007, 02:42 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, I simplified this a lot inside BT. Now I need only one namespace/prefix added. This is the output I need:

<ns0:Participants xmlns:ns0="http://COINS_CHEMAS.COINS_EXPORT">
  <ns0:Participant>
    <ns0:insuranceStartDate>2006-06-20T00:00:00.0000000-04:00</ns0:insuranceStartDate>
    <ns0:insuranceEndDate>2006-08-31T00:00:00.0000000-04:00</ns0:insuranceEndDate>
    <ns0:serialNumber>1111111</ns0:serialNumber>
    <ns0:medicalNotes />
    <ns0:personChangeCode>C</ns0:personChangeCode>
    <ns0:programType>Program</ns0:programType>
    <ns0:OrganizationName>Organization</ns0:OrganizationName>
    <ns0:benefitAmount>0</ns0:benefitAmount>
    <ns0:person>
      <ns0:FirstName>FName</ns0:FirstName>

etc.

I'm still clueless as to how to make this happen, but it's got to be easier than the first example.
Reply With Quote
  #3 (permalink)  
Old November 7th, 2007, 06:39 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There are three elements you want to rename: Participants, Participant, and person. The other elements should all be copied unchanged. (The name of an element is in two parts: namespace URI and local name. Changing either part is best thought of as a renaming operation).

So you need four template rules. The default rule copies things unchanged:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

and the others do a renaming. Here is one of them:

<xsl:template match="Participants">
  <ns0:Participants xmlns:ns0="http://COINS_SCHEMAS.Participants">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </ns0:Participants>
</xsl:template>

The others are similar.

>if there was a way to make BizTalk not worry about the prefix/namespace definitions on the records I wouldn't need to do the transform.

That's a bit like me asking my bank manager to accept instructions from anyone called Mike, regardless of the surname.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old November 7th, 2007, 06:53 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

I've used BizTalk a few times and never come across this restriction. It can struggle with non-namespaced documents because it relies on XML schemas which generally use namespaced nodes, but it doesn't worry about the actual prefix/URI mapping.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #5 (permalink)  
Old November 7th, 2007, 10:01 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Joe: I've manually edited files to gives them the proper namespace declarations and it will actually take them. That's the reason I think this is the solution I need. This is also my first BT project so I'm sure I'm missing things along the way but I just want this thing to get going.

mhkay: I'm not working today but it looks like what you've given me should do what I need. I'll test it tomorrow. Thanks so much.
Reply With Quote
  #6 (permalink)  
Old November 8th, 2007, 12:07 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think this worked but I have a question. When I use a browser I'm getting unexpected output. I'm using <xsl:output method="xml" version="1.0"> and the browser just outputs unformatted data rather than pretty xml with my new namespaces applied. Is this what I should expect?
Reply With Quote
  #7 (permalink)  
Old November 8th, 2007, 03:14 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yep. It works like a charm. Still having BT issues but this got me a lot closer. Thanks again.
Reply With Quote
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
Remove namespace prefix from XmlBean ratzko BOOK: Professional Java Development with the Spring Framework 0 August 10th, 2008 01:23 PM
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM
How a additional prefix in a namespace comes? diang BOOK: ASP.NET Website Programming Problem-Design-Solution 0 July 13th, 2006 10:40 PM
change namespace leaving prefix unchanged lantoli XSLT 3 June 26th, 2006 07:31 AM
attribute namespace prefix tsmith XSLT 1 August 12th, 2004 06:01 AM



All times are GMT -4. The time now is 11:01 AM.


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