?
Solved

XSL minor change

Posted on 2009-12-16
17
Medium Priority
?
393 Views
Last Modified: 2013-11-18
Hi,

I got an xsl which transforming the source xml into final output. There need to be a minor change in the xsl file so this will produce the output we wanted. I have attached the XSL and the both XML file (source and the final output this xsl should produce) so can you show me how this should be done on the xsl file please.

Thanks in advance!

RequesXML.xml
AmericaDirectFareQuoteRequest1.xml
AmericaDirectFareQuoteRequest1.xml
0
Comment
Question by:ube100
  • 11
  • 6
17 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061020
Hi Ube, you did not pass in the result, but passed in the xslt twice.
It also helps if you told us where to look for changes.
Please be more specific in your questions and pass the expected result
0
 

Author Comment

by:ube100
ID: 26061100
Hi Gertone,
Sorry my mistake, please find the expected output xml here. This should produce:
 </RoomStayCandidates>
- <RoomStayCandidate>
- <GuestCounts>
  <GuestCount AgeQualifyingCode="10" Count="2" />
  <GuestCount AgeQualifyingCode="08" Age="11" Count="1" />
  </GuestCounts>
  </RoomStayCandidate>
- <RoomStayCandidate>
- <GuestCounts>
  <GuestCount AgeQualifyingCode="10" Count="1" />
  <GuestCount AgeQualifyingCode="08" Age="11" Count="2" />
  </GuestCounts>
  </RoomStayCandidate>
  </RoomStayCandidates>

instead of this:

  <RoomStayCandidates>
        <RoomStayCandidate>
          <GuestCounts>
            <GuestCount Count="2" AgeQualifyingCode="10" Age="19 and over" />
            <GuestCount Count="1" AgeQualifyingCode="8" Age="4" />
            <GuestCount Count="1" AgeQualifyingCode="8" Age="8" />
            <GuestCount Count="2" AgeQualifyingCode="8" Age="2" />
          </GuestCounts>
        </RoomStayCandidate>
      </RoomStayCandidates>
HotelAvailabilityRateByItemCode.xml
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061117
Do you realize by the way that this current XSLT has a major flaw?
It puts all your elements in the null namespace, not in the opentravel namespace.
I am not sure that is what you want.
I gave you a solution to that yesterday by the way
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061127
Can you do something about the source XML?
This current XML requires some clumsy grouping, which is not straightforward in XSLT1.

Or can you use XSLT2?
0
 

Author Comment

by:ube100
ID: 26061140
What is XSLT2 or XSLT1
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061146
Here I took care of the grouping.
It is less trivial then other things you have seen before, I believe
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:ota="http://www.opentravel.org/OTA/2003/05"
        version="1.0">
	<xsl:strip-space elements="*"/>
	<xsl:output indent="yes"/>
	<xsl:template match="/">
		<OTA_HotelAvailRQ xmlns="" Version="1.0" SummaryOnly="0" AllowPartialAvail="0" SequenceNmbr="1" EchoToken="123456">
			<AvailRequestSegments>
				<xsl:apply-templates select="Anteeo"></xsl:apply-templates>
			</AvailRequestSegments>
		</OTA_HotelAvailRQ>
	</xsl:template>
	<xsl:template match="Anteeo">
		<AvailRequestSegment>
			<StayDateRange Start="{normalize-space(Availability/StartDate)}" Duration="{normalize-space(Availability/Duration)}"/>
			<RoomStayCandidates>
				<xsl:for-each select="Room">
					<RoomStayCandidate>
						<GuestCounts>
							<xsl:if test="following-sibling::*[1][self::GuestCount]">
								<xsl:apply-templates select="following-sibling::*[1]"/>
							</xsl:if>
						</GuestCounts>
					</RoomStayCandidate>
				</xsl:for-each>
			</RoomStayCandidates>
			<HotelSearchCriteria>
				<xsl:for-each select="Hotel">
					<Criterion>
						<HotelRef HotelCodeContext="{normalize-space(HotelRef)}"/>
					</Criterion>
				</xsl:for-each>
			</HotelSearchCriteria>
		</AvailRequestSegment>
	</xsl:template>
	<xsl:template match="GuestCount">
			<GuestCount Count="{normalize-space(Count)}">
				<xsl:attribute name="AgeQualifyingCode">
					<xsl:choose>
						<xsl:when test="normalize-space(Type) = 'Adult'">
							<xsl:text>10</xsl:text>
						</xsl:when>
						<xsl:when test="normalize-space(Type) = 'Child'">
							<xsl:text>8</xsl:text>
						</xsl:when>
					</xsl:choose>
				</xsl:attribute>
				<xsl:if test="Age">
					<xsl:attribute name="Age">
						<xsl:value-of select="normalize-space(Age)"/>
					</xsl:attribute>
				</xsl:if>
			</GuestCount>
		<xsl:if test="following-sibling::*[1][self::GuestCount]">
			<xsl:apply-templates select="following-sibling::*[1]"/>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061151
I have done it using XSLT1 now, so you will be fine, don't worry.
XSLT2 is the second version of XSLT2. A lot functionality has been added,
but most tools are not ready yet, basically you can only use it if you are OK with using Saxon9 as the processor
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061159
I have built in some sort of tricky iteration
                                                       <xsl:if test="following-sibling::*[1][self::GuestCount]">
                                                                <xsl:apply-templates select="following-sibling::*[1]"/>
                                                        </xsl:if>
goes looking for the next element in row, only if it is a guestcount.
I do that inside the construction of the RoomStayCandidate
and I also do that inside the template for GuestCount, to see if the next is a GuestCount again, or a new Room
0
 

Author Comment

by:ube100
ID: 26061182
Can you correct that please as I cannot do any thing about source xml which is being produced by the application written by somebody else.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061210
correct what?
I made it compliant with your source.
I allready guessed you could not change the source.

If there is still something you want changed,
please be VERY specific about it,
explain the logic of what you want etc...
Experts Exchange is a forum for "assisting" you in doing things yourself.
In contrast to that, all your questions are like: "Here is what I want, do it"
Please take at least the effort of explaining in detail what you need,
it makes my live easier,
I have many questions to answer, so the sooner I know what to do, the faster that gets
0
 

Author Comment

by:ube100
ID: 26061237
Gertone,
IIgnore the last post please. Couple of things:

(1) <GuestCounts>
            <GuestCount Count="2" AgeQualifyingCode="10" Age="19 and over" />
            <GuestCount Count="1" AgeQualifyingCode="8" Age="2" />
          </GuestCounts>
For the AgeQualifyingcode 10 we don't need "age " attribute and the count should be the last attribute so the above should be:

<GuestCounts>
            <GuestCount  AgeQualifyingCode="10" Count="2"  />
            <GuestCount  AgeQualifyingCode="8" Age="2" Count="1"/>
          </GuestCounts>
otherwise, it's wonderfull. Thank you!!!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061341
The age attribute I can disable for code 10, that is not a problem, will do that in my next post

The order of attributes can not be changed.
Attribute order in XML is NOT relevant, by recommendation, attributes are an unordered nodeset.
That means that in the serialisation, you can't rely on the processor doing what you want
(most of the processors order alphabetticaly by attribute name)
I am afraid that using XSLT you can't get the attributes in the order you want.
BUT it SHOULD NOT matter to you.
The application handling your XML SHOULD be able to handle attributes in any order
if not, it is not an XML application.
It is highly recommended using real XML parsers to handle the data coming in
not to hack something together using a poor finite state machine and some regexes yourself

So tell me, why does your ttributes require a certain order?
If it is crucial (and it should not!) we will try to find a way
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061346
make the test somewhat more extensive on Age

                        <xsl:if test="Age and not(normalize-space(Type) = 'Adult')">
                              <xsl:attribute name="Age">
                                    <xsl:value-of select="normalize-space(Age)"/>
                              </xsl:attribute>
                        </xsl:if>
0
 

Author Comment

by:ube100
ID: 26061374
Thats fine then if this should not matter. The only reason I asked you was that is the order those attributes  are lined up on the request xml from  this web service.  I'm sorry If I increase your work load by not explaing things but in future I will explain as much possible as I can so that will make your life easier.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 26061398
OK, that is good, if there is a real webservice behind the response, then that will indeed not matter

Yep, giving more detail would be nice, indeed, thanks
0
 

Author Closing Comment

by:ube100
ID: 31666677
Once again thank you very much for your help!! I will need your wonderfull service in future.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 26061566
welcome
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question