Skip to content Skip to sidebar Skip to footer

New To Xslt - How To Return Text Of Span Class In Xml?

I have the following XML: Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiv

Solution 1:

Assuming this xml (the one you posted is not well formed):

<divxmlns:core="http://www.w3.org/core"><core:renderedItem><spanclass="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
        '
        <span><em>Journal of Human Rights and the Environment</em></span>
        .
    </core:renderedItem></div>

You can get the text you want with this xsl code:

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:core="http://www.w3.org/core"exclude-result-prefixes="xsl"><xsl:outputomit-xml-declaration="yes"method="xml"indent="yes" /><xsl:templatematch="/"><xsl:value-ofselect="substring-before(div/core:renderedItem/span[@class = 'harvard_title'], ':')" /></xsl:template></xsl:stylesheet>

Whith this: span[@class = 'harvard_title'] you get the text inside the span, and with substring-before function you get just the text before de colon.

Hope it helps.

Edit: you can use an if to take into account the case where there is not a colon in the text, but you can do it using templates:

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:core="http://www.w3.org/core"exclude-result-prefixes="xsl"><xsl:outputomit-xml-declaration="yes"method="xml"indent="yes" /><xsl:templatematch="/"><xsl:apply-templatesselect="div/core:renderedItem/span[@class = 'harvard_title']" /></xsl:template><!-- generic template, for span elements with class = 'harvard_title' --><xsl:templatematch="span[@class = 'harvard_title']"><xsl:value-ofselect="." /></xsl:template><!-- more specific template, for those ones containing a colon --><xsl:templatematch="span[@class = 'harvard_title'][contains(.,':')]"><xsl:value-ofselect="substring-before(., ':')" /></xsl:template></xsl:stylesheet>

Post a Comment for "New To Xslt - How To Return Text Of Span Class In Xml?"