 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 26th, 2008, 01:51 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
changing output for one node in XSLT
Hi,
Forgive my lack of knowledge, I am trying to to add a base URL to the html output of a DOI, the first "when" statement does this by selecting nodes that start with "10" and then concatenating the output with the "http://dx.doi.org/" base URL. However, in the example I have below it is not working because the next "when" statements seem to overvide it. I have tried using "if" and "otherwise" and also putting the first "when" statement in a different "choose" element but this mucks up the "test" elements of the following statements. Below is the stylesheet I am modifying with my added "when" statement first. At the end I have included a sample XML file.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template name="element">
<xsl:param name="indent" select="0"/>
<xsl:text>
</xsl:text>
<xsl:choose>
<xsl:when test='starts-with(./@identifier,"10")'>
<div class="text" style="margin-left:{$indent * 16}px;">
<code>"<xsl:value-of select="concat('http://dx.doi.org/',.)"/></code>
</div>
</xsl:when>
<xsl:when test="self::text()">
<div class="text" style="margin-left:{$indent * 16}px;">
<code><xsl:value-of select="normalize-space(.)"/></code>
</div>
</xsl:when>
<xsl:when test="self::*">
<div class="element" style="margin-left:{$indent * 16}px;">
<code>
<xsl:value-of select="concat('<',name(.))"/>
<xsl:for-each select="./@*"><xsl:text> </xsl:text><xsl:value-of select="name(.)"/>="<xsl:value-of select="."/>"</xsl:for-each>
<xsl:value-of select="concat('','>')"/>
</code>
</div>
<xsl:for-each select="node()">
<xsl:call-template name="element">
<xsl:with-param name="indent">
<xsl:value-of select="$indent + 1"/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
<xsl:text>
</xsl:text>
<div class="element" style="margin-left:{$indent * 16}px;">
<code><xsl:value-of select="concat('</',name(.),'>')"/></code>
</div>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<div id="xml_view">
<xsl:for-each select="node()">
<xsl:call-template name="element">
<xsl:with-param name="indent">
0
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
<oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:title>A new class of designs which protect against quantum jumps</dc:title>
<dc:creator>Alber, G</dc:creator>
<dc:creator>Beth, T</dc:creator>
<dc:creator>Charnes, C</dc:creator>
<dc:creator>Delgado, A</dc:creator>
<dc:creator>Grassl, M</dc:creator>
<dc:creator>Mussinger, M</dc:creator>
<dc:contributor>Macquarie University. Computing</dc:contributor>
<dc:type>journal article</dc:type>
<dc:publisher>USA : Springer Verlag</dc:publisher>
<dc:date>2006</dc:date>
<dc:language>eng</dc:language>
<dc:description>Publisher PDF allowed as per publisher agreement.</dc:description>
<dc:subject></dc:subject>
<dc:relation>Designs Codes and Cryptography, Vol. 1, Issue 7, no. , p. 34-67</dc:relation>
<dc:identifier>http://www.economics.unsw.edu.au/nps/servlet/portalservice?GI_ID=System.LoggedOutInheritableAre a&maxWnd=_Heterodox_FifthConference</dc:identifier>
<dc:identifier>10.1111/j.1469-8137.2006.01809.x</dc:identifier>
<dc:identifier>http://www.mq.edu.au</dc:identifier>
<dc:rights>Publisher PDF allowed as per publisher agreement.</dc:rights>
<dc:description>33</dc:description>
</oai_dc:dc>
Shane C
|
|

February 26th, 2008, 06:19 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
The sample XML does not contain any attributes called 'identifier'. Plus you are not referring to it by its namespace.
The start of your stylesheet should contain the following:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">
Your xsl:when should then read:
<xsl:when test='starts-with(self::dc:identifier,"10")'>
Also, you should be using the xsl:element instruction, rather than trying to construct the element as text:
<xsl:element name="{name(.)}">
<xsl:copy-of select="@*"/>
<xsl:element>
/- Sam Judson : Wrox Technical Editor -/
|
|

February 26th, 2008, 09:19 PM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by shane_carty
Thanks for the reply.
Declaring the namespace and changing the "starts with" match did help, but now I am getting two elements output, one with the base URL added and one with the DOI number only (which I don't want). Is there a way of excluding that XML element from the second choose element so that it doesn't get processed twice? I tried using != in the test parameter but it was not accepted. Below is a section of the result with the two DOI strings and underneath that the stylesheet I am know using.
<div class="text" style="margin-left:16px;"><code>http://dx.doi.org/10.1111/j.1469-8137.2006.01809.x</code></div><div class="element" style="margin-left:16px;"><code><dc:identifier></code></div>
<div class="text" style="margin-left:32px;"><code>10.1111/j.1469-8137.2006.01809.x</code></div>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">
<xsl:strip-space elements="*"/>
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template name="element">
<xsl:param name="indent" select="0"/>
<xsl:text>
</xsl:text>
<xsl:choose>
<xsl:when test='starts-with(self::dc:identifier,"10")'>
<div class="text" style="margin-left:{$indent * 16}px;">
<code><xsl:value-of select="concat('http://dx.doi.org/',.)"/></code>
</div>
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="self::text()">
<div class="text" style="margin-left:{$indent * 16}px;">
<code><xsl:value-of select="normalize-space(.)"/></code>
</div>
</xsl:when>
<xsl:when test="self::*">
<div class="element" style="margin-left:{$indent * 16}px;">
<code>
<xsl:value-of select="concat('<',name(.))"/>
<xsl:for-each select="./@*"><xsl:text> </xsl:text><xsl:value-of select="name(.)"/>="<xsl:value-of select="."/>"</xsl:for-each>
<xsl:value-of select="concat('','>')"/>
</code>
</div>
<xsl:for-each select="node()">
<xsl:call-template name="element">
<xsl:with-param name="indent">
<xsl:value-of select="$indent + 1"/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
<xsl:text>
</xsl:text>
<div class="element" style="margin-left:{$indent * 16}px;">
<code><xsl:value-of select="concat('</',name(.),'>')"/></code>
</div>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<div id="xml_view">
<xsl:for-each select="node()">
<xsl:call-template name="element">
<xsl:with-param name="indent">
0
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
Regards,
Shane
Shane C
|
Shane C
|
|

February 27th, 2008, 05:28 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
You have two <xsl:choose> instructions in there, so you are getting two outputs.
/- Sam Judson : Wrox Technical Editor -/
|
|
 |