MaestroAshmin
MaestroAshmin

Reputation: 17

How to replace text after comma in an XML node when transforming with XSLT

I cannot figure out how to replace the text after the comma (including the comma) from the authors node when transforming using XSLT. When there is more than one author like Grant, Colman, and Liu, the output should be Grant et al. Right now I am using translate to remove comma (,) but it will only replace a single text. Is there a workaround using xslt 1.0?

My XML File is:

<?xml version="1.0" encoding="iso-8859-1"?> 
<books>
 <book>
    <title type="fiction">Book 1</title>
    <authors>Louis, Van and Gaal</authors>
    <publisher>Publisher 1</publisher>
    <year>2022</year>
    <price>200</price>
</book>
<book>
    <title type="non-fiction">Book 2</title>
    <authors>Ralls, Kim</authors>
    <publisher>Publisher 2</publisher>
    <year>2000</year>
    <price>100</price>
</book>
<book>
    <title type="fiction">Book 3</title>
    <authors>Corets</authors>
    <publisher>Publisher 3</publisher>
    <year>1996</year>
    <price>150</price>
</book>
<book>
    <title type="non-fiction">Book 4</title>
    <authors>Pep</authors>
    <publisher>Publisher 4</publisher>
    <year>2001</year>
    <price>170</price>
</book>
<book>
    <title type="fiction">Book 5</title>
    <authors>Conte</authors>
    <publisher>Publisher 5</publisher>
    <year>2002</year>
    <price>10</price>
</book>
<book>
    <title type="fiction">Book 6</title>
    <authors>Anton, Louis</authors>
    <publisher>Publisher 6</publisher>
    <year>2010</year>
    <price>25</price>
</book>
<book>
    <title type="non-fiction">Book 7</title>
    <authors>Thomax, Tuchel</authors>
    <publisher>Publisher 7</publisher>
    <year>2011</year>
    <price>15</price>
</book>

MY XSLT file is:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/> 
 <xsl:template match="/">
<xsl:variable name="book-group" select="books/book[(price &lt; 30) and (title/@type='fiction')]"/>
<html>
<body>
<h2>Books</h2>
  <xsl:for-each select="books/book"> 
    <xsl:if test="price &lt; 30">
        <xsl:if test="title/@type='fiction'">
                <span id="title"><xsl:value-of select="title"/></span><br />
            <xsl:variable name="author" select="authors"/>
            <span id="authors">
                <xsl:value-of select='translate($author, ",", "et al")'/>
            </span>
            <br />
            <span><xsl:value-of select="price"/></span><br />
            <span><xsl:value-of select="title/@type"/></span><br />
        </xsl:if>
    </xsl:if>
   </xsl:for-each>
   Total Cost : <xsl:value-of select="sum($book-group/price)"/>
   </body>
   </html>
  </xsl:template>
  </xsl:stylesheet>

Upvotes: 0

Views: 36

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117102

Try something like:

XSLT 1.0

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

<xsl:template match="/books">
    <xsl:variable name="book-group" select="book[price &lt; 30 and title/@type='fiction']"/>
    <html>
        <body>
            <h2>Books</h2>
            <xsl:for-each select="$book-group"> 
                <span id="title">
                    <xsl:value-of select="title"/>
                </span>
                <br/>
                <span id="authors">
                    <xsl:choose>
                        <xsl:when test="contains(authors, ',')">
                            <xsl:value-of select="substring-before(authors, ',')"/>
                            <xsl:text> et al.</xsl:text>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="authors"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </span>
                <br/>
                <span>
                    <xsl:value-of select="price"/>
                </span>
                <br/>
                <span>
                    <xsl:value-of select="title/@type"/>
                </span>
                <br/>
            </xsl:for-each>
            <xsl:text>Total Cost : </xsl:text>
            <xsl:value-of select="sum($book-group/price)"/>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions