每个循环问题的XSLT转换 [英] XSLT Tranformation for-each looping problem
本文介绍了每个循环问题的XSLT转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个xslt
i have an xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Default template : ignore unrecognized elements and text -->
<xsl:template match="*|text()" />
<xsl:key name="room-per-hotel" match="result" use="concat(AccommodationId,'-', RoomId)" />
<!-- Match document root : add hotels element and process each children node of result -->
<xsl:variable name="doc2" select="."/>
<xsl:template match="/">
<!--<xsl:apply-templates/>-->
<!-- We assume that the XML documents are always going to follow the structure:
result as the root node and xml_acc elements as its children -->
<xsl:for-each select="$doc2/Success/result">
<xsl:if test="position() = 1">
<RuleMessage>
<xsl:attribute name="HotelCode">
<xsl:value-of select="AccommodationId"/>
</xsl:attribute>
<xsl:for-each select="$doc2/Success/result[count(. | key('room-per-hotel', concat(AccommodationIdAccommodationId,'-', RoomId))[1]) = 1]">
<xsl:sort select="concat(AccommodationId,'-', RoomId)" />
<xsl:variable name="doc1" select="."/>
<xsl:element name="StatusApplication">
<xsl:attribute name="RatePlanCode">
<xsl:value-of select="RoomIdRatePlanIdCode"/>
</xsl:attribute>
<xsl:attribute name="InvTypeCode">
<xsl:value-of select="RoomId"/>
</xsl:attribute>
<xsl:attribute name="RatePlanCategory">
<xsl:value-of select='"BB"'/>
</xsl:attribute>
</xsl:element>
<BookingRules>
<xsl:for-each select="$doc1/StartDate/tokenize(.,',')">
<xsl:variable name="pos" select="position()"/>
<xsl:if test="$doc1/OpenClose/tokenize(.,',')[position()=$pos] !='' ">
<xsl:element name="BookingRule">
<xsl:attribute name="Start" >
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:attribute name="End" >
<xsl:value-of select="."/>
</xsl:attribute>
<xsl:element name="RestrictionStatus">
<xsl:attribute name="Restriction">
<xsl:value-of select='"Master"'/>
</xsl:attribute>
<xsl:attribute name="Status" >
<xsl:value-of select="$doc1/OpenClose/tokenize(.,',')[position()=$pos]"/>
</xsl:attribute>
</xsl:element>
</xsl:element>
</xsl:if>
</xsl:for-each>
</BookingRules>
</xsl:for-each>
</RuleMessage>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
产生如下结果
which is producing the result as below
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<OTA_HotelBookingRuleRS xmlns="http://vbookings.bookingwhizz.com/connectAPI.asmx">
<RuleMessage HotelCode="5" xmlns="">
<StatusApplication RatePlanCode="101739608_1" InvTypeCode="101739608" RatePlanCategory="BB"/>
<BookingRules>
<BookingRule Start="2015-07-15" End="2015-07-15">
<LengthsOfStay>
<LengthOfStay MinMaxMessageType="SetMinLOS" TimeUnit="Day" Time="1"/>
</LengthsOfStay>
</BookingRule>
</BookingRules>
<StatusApplication RatePlanCode="101739608_2" InvTypeCode="101739608" RatePlanCategory="BB"/>
<BookingRules>
<BookingRule Start="2015-07-15" End="2015-07-15">
<LengthsOfStay>
<LengthOfStay MinMaxMessageType="SetMinLOS" TimeUnit="Day" Time="2"/>
</LengthsOfStay>
</BookingRule>
</BookingRules>
</RuleMessage>
</OTA_HotelBookingRuleRS>
</soap:Body>
</soap:Envelope>
但我需要如下结果
but i need the result as below
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<OTA_HotelBookingRuleRS xmlns="http://vbookings.bookingwhizz.com/connectAPI.asmx">
<RuleMessage HotelCode="5" xmlns="">
<StatusApplication RatePlanCode="101739608_1" InvTypeCode="101739608" RatePlanCategory="BB" />
<BookingRules>
<BookingRule Start="2015-07-14" End="2015-07-14">
<RestrictionStatus Restriction="Master" Status="Close" />
</BookingRule>
</BookingRules>
</RuleMessage>
<RuleMessage HotelCode="5" xmlns="">
<StatusApplication RatePlanCode="101739608_2" InvTypeCode="101739608" RatePlanCategory="BB" />
<BookingRules>
<BookingRule Start="2015-07-14" End="2015-07-14">
<RestrictionStatus Restriction="Master" Status="Close" />
</BookingRule>
</BookingRules>
</RuleMessage>
....
</OTA_HotelBookingRuleRS>
</soap:Body>
</soap:Envelope>
推荐答案
doc2/Success/result\">
<xsl:if test=\"position() = 1\">
<RuleMessage>
<xsl:attribute name=\"HotelCode\">
<xsl:value-of select=\"AccommodationId\"/>
</xsl:attribute>
<xsl:for-each select=\"
doc2/Success/result"> <xsl:if test="position() = 1"> <RuleMessage> <xsl:attribute name="HotelCode"> <xsl:value-of select="AccommodationId"/> </xsl:attribute> <xsl:for-each select="
doc2/Success/result[count(. | key('room-per-hotel', concat(AccommodationIdAccommodationId,'-', RoomId))[1]) = 1]\">
<xsl:sort select=\"concat(AccommodationId,'-', RoomId)\" />
<xsl:variable name=\"doc1\" select=\".\"/>
<xsl:element name=\"StatusApplication\">
<xsl:attribute name=\"RatePlanCode\">
<xsl:value-of select=\"RoomIdRatePlanIdCode\"/>
</xsl:attribute>
<xsl:attribute name=\"InvTypeCode\">
<xsl:value-of select=\"RoomId\"/>
</xsl:attribute>
<xsl:attribute name=\"RatePlanCategory\">
<xsl:value-of select='\"BB\"'/>
</xsl:attribute>
</xsl:element>
<BookingRules>
<xsl:for-each select=\"
doc2/Success/result[count(. | key('room-per-hotel', concat(AccommodationIdAccommodationId,'-', RoomId))[1]) = 1]"> <xsl:sort select="concat(AccommodationId,'-', RoomId)" /> <xsl:variable name="doc1" select="."/> <xsl:element name="StatusApplication"> <xsl:attribute name="RatePlanCode"> <xsl:value-of select="RoomIdRatePlanIdCode"/> </xsl:attribute> <xsl:attribute name="InvTypeCode"> <xsl:value-of select="RoomId"/> </xsl:attribute> <xsl:attribute name="RatePlanCategory"> <xsl:value-of select='"BB"'/> </xsl:attribute> </xsl:element> <BookingRules> <xsl:for-each select="
doc1/StartDate/tokenize(.,',')\">
<xsl:variable name=\"pos\" select=\"position()\"/>
<xsl:if test=\"
doc1/StartDate/tokenize(.,',')"> <xsl:variable name="pos" select="position()"/> <xsl:if test="
这篇关于每个循环问题的XSLT转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文