XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Jeff DuffSubject: I've combed the HELP sites, but still stuck...
Author: Jeff Duff
Date: 14 Jun 2007 07:01 PM
Originally Posted: 14 Jun 2007 06:58 PM
I'm a newbie to xsl, but I seem to be stuck on something that I think should be relatively easy.

This is an xml news feed that can have up to 20 different news items that are contained within the <ContentItem> tags.

What I am trying to do is to represent each individual news item with a different background format. Basically I would like to have Item 1 with a green background, item 2 with blue, item 3 with purple and so on. Until I figure this out, the code is only formating the items with a green background (see xsl below xml).

The conditional formatting should happen in the "CIndexStoryHead" template.

Any help would be greatly appreciated!


The xml looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE NewsML PUBLIC "urn:newsml:iptc.org:20001006:NewsMLv1.0:1" "http://www.test.org/NewsML/DTD/NewsMLv1.0.dtd" [ <!ENTITY % cpnewsmldtd SYSTEM "CNewsML.dtd"> %cnewsmldtd;]>
<?xml-stylesheet type="text/xsl" href="CNewsML.xsl"?>
<NewsML>
<Catalog Href="http://www.iptc.org/NewsML/catalog/catalog.IptcMasterCatalog.xml"/>
<NewsEnvelope>
<DateAndTime>20070614</DateAndTime>
</NewsEnvelope>
<NewsItem>
<Identification>
<NewsIdentifier>
<ProviderId>cNewstest.org</ProviderId>
<DateId>20070614</DateId>
<NewsItemId>46076027</NewsItemId>
<RevisionId PreviousRevision="0" Update="N">1</RevisionId>
<PublicIdentifier>urn:newsml:cNewstest.org:20070614:46076027:1</PublicIdentifier>
</NewsIdentifier>
</Identification>
<NewsManagement>
<NewsItemType FormalName="News" Scheme="NewsItemType"/>
<FirstCreated>20070614</FirstCreated>
<ThisRevisionCreated>20070614</ThisRevisionCreated>
<Status FormalName="Usable" Scheme="IptcStatus"/>
<Urgency FormalName="8" Scheme="IptcUrgency"/>
</NewsManagement>
<NewsComponent>
<NewsLines>
<HeadLine>News</HeadLine>
<DateLine>at 16:04 on June 14, 2007, EDT.</DateLine>
<CopyrightLine>&EngCNews;, 2007</CopyrightLine>
<SlugLine>MUSIC</SlugLine>
<KeywordLine>News</KeywordLine>
</NewsLines>
<AdministrativeMetadata>
<Source>
<Party FormalName="CNews" Scheme="Provider"/>
</Source>
</AdministrativeMetadata>
<DescriptiveMetadata>
<Language FormalName="en" Scheme="Language"/>
<SubjectCode>
<SubjectMatter FormalName="entertainment"/>
</SubjectCode>
</DescriptiveMetadata>

<ContentItem>
<DataContent>
<COnlineFile Type="IndexStoryItem">
<JavaScript ScriptLanguage="&JavaScriptLanguage;">&CJavaScriptOpenWindow;</JavaScript>
<CIndexStoryHead>Headline TEXT</CIndexStoryHead>
<CStory>
<CParag Number="1" ParaSpace="FALSE">
Text Content Goes Here

</CParag>
<CParag Number="2" ParaSpace="FALSE">
Text Content Goes Here

</CParag>
</CStory>
<CLink Type="StoryFile" Number="1" SourceFilePath="./efile1.xml"/>

</COnlineFile>
</DataContent>
</ContentItem>

<ContentItem>
<DataContent>
<COnlineFile Type="IndexStoryItem">
<JavaScript ScriptLanguage="&JavaScriptLanguage;">&CJavaScriptOpenWindow;</JavaScript>
<CIndexStoryHead>Headline TEXT</CIndexStoryHead>
<CStory>
<CParag Number="1" ParaSpace="FALSE">
Text Content Goes Here

</CParag>
<CParag Number="2" ParaSpace="FALSE">
Text Content Goes Here

</CParag>
</CStory>
<CLink Type="StoryFile" Number="1" SourceFilePath="./efile1.xml"/>

</COnlineFile>
</DataContent>
</ContentItem>
</NewsComponent>
</NewsItem>
</NewsML>

==============Here's the XSL====================
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="http://www.w3.org/TR/REC-html40">


<!-- Our output is ScalaScript, which is not XML -->
<xsl:output method="text" omit-xml-declaration="yes"/>

<!-- ============================================================ -->

<!-- ~~~~~~~~~~~~~ template: NewsML ~~~~~~~~~~~~~ -->

<xsl:template match="NewsML">

<!-- Emit boilerplate for top of script -->
<xsl:text disable-output-escaping="yes">!ScalaScript500
:"&lt;untitled&gt;"
{
Group:
BackgroundSettings(Size(1930, 1260));
Config.RecentPublishLocations(PublishLocation("testserver.com CM33", "News"));
Config.SaveOpts(SaveEditable(On));
Config.SaveEncoding("Auto");
Sequence:
</xsl:text>

<xsl:apply-templates select="NewsItem/NewsComponent/ContentItem/DataContent"/>

<!-- Emit boilerplate for bottom of script -->
<xsl:text disable-output-escaping="yes">
}
</xsl:text>
</xsl:template>

<!-- ============================================================ -->
<!-- ~~~~~~~~~~~~~ template: NewsEnvelope ~~~~~~~~~~~~~ -->
<xsl:template match="NewsEnvelope"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: NewsItem ~~~~~~~~~~~~~-->
<xsl:template match="NewsItem"></xsl:template>
<!-- ============================= NewsIdentifier ============================= -->
<!-- ~~~~~~~~~~~~~ template: NewsIdentifier ~~~~~~~~~~~~~ -->
<xsl:template match="NewsIdentifier"></xsl:template>
<!-- ============================= NewsManagement ============================= -->
<!-- ~~~~~~~~~~~~~ template: NewsManagement ~~~~~~~~~~~~~ -->
<xsl:template match="NewsManagement"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: NewsItemType ~~~~~~~~~~~~~ -->
<xsl:template match="NewsItemType"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: FirstCreated ~~~~~~~~~~~~~ -->
<xsl:template match="FirstCreated"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: ThisRevisionCreated ~~~~~~~~~~~~~ -->
<xsl:template match="ThisRevisionCreated"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: Status ~~~~~~~~~~~~~-->
<xsl:template match="Status"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: NewsComponent ~~~~~~~~~~~~~ -->
<xsl:template match="NewsComponent"></xsl:template>
<!-- ============================= NewsLines ============================= -->
<!-- ~~~~~~~~~~~~~ template: NewsLines ~~~~~~~~~~~~~ -->
<xsl:template match="NewsLines"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: HeadLine ~~~~~~~~~~~~~ -->
<xsl:template match="HeadLine"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: ByLine ~~~~~~~~~~~~~ -->
<xsl:template match="ByLine"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: DateLine ~~~~~~~~~~~~~ -->
<xsl:template match="DateLine"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: CopyrightLine ~~~~~~~~~~~~~ -->
<xsl:template match="CopyrightLine"></xsl:template>
<!-- ~~~~~~~~~~~~~ template: KeywordLine ~~~~~~~~~~~~~ -->
<xsl:template match="KeywordLine"></xsl:template>

<!-- ============================= DataContent ============================= -->
<!-- ~~~~~~~~~~~~~ template: DataContent ~~~~~~~~~~~~~ -->

<xsl:template match="DataContent">
<xsl:text disable-output-escaping="yes">
{
Group:
Display(Wipe("Dissolve", Duration(1000), Direction(90)), Backdrop(Pen(16)), Margin(10, 10, 0, 0), Tabs(Explicit(80, 160, 240, 320, 400, 480, 560)), UserPalette(RGBPen(1, $0, $ffffff)));</xsl:text>

<xsl:apply-templates/>
</xsl:template>


<!-- ~~~~~~~~~~~~~ template: JavaScript ~~~~~~~~~~~~~ -->
<xsl:template match="JavaScript"></xsl:template>

<!-- ~~~~~~~~~~~~~ template: CIndexStoryHead ~~~~~~~~~~~~~ -->

<xsl:template match="CIndexStoryHead">
<xsl:text disable-output-escaping="yes">
Clip(0, -2, "Green.png", Transparent(Off), Operation(On));
Text(62, 159, "News", Face(On, Pen(20))));
Text(63, 206,
</xsl:text>"
<xsl:value-of select="(.)"/>", <xsl:text disable-output-escaping="yes">Face(On, Pen(10)));
</xsl:text>
</xsl:template>

<xsl:template match="*|@*|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="CParag"><xsl:choose>
<xsl:when test="@Number='1'">
<xsl:text>
Text(65, 453, "
</xsl:text>
<xsl:value-of select="translate(normalize-space(.), '&#x22;', '«')"/>
</xsl:when>

<xsl:when test="@Number='2'">
<xsl:value-of select="translate(normalize-space(.), '&#x22;', '«')"/>
<xsl:text disable-output-escaping="yes">",
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>


<!-- ~~~~~~~~~~~~~ template: CLink ~~~~~~~~~~~~~ -->
<xsl:template match="CLink">
<xsl:choose>

<!-- ~~~~ Link: Story Photo1 ~~~~ -->
<xsl:when test="@Type='StoryPhoto'"></xsl:when>

<!-- ~~~~ Link: Story File ~~~~ -->
<xsl:when test="@Type='StoryFile'">
<xsl:text disable-output-escaping="yes">Face(On, Pen(2));
HardDuration(1000);
}
</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>

Postnext
(Deleted User) Subject: I've combed the HELP sites, but still stuck...
Author: (Deleted User)
Date: 15 Jun 2007 11:48 AM
Hi Jeff,
you can count the number of items by using count(preceding-sibling::*), and then using a xsl:when to pick the proper color given the position.

Hope this helps,
Alberto

Postnext
Jeff DuffSubject: I've combed the HELP sites, but still stuck...
Author: Jeff Duff
Date: 15 Jun 2007 03:10 PM
Originally Posted: 15 Jun 2007 02:17 PM
Thanks for the reply Alberto.

I've tried the following syntax to test the position, but now I get no output at all... What am I doing wrong??

<xsl:template match="CIndexStoryHead">
<xsl:choose><xsl:when test="count(preceding-sibling::*) = 1">
<xsl:text disable-output-escaping="yes">
Clip(0, -2, "Green.png", Transparent(Off), Operation(On));
Text(62, 159, "Entertainment News", Face(On, Pen(20)));
Text(63, 206,
</xsl:text>"
<xsl:value-of select="(.)"/>",
<xsl:text disable-output-escaping="yes">Face(On, Pen(10)));
</xsl:text>
</xsl:when>

Posttop
(Deleted User) Subject: I've combed the HELP sites, but still stuck...
Author: (Deleted User)
Date: 18 Jun 2007 08:01 AM
Hi Jeff,
if you want to display the first item in green you should test for a count == 0 (i.e. the first one has no preceding items)

Alberto

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.