xsl-list
[Top] [All Lists]

RE: Getting unique nodes filtering by several attributes and mixing files

2003-11-25 09:34:41
Hi,

Thanks for the answer, I see I should read even more on XSLT :)
onAir blocks will come ordered by date and time, so I don't 
realy care ordering them with the XSLT.

Your XSLT does the work perfectly, but I have another question:
I would like to change the attribute "adsAt" on my XML result to
elements, changing your:

<xsl:attribute name="adsAt">
  <xsl:for-each select="$air/@adsAt">
    <xsl:if test="not(position() = 1)">,</xsl:if>
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:attribute>

to:

<xsl:for-each select="$air/@adsAt">
  <xsl:element name="adsAt"><xsl:value-of select="."/></xsl:element>
</xsl:for-each>

I've tried that but I miss all the attributes on the parent "program"
node, getting the following XML:

<program date='17/11/2003'>
  <adsAt>10:15</adsAt>
  <adsAt>10:30</adsAt>
</program>

That's needed because each "onAir" node has some special attributes
regarding the allowed advertisements among the hour (marketing share,
pricing for 20", special commercials allowed, etc). In the example
I'm attaching below I've added a pricing attribute.

Thanks,

Iván

P.S. XML files attached again:

<!-- timetable.xml -->
<!-- I control this file -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<programs>
  <program progName='Friends' weekDay='Mon' channel='1' start='10:00' 
end='10:45'/>
  <program progName='X-Files' weekDay='Sat' channel='2' start='14:00' 
end='15:30'/>
  <program progName='Friends' weekDay='Sun' channel='1' start='09:00' 
end='09:45'/>
</programs>

<!-- week_schedule.xml -->
<!-- This file comes from an external database I don't control -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<weekSchedule>
  <onAir progName='Friends' channel='1' weekDay='Mon' date='17/11/2003' 
adsAt='10:15' pricing='450.0'/>
  <onAir progName='Friends' channel='1' weekDay='Mon' date='17/11/2003' 
adsAt='10:30' pricing='620.0'/>
  <onAir progName='X-Files' channel='2' weekDay='Sat' date='22/11/2003' 
adsAt='14:15' pricing='420.0'/>
  <onAir progName='X-Files' channel='2' weekDay='Sat' date='22/11/2003' 
adsAt='14:30' pricing='450.0'/>
  <onAir progName='X-Files' channel='2' weekDay='Sat' date='22/11/2003' 
adsAt='14:45' pricing='500.0'/>
  <onAir progName='X-Files' channel='2' weekDay='Sat' date='22/11/2003' 
adsAt='15:25' pricing='610.0'/>
  <onAir progName='Friends' channel='1' weekDay='Sun' date='23/11/2003' 
adsAt='09:15' pricing='410.0'/>
  <onAir progName='Friends' channel='1' weekDay='Sun' date='23/11/2003' 
adsAt='09:30' pricing='480.0'/>
</weekSchedule>

<!-- Jarno's XSLT -->
<xsl:variable name="sep" select="'&#xE000;'"/>
<xsl:variable name="schedule" select="document('week_schedule.xml', /)"/>
<xsl:key name="lookup" match="onAir" use="concat(@progName, $sep, @weekDay, 
$sep, @channel)"/>
<xsl:template match="programs">
  <tv>
    <xsl:for-each select="program">
      <xsl:copy>
        <xsl:variable name="key" select="concat(@progName, $sep, @weekDay, 
$sep, @channel)"/>
        <xsl:for-each select="$schedule">
          <xsl:variable name="air" select="key('lookup', $key)"/>
          <xsl:copy-of select="$air/@date"/>
          <xsl:attribute name="adsAt">
            <xsl:for-each select="$air/@adsAt">
              <xsl:if test="not(position() = 1)">,</xsl:if>
              <xsl:value-of select="."/>
            </xsl:for-each>
          </xsl:attribute>
        </xsl:for-each>
        <xsl:copy-of select="@*"/>
      </xsl:copy>
    </xsl:for-each>
  </tv>
</xsl:template>

<!-- my_new_result.xml -->
<tv>
  <program progName='Friends' date='17/11/2003' channel='1' weekDay='Mon' 
start='10:00' end='10:45'>
    <commercials hour='10:15' pricing='450.0'/>
    ...
  </program>
  <program progName='Friends' date='23/11/2003' channel='1' weekDay='Sun' 
start='09:00' end='09:45'>
    <commercials hour='14:15' pricing='620.0'/>
    ...
  </program>  
  <program progName='X-Files' date='22/11/2003' channel='2' weekDay='Sat' 
start='14:00' end='15:30'>
    <commercials hour='09:15' pricing='410.0'/>
    ...
  </program>
</tv>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>