Simian7
Simian7

Reputation: 1

XSLT to XML - Parse part of the XML (major output issues)

I am posting a question for the first time, so please accept my apologies if I done something incorrect. . Essentially what I am attempting is to create an XML from XML using XSLT. I attempted to reference examples online but I could not find anything like it. What I am trying could broken into 3 main steps:

  1. Setting up a template for 3 separate methods. create, update and purge. Create and Update require some logic to transform the XML whereas Purge method will parse the XML through.

  2. Parsing part of the XML from PromoMessage tag down to the closing tag for distribution.

  3. Fields output actually in the XML to contain values for the tags i'm referencing.

Here are two examples of the source XMLs:

Source 1 is the most complicated source which requires logic to select the interstitial category and promoted programme type, the method create should be reference to use this logic. An update method would work the same way:

<?xml version="1.0" ?>
<PromoMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Header origin="IBMSV6" method="create" timestamp="2013-08-06T06:32:17Z" />
<Body>
<CreatePromo providerId="NJR" promoID="245686_NJ01858S ">
<Distribution>
<RightsWindow start="2013-07-31T14:00:00Z" end="2013-08-31T13:59:59Z" />
<Destinations>
<Destination platform="WCMS" site="EPG" visible="true">
<DisplayWindow start="2013-07-31T14:00:00Z" end="2013-08-31T13:59:59Z" />
</Destination>
</Destinations>
</Distribution>
<Interstitial category= "episodic" multi_prog="false" name="Promo Alpha" parentalRating ="PG"  >
<PromotedProgramme type ="MovieOrEvent" seq_number ="1" > //promoted prog non-series
<TitleId>000000218564</TitleId>
<ExternalProgId>785432</ExternalProgId>
<TitleBroadcastRef>078654</TitleBroadcastRef>
<TitleName>Men In Black</TitleName>
</PromotedProgramme>
<PromotedProgramme type ="MoviefssOrEvent" seq_number ="2"> <!--promoted slot non-series-->
<SlotId>675342</SlotId>
<SlotName>Late Movie</SlotName>
<TitleId>000000218576</TitleId>
<ExternalProgId>785632</ExternalProgId>
<TitleBroadcastRef>078123</TitleBroadcastRef>
<TitleName>Die Hard</TitleName>
<EPGTitleName>Die Hard</EPGTitleName>
</PromotedProgramme>
<PromotedProgramme type ="Series" seq_number ="3"> <!--promoted prog series-->
<TitleId>000000218321</TitleId>
<ExternalProgId>785122</ExternalProgId>
<TitleBroadcastRef>075432</TitleBroadcastRef>
<TitleName> Friends S5 Ep5</TitleName>
<EpisodeNo>10</EpisodeNo>
<SeasonId>10645346</SeasonId>
<SeasonName>Friends</SeasonName>
<SeasonNo>5</SeasonNo>
<SeasonYear>5</SeasonYear>
<ShowId>10987532</ShowId>
<ShowName>Friends</ShowName>
<EPGTitleName>Friends</EPGTitleName>
</PromotedProgramme>
<PromotedProgramme type ="Slot" seq_number ="4"> <!--promoted slot - no prog or title assigned to slot-->
<SlotId>675347</SlotId>
<SlotName>Late Movie</SlotName>
</PromotedProgramme>
<PromotedProgramme type ="ExtChannel" seq_number ="5"> <!--promoted prog from ext.channel-->
<PromotedBroadcastRef>675347</PromotedBroadcastRef >
</PromotedProgramme>
</Interstitial>
<Media>
<Source>playout</Source>
<Identifier>NJ01858S </Identifier>
<AspectRatio>16X9 </AspectRatio>
</Media>
</CreatePromo>
</Body>
</PromoMessage>

Source 2 is a purge request and needs to just be parsed through based on the method:

- <PromoMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Header origin="IBMSV6" method="purge" timestamp="2013-08-06T06:34:07Z" /> 
- <Body>
- <PurgePromo providerId="NJR" promoID="245685_NJ01857S">
- <Distribution>
- <Destinations>
  <Destination platform="WCMS" site="EPG" /> 
  </Destinations>
  </Distribution>
  </PurgePromo>
  </Body>
  </PromoMessage>

Here is my current XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="/">   
<!--xsl:template match="PromoMessage/Header[@method]='purge'"/-->                   
<PromoMessage>

<!--xsl:variable name="origin"/>
<xsl:variable name="method"/>
<xsl:variable name="timestamp"/>

<xsl:variable name="providerId"/>
<xsl:variable name="promoID"/>

<xsl:variable name="RWstart"/>
<xsl:variable name="RWend"/>

<xsl:variable name="platform"/>
<xsl:variable name="site"/>
<xsl:variable name="visible"/>

<xsl:variable name="DWstart"/>
<xsl:variable name="DWend"/-->

<Header>    
<xsl:apply-templates select="PromoMessage/Header"/>
<xsl:attribute name="origin">
<xsl:value-of select="PromoMessage/Header[@origin]"/>
</xsl:attribute>

<!--xsl:variable name="method">
<xsl:if test="PromoMessage/Header[@method]='create' or 'update'"></xsl:if>
<xsl:if test="PromoMessage/Header[@method]='purge'"></xsl:if>
</xsl:variable-->       
<xsl:attribute name="method">
<xsl:value-of select="/"/>
</xsl:attribute>    

<xsl:attribute name="timestamp">
<xsl:value-of select="/"/>
</xsl:attribute>

</Header>   

<Body>
<xsl:apply-templates select="PromoMessage/Header/Body"/>

<CreatePromo>

<xsl:attribute name="providerId">
<xsl:value-of select="@providerId"/>
</xsl:attribute>

<xsl:attribute name="promoID">
<xsl:value-of select="@promoID"/>
</xsl:attribute>    




<Distribution>

<RightsWindow>

<xsl:attribute name="start">
<xsl:value-of select="@start"/>
</xsl:attribute>    

<xsl:attribute name="end">
<xsl:value-of select="@end"/>
</xsl:attribute>    

</RightsWindow>


<Destinations>

<Destination>

<xsl:attribute name="platform">
<xsl:value-of select="@platform"/>
</xsl:attribute>    

<xsl:attribute name="site">
<xsl:value-of select="@site"/>
</xsl:attribute>

<xsl:attribute name="visible"   >
<xsl:value-of select="@visible"/>
</xsl:attribute>    

<DisplayWindow>

<xsl:attribute name="start">
<xsl:value-of select="@start"/>
</xsl:attribute>    

<xsl:attribute name="end">
<xsl:value-of select="@end"/>
</xsl:attribute>


</DisplayWindow>    

</Destination>  

</Destinations>

</Distribution>



<EventLink>

<xsl:choose>
<!--Interstitial Category episodic & Promoted Programme Type MovieOrEvent-->
<xsl:when test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme/@type='MovieOrEvent'">

<xsl:attribute name="type">
<xsl:value-of select="@category"/>
</xsl:attribute>


<xsl:attribute name="title">
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/EPGTitleName != '0'">
<xsl:value-of select="EPGTitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/TitleName != '0'">
<xsl:value-of select="TitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/SlotName != '0'">
<xsl:value-of select="SlotName"/>
</xsl:if>
</xsl:attribute>     

<xsl:attribute name="parentalRating">
<xsl:value-of select="@parentalRating"/>
</xsl:attribute>

<Programme>
<ProgrammeId>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/ExternalProgId != '0'">
<xsl:value-of select ="ExternalProgId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/TitleId != '0'">
<xsl:value-of select ="TitleId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/SlotId != '0'">
<xsl:text>S</xsl:text><xsl:value-of select ="SlotId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/PromotedBroadcastRef != '0'">
<xsl:value-of select ="PromotedBroadcastRef"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='MovieOrEvent']/TitleBroadcastRef != '0'">
<xsl:value-of select ="TitleBroadcastRef"/></xsl:if>
</ProgrammeId>
</Programme>


</xsl:when>

<!--Interstitial Category episodic & Promoted Programme Type Series-->
<xsl:when test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme/@type='Series'">
<xsl:attribute name="type">
<xsl:value-of select="@category"/>
</xsl:attribute>

<xsl:attribute name="title">
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/EPGTitleName != '0'">
<xsl:value-of select="EPGTitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/SeasonName != '0'">
<xsl:value-of select="SeasonName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/TitleName != '0'">
<xsl:value-of select="TitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/SlotName != '0'">
<xsl:value-of select="SlotName"/>
</xsl:if>
</xsl:attribute>     


<xsl:attribute name="parentalRating">
<xsl:value-of select="@parentalRating"/>
</xsl:attribute>

<Programme>
<ProgrammeId>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/ExternalProgId != '0'">
<xsl:value-of select ="ExternalProgId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/TitleId != '0'">
<xsl:value-of select ="TitleId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/SlotId != '0'">S
<xsl:value-of select ="SlotId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/PromotedBroadcastRef != '0'">
<xsl:value-of select ="PromotedBroadcastRef"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Series']/TitleBroadcastRef != '0'">
<xsl:value-of select ="TitleBroadcastRef"/></xsl:if>
</ProgrammeId>
</Programme>
</xsl:when>

<!--Interstitial Category episodic & Promoted Programme Type Slot-->
<xsl:when test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme/@type='Slot'">
<xsl:attribute name="type">
<xsl:value-of select="@category"/>
</xsl:attribute>

<xsl:attribute name="title">

<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/EPGTitleName != '0'">
<xsl:value-of select="EPGTitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/SeasonName != '0'">
<xsl:value-of select="SeasonName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/TitleName != '0'">
<xsl:value-of select="TitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/SlotName != '0'">
<xsl:value-of select="SlotName"/>
</xsl:if>
</xsl:attribute>     


<xsl:attribute name="parentalRating">
<xsl:value-of select="@parentalRating"/>
</xsl:attribute>

<Programme>
<ProgrammeId>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/ExternalProgId != '0'">
<xsl:value-of select ="ExternalProgId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/TitleId != '0'">
<xsl:value-of select ="TitleId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/SlotId != '0'">S
<xsl:value-of select ="SlotId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/PromotedBroadcastRef != '0'">
<xsl:value-of select ="PromotedBroadcastRef"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='Slot']/TitleBroadcastRef != '0'">
<xsl:value-of select ="TitleBroadcastRef"/></xsl:if>

</ProgrammeId>
</Programme>

</xsl:when>

<!--Interstitial Category episodic & Promoted Programme Type ExtChannel-->
<xsl:when test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme/@type='ExtChannel'">
<xsl:attribute name="type">
<xsl:value-of select="@category"/>
</xsl:attribute>

<xsl:attribute name="title">

<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/EPGTitleName != '0'">
<xsl:value-of select="EPGTitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/SeasonName != '0'">
<xsl:value-of select="SeasonName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/TitleName != '0'">
 <xsl:value-of select="TitleName"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/SlotName != '0'">
<xsl:value-of select="SlotName"/>
</xsl:if>
</xsl:attribute>     


<xsl:attribute name="parentalRating">
<xsl:value-of select="@parentalRating"/>
</xsl:attribute>

<Programme>
<ProgrammeId>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/ExternalProgId != '0'">
<xsl:value-of select ="ExternalProgId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/TitleId != '0'">
<xsl:value-of select ="TitleId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/SlotId != '0'">S
<xsl:value-of select ="SlotId"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/PromotedBroadcastRef != '0'">
<xsl:value-of select ="PromotedBroadcastRef"/></xsl:if>
<xsl:if test="PromoMessage/Body/CreatePromo/Interstitial[@category='episodic']/PromotedProgramme[@type='ExtChannel']/TitleBroadcastRef != '0'">
<xsl:value-of select ="TitleBroadcastRef"/></xsl:if>
</ProgrammeId>
 </Programme>

</xsl:when>                         
</xsl:choose>        


<Media>
<Source>
<Videos>
<Video>
<xsl:attribute name="source">
<xsl:value-of select="@Source"/>
</xsl:attribute>
<xsl:attribute name="Identifier">
<xsl:value-of select="@Identifier"/>                                                                        
</xsl:attribute>
<xsl:attribute name="aspect_ratio">
<xsl:value-of select="@AspectRatio"/>                                                                       
</xsl:attribute>
</Video>
</Videos>
</Source>
</Media>

 </CreatePromo>

</Body>
</PromoMessage> 
</xsl:template> 
</xsl:stylesheet>

Here is the output of the XSLT using Source 1 XML:

<?xml version="1.0" encoding="UTF-8"?>
<PromoMessage xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <Header origin="" method=" //promoted prog non-series&#xA;        000000218564785432078654Men In Black675342Late Movie000000218576785632078123Die HardDie Hard000000218321785122075432 Friends S5 Ep51010645346Friends5510987532FriendsFriends675347Late Movie675347playoutNJ01858S 16X9 " timestamp=" //promoted prog non-series&#xA;        000000218564785432078654Men In Black675342Late Movie000000218576785632078123Die HardDie Hard000000218321785122075432 Friends S5 Ep51010645346Friends5510987532FriendsFriends675347Late Movie675347playoutNJ01858S 16X9 "/>
<Body>
<CreatePromo providerId="" promoID="">
<Distribution>
<RightsWindow start="" end=""/>
<Destinations>
    <Destination platform="" site="" visible="">
    <DisplayWindow start="" end=""/>
    </Destination>
    </Destinations>
    </Distribution>
    <EventLink type="" title="" parentalRating="">
    <Programme>
    <ProgrammeId></ProgrammeId>
    </Programme>
    </EventLink>
    <Media>
    <Source>
    <Videos>
    <Video source="" Identifier="" aspect_ratio=""/>
    </Videos>
    </Source>
    </Media>
    </CreatePromo>
    </Body>
    </PromoMessage>

And here is how the output should look:

<?xml version="1.0" ?> 
    - <PromoMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Header origin="IBMSV6" method="create" timestamp="2013-08-06T06:32:17Z" /> 
    <Body>
    <CreatePromo providerId="NJR" promoID="245686_NJ01858S">
    <Distribution>
    <RightsWindow start="2013-07-31T14:00:00Z" end="2013-08-31T13:59:59Z" /> 
    <Destinations>
    <Destination platform="WCMS" site="EPG" visible="true">
    <DisplayWindow start="2013-07-31T14:00:00Z" end="2013-08-31T13:59:59Z" /> 
    </Destination>
    </Destinations>
    </Distribution>
    <EventLink type="episodic" title="Men In Black" parentalRating="PG">
    <!-- title from Listing Text Set Title
    --> 
    <Programme>
    <ProgrammeId>785432</ProgrammeId> 
    <!-- From ExtProgId (if it exists) or title ID 
    --> 
    </Programme>
    </EventLink>
    <Media>
    <Source>
    <Videos>
    <Video source="playout" Identifier="NJ01858S" aspect_ratio="16X9" /> 
    </Videos>
    </Source>
    </Media>
    </CreatePromo>
    </Body>
    </PromoMessage>

I certainly would appreciate if someone could point me in the right direction, Thanks.

Upvotes: 0

Views: 175

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

I can answer your question regarding the first step, because that's the only part of it that I think(?) I understood:

Create and Update require some logic to transform the XML whereas Purge method will parse the XML through.

Your XSLT needs to have this basic structure:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/PromoMessage[Header/@method='create' or Header/@method='update']">
    <!-- logic to process "create/update" message types  -->
</xsl:template>

<xsl:template match="/PromoMessage[Header/@method='purge']">  
    <!-- logic to process "purge" message type  -->
</xsl:template>

</xsl:stylesheet>

I am not quite sure what you mean by:

Purge method will parse the XML through.

If by "parse-through" you mean "pass-through" - i.e.copy as is, then the logic to process "purge" message type can be very simple:

<xsl:template match="/PromoMessage[Header/@method='purge']">  
    <xsl:copy-of select="."/>
</xsl:template>

I suggest you post separate question/s regarding any specific issue/s with processing of "create/update" message types.

Upvotes: 0

Related Questions