user2423959
user2423959

Reputation: 834

confusing statement while version change

I've the below XSLT statement.

 <xsl:value-of select="substring-after(
    ./title/content-style/text(),' ')" />

when i use the style sheet version 1 as

    <xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ntw="Number2Word.uri" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="ntw">

But when i change it to version 2.0 as below.

<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:ntw="Number2Word.uri" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="ntw">

it is throwing the below error

XSLT 2.0 Debugging Error: Error: 
file:///C:/Users/u0138039/Desktop/Proview/HK/HKWB2014/XSLT%20and%20CSS/new_bull.xsl:202: 
Wrong occurrence to match required sequence type -   
Details: -     XPTY0004: The supplied sequence ('2' item(s)) has 
the wrong occurrence to match the sequence type xs:string ('zero or 
one')

please let me know what is going right in XSL1.0 is going wrong in XSL 2.0.

Thanks

Upvotes: 1

Views: 348

Answers (2)

topherg
topherg

Reputation: 4303

I had the same issue, but for me, it turned out that while my schema, for the element in question, had the multiplicity optional setting, it did not have the unbounded setting, and the input data I was using had 2 elements where it was expecting 0...1

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122394

In XSLT 1.0, when you pass a node set of more than one node to a function that expects a single string, it simply uses the string value of the first node in the set and ignores the rest of them. In XSLT 2.0 the substring-after function expects its arguments to be xs:string?, i.e. zero or one string, so if you pass it a sequence with more than one item it will cause a type mismatch error.

I presume that ./title/content-style/text() selects more than one node, i.e. there's more than one title, more than one content-style and/or more than one text node within the content-style element.

Consider whether you actually need to use text() here at all - do you really need to process each text node child of content-style individually, or do you just want the string value of the content-style element as a whole? If the former, you need something like title/content-style/text()/substring-before(., ' ') (which only works in 2.0), if the latter, try just saying substring-after(title/content-style, ' ').

Upvotes: 3

Related Questions