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 November 6th, 2007, 11:41 AM
Registered User
 
Join Date: Nov 2007
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.
 
Old November 6th, 2007, 02:42 PM
Registered User
 
Join Date: Nov 2007
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.
 
Old November 7th, 2007, 06:39 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old November 7th, 2007, 06:53 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old November 7th, 2007, 10:01 AM
Registered User
 
Join Date: Nov 2007
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.
 
Old November 8th, 2007, 12:07 PM
Registered User
 
Join Date: Nov 2007
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?
 
Old November 8th, 2007, 03:14 PM
Registered User
 
Join Date: Nov 2007
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.





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





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