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

Scrapy: Issues in dealing with Abbr tag in Xpath
XPath for ImportXML in Gsheet
XMLStarlet: selecting nodes using less than / greater than
Date range comparison in CQ using XPATH
I don't understand why this XPath expression is not working as a Scrapy selector
Selenium IDE, check (assert) if a dynamic element contains a specific text
xpath help to get buttons under a class where a link contains some href value
How to write xpath for below code displayed on Image
Fetch element child elements in XQuery
Quickly extract value using xpath
WSO2 ESB- Error Handling - On Error Sequence
Multiple xpath expressions
great ancestor & great great ancestor
How do I convert a string to an int variable in BizTalk XLANG?
extract table rows with XPATH
find the xpath of specific label

Categories

HOME
office365
webview
events
ontouchlistener
openxml
angular-ui-grid
workflow
magento-2.0
haxe
u-boot
polymer-2.x
yahoo
spring-security-oauth2
dhcp
trace32
bnf
pywinauto
rpmbuild
spam
spring-shell
bluemix-mobile-services
angular2-highcharts
bsd
perfino
dragula
basex
miniprofiler
mdns
location-services
series
restful-architecture
ftp-server
spinner
djcelery
twilio-php
cloudera-quickstart-vm
plane
resampling
klee
cloudera-sentry
spongycastle
qvtkwidget
deployd
scala-breeze
android-webservice
line-intersection
asp.net-web-api-routing
rcharts
dynamics-crm-4
ltrace
google-shopping-api
cocoa-bindings
okio
ndk-build
p6spy
django-testing
system.net.mail
dpkg
eclemma
fiber
exists
jemdoc
android-expansion-files
enunciate
dsa
360-degrees
flow-js
scala-swing
innerhtml
debug-symbols
baseadapter
utf8-decode
window-resize
qt5.4
multivariate-testing
xcode6.3.1
lumia-imaging-sdk
stripe.net
incognito-mode
mousemotionlistener
blobstorage
telerik-ajax
autosize
litespeed
asio
datacontracts
kext
downgrade
aptitude
android-input-method
ppc
iphone-sdk-3.1.3
jboss-mdb

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