In this step you will add the instructions that assign the value of the title in the original metadata, to the title GN4 attribute.
First, you need to locate the title in the original metadata. That's not always easy (if you do not have the wire story provider's Codebook). In this example, it's identified as the value of the HeadLine.
<NewsComponent>
<NewsLines>
<DateLine xml:lang="fr">WASHINGTON, 22 déc 2010 (AFP) -</DateLine>
<HeadLine xml:lang="fr">Un photographe de l'AFP récompensé par le magazine Time</HeadLine>
<NewsLine>
....
Now, the complication is that Headline is the child of the NewsLines node, that in turn is the child of the NewsComponent node, that in turn (not displayed above) is the child of the NewsItem node.
Here you have two choices:
1.Specify the entire path always: that can be useful if you need to extract only one value and only once.
The first block of code creates the XSL variable title (<xsl:variable name="title">), and then assigns to it the value of the node NewsItem/NewsComponent/NewsLines/HeadLine. The second block of code assigns to the title GN4 attribute, the value of the $title XSL variable.
<xsl:variable name="title">
<xsl:value-of select="NewsItem/NewsComponent/NewsLines/HeadLine"/>
</xsl:variable>
<title>
<xsl:value-of select="$title"/>
</title>
Now, the above solution is a bit weak, as it does not test if the value passed to the title attribute is suitable, i.e. that it's a string.
It is easy to add such test:
<xsl:variable name="title">
<xsl:when test="string(NewsItem/NewsComponent/NewsLines/HeadLine)">
<xsl:value-of select="NewsItem/NewsComponent/NewsLines/HeadLine"/>
</xsl:when>
</xsl:variable>
<title>
<xsl:value-of select="$title"/>
</title>
The above solution solves the testing problem, but introduces another one: the duplicated parts of the long path. To solve it, it is recommended to use the approach #2 below.
2.Put the common part of the path in a variable, and then use the variable: that is useful if you need to extract many values on the same subpath and more than once - it will make the code shorter.
<xsl:variable name="NewsLines" select="NewsItem/NewsComponent/NewsLines"/>
....
<xsl:variable name="title">
<xsl:when test="string($NewsLines/HeadLine)">
<xsl:value-of select="$NewsLines/HeadLine"/>
</xsl:when>
</xsl:variable>
<title>
<xsl:value-of select="$title"/>
</title>
Again, there's a possible problem: what if the headline is not a string? Is acceptable that in such case our wires have no title? If the answer is "no", you need to find the alternative. The alternative is to test if the headline is not a string, and if so, take the first line of the wire story itself.
In order for the below code to work, we need to locate under which node is the content of the original wire story. We found it under NewsItem/NewsComponent/ContentItem, and to shorten the path, we defined variable ContentItem:
<xsl:variable name="ContentItem" select="NewsItem/NewsComponent/ContentItem"/>
Therefore the complete code becomes:
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string($NewsLines/HeadLine)">
<xsl:value-of select="$NewsLines/HeadLine"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$ContentItem/DataContent//p[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<title>
<xsl:value-of select="$title"/>
</title>
Entire code of this step
<?xml version="1.0" encoding="utf-8"?>
<!--
Simplified XSLT used to import AFP NewsML wires into the system
-->
<story name="{fn:createGuid()}" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="folderPath" select="$pars/*/add[@key='folderPath']/@value" />
<xsl:variable name="sourceName" select="$pars/*/add[@key='sourceName']/@value" />
<xsl:variable name="copyrightName" select="$pars/*/add[@key='copyrightName']/@value" />
<xsl:variable name="srcPath" select="$pars/*/add[@key='srcPath']/@value" />
<xsl:variable name="NewsLines" select="NewsItem/NewsComponent/NewsLines"/>
<xsl:variable name="ContentItem" select="NewsItem/NewsComponent/ContentItem"/>
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string($NewsLines/HeadLine)">
<xsl:value-of select="$NewsLines/HeadLine"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$ContentItem/DataContent//p[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<title>
<xsl:value-of select="$title"/>
</title>
<folderRef>
<keyVal>
<xsl:value-of select="$folderPath"/>
</keyVal>
</folderRef>
<xsl:if test="string($sourceName)">
<sourceRef>
<keyVal>
<xsl:value-of select="$sourceName"/>
</keyVal>
</sourceRef>
</xsl:if>
<xsl:if test="string($copyrightName)">
<rights>
<ref>
<keyVal>
<xsl:value-of select="$copyrightName"/>
</keyVal>
</ref>
</rights>
</xsl:if>
<sourceFileName>
<xsl:value-of select="fn:pathFileName($srcPath)"/>
</sourceFileName>
</story>
Note
At this stage, the code is still incomplete: it will create a story with an automatic name, title and some other attributes, but with no body text.