XSL minor change

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
ube100Asked:
Who is Participating?
 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
ube100Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
ube100Author Commented:
What is XSLT2 or XSLT1
0
 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
ube100Author Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
ube100Author Commented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
Geert BormansInformation ArchitectCommented:
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
 
ube100Author Commented:
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
 
ube100Author Commented:
Once again thank you very much for your help!! I will need your wonderfull service in future.
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.