Динамическое приведение в XSLT 2.0 или 3.0

У меня есть этот код для приведения строки к типу, определенному во время выполнения:

<xsl:variable name="typed-val" select="
    if ($local-name eq 'integer') then $val cast as xs:integer
    else if ($local-name eq 'decimal') then $val cast as xs:decimal
    else if ($local-name eq 'double') then $val cast as xs:double
    else if ($local-name eq 'float') then $val cast as xs:float
    else if ($local-name eq 'date') then $val cast as xs:date
    else if ($local-name eq 'time') then $val cast as xs:time
    else if ($local-name eq 'dateTime') then $val cast as xs:dateTime
    else if ($local-name eq 'duration') then $val cast as xs:duration
    else if ($local-name eq 'string') then $val cast as xs:string
    else if ($local-name eq 'boolean') then $val cast as xs:boolean
    else if ($local-name eq 'anyURI') then $val cast as xs:anyURI
    else if ($local-name eq 'QName') then resolve-QName($val, $context)
    else if ($local-name eq 'gDay') then $val cast as xs:gDay
    else if ($local-name eq 'gMonthDay') then $val cast as xs:gMonthDay
    else if ($local-name eq 'gMonth') then $val cast as xs:gMonth
    else if ($local-name eq 'gYearMonth') then $val cast as xs:gYearMonth
    else if ($local-name eq 'gYear') then $val cast as xs:gYear
    else if ($local-name eq 'yearMonthDuration') then $val cast as xs:yearMonthDuration
    else if ($local-name eq 'dayTimeDuration') then $val cast as xs:dayTimeDuration
    else if ($local-name eq 'base64Binary') then $val cast as xs:base64Binary
    else if ($local-name eq 'hexBinary') then $val cast as xs:hexBinary
    else ()
"/>

Есть ли лучший способ сделать это в XSLT 2.0? как насчет 3.0?


person Max Toro    schedule 21.08.2014    source источник


Ответы (1)


В XPath 3.0 вы можете

let $constructor := function-lookup(
     QName("http://www.w3.org/2001/XMLSchema", $local-name), 1)
return $constructor($val)
person Michael Kay    schedule 21.08.2014