xpath


XSLT Function Return Type


Originally: **How to apply XPath query to a XML variable typed as element()* **
I wish to apply XPath queries to a variable passed to a function in XSLT 2.0.
Saxon returns this error:
Type error at char 6 in xsl:value-of/#select on line 13 column 50 of stackoverflow_test.xslt:
XTTE0780: Required item type of result of call to f:test is element(); supplied value has item type text()
This skeleton of a program is simplified but, by the end of its development, it is meant to pass an element tree to multiple XSLT functions. Each function will extract certain statistics and create reports from the tree.
When I say apply XPath queries, I mean I wish to have the query consider the base element in the variable... if you please... as if I could write {count(doc("My XSLT tree/element variable")/a[1])}.
Using Saxon HE 9.7.0.5.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="f:f">
<xsl:template match="/root">
<xsl:variable name="first" as="element()*">
<xsl:copy-of select="(./a[1])" />
</xsl:variable>
<html>
<xsl:copy-of select="f:test($first)" />
</html>
</xsl:template>
<xsl:function name="f:test" as="element()*">
<xsl:param name="frstElem" as="element()*" />
<xsl:value-of select="count($frstElem/a)" />
<!-- or any XPath expression -->
</xsl:function>
</xsl:stylesheet>
Some example data
<root>
<a>
<b>
<c>hi</c>
</b>
</a>
<a>
<b>
<c>hi</c>
</b>
</a>
</root>
Possibly related question: How to apply xpath in xsl:param on xml passed as input to xml
If you want f:test() to return the number of a elements in the sequence that is the value of $frstElem, you can use something like
<xsl:value-of select="count($frstElem/self::a)" />
instead of using the (implicit) child:: axis.
What you are doing is perfectly correct, except that you have passed an a element to the function, and the function is looking for an a child of this element, and with your sample data this will return an empty sequence.

Related Links

Double iteration over a nodeset in XSLT-1.0
What is the correct XPath query for 'select onchange'
XPath 2.0: Retrieving nodes by attribute where value is case Insensitive
XPath - abbreviation of position() function
XPath Query in JMeter
xpath expression to select attribute value
DOMDocument xpath query cut off time
How to parse the following html code get all text before “br” tag
XPath selector by class AND index
How to perform set operations in XPath 1.0
extract data from a div that have no class using xpath
Xpath: Select node but not specific child elements
XQuery - How to use eq (=) on a group of values
better selenium xpath is expecting
How to get only numbers from string with XPath
xpath expression to find url and data

Categories

HOME
swi-prolog
stacktrace.js
nsview
theano
whmcs
tabs
mathematical-optimization
qooxdoo
openflow
ontology
repair
ag-grid
cdi
angular2-template
selection
sharepoint-online
heuristics
angular4
tomcat8
error-logging
sharedpreferences
mime-types
managed-c++
introspection
list-comprehension
cube
quick-nimble
rspec-rails
lxml
cloveretl
apptentive
creation
xerces-c
elastica
ng-repeat
selenide
latexml
sendinput
oracle-bmcs
dredd
viewmodel
rm
xcglogger
jgrapht
extjs4.1
pecl
sidewaffle
cognos-tm1
allegro
ndk-build
visual-studio-monaco
noclassdeffounderror
sharepoint-apps
psd
vim-plugin
variable-length-array
fanotify
gitlab-omnibus
textscan
mod-auth-openidc
nesper
inequality
distributed-r
git-reset
commenting
zend-search-lucene
jack
hotswap
cpu-time
application-blocks
stty
node.js-stream
objective-c-runtime
oscilloscope
jbox2d
msinfo32
microblogging
snackjs
airprint
bll
jdownloader
sscli
zend-test
eventaggregator
rawcontacts
powergui
processors
jquery-effects
index.dat
prism-2
activex-exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App