starts-with Function

Returns true if the first argument string starts with the second argument string; otherwise returns false.

boolean starts-with(string, string)

Remarks

If an argument is not of type string, it is first converted to a string using the string() function and then the result of that conversion is evaluated.

Caution noteCaution

String conversions for node sets that are passed as arguments to this function may yield unexpected results. For more information, see string Function.

This function is case-sensitive.

Example

The following example illustrates how to use the starts-with() function to query a collection of books whose title begins with a capital letter "W".

XML FILE (starts-with.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" 

href="contains.xsl"?>      
<bookstore>
  <book>
     <title>The Weather Pattern</title>
     <author>Weather Man</author>
     <price>100.00</price>
  </book>
  <book>
     <title>Weaving Patterns</title>
     <author>Weaver</author>
     <price>150.00</price>
  </book>
  <book>
     <title>Speech Pattern</title>
     <author>Speaker</author>
     <price>15.00</price>
  </book>
  <book>
     <title>Writing Style</title>
     <author>Writer</author>
     <price>1500.00</price>
  </book>
</bookstore>

XSLT FILE (starts-with.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"           

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"   
     omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <html>
       <head><title>example</title></head>
    <body>
       <xsl:apply-templates select="//book"/>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="book">
     <xsl:if test="starts-with(title, 'W')">
       <DIV>
         <B><xsl:value-of select="title"/></B> by 
         <I><xsl:value-of select="author"/></I> costs
         <xsl:value-of select="price"/>.
       </DIV>
     </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Output

This XSLT style sheet, when applied to the XML file (starts-with.xml) produces the following output:

Weaving Patterns by Weaver costs 150.00.

Writing Style by Writer costs 1500.00.

See Also

Reference

XML Data Types Reference