Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string in xsl for content with /

Tags:

string

xml

xslt

I have some content being pulled in from an external xml with xsl. In the xml the title is merged with the author with a backslash separating them.

How do I separate the title and author in xsl so that I can have them with different tags

<product>
  <title>The Maze / Jane Evans</title> 
</product>

to be

<h2>The Maze</h2>
<p>Jane Evans</p>
like image 414
kristina Avatar asked Aug 19 '26 08:08

kristina


1 Answers

Hope this helps! Let me know if I misinterpreted the question!

<xsl:variable name="title">
    <xsl:value-of select="/product/title"/>
</xsl:variable>

<xsl:template match="/">
    <xsl:choose>
        <!--create new elements from existing text-->
        <xsl:when test="contains($title, '/')">
            <xsl:element name="h2">
                <xsl:value-of select="substring-before($title, '/')"/>
            </xsl:element>
            <xsl:element name="p">
                <xsl:value-of select="substring-after($title, '/')"/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <!--no '/' deliminator exists-->
            <xsl:value-of select="$title"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
like image 54
developer Avatar answered Aug 21 '26 13:08

developer