AnyURI as an XPath expression?

  • Thread starter honestrosewater
  • Start date
  • Tags
    Expression
In summary: Another alternative is to use a function such as fn:iri-to-uri or fn:resolve-uri to convert the string into a valid URI. Ultimately, the issue here is that URIs are not a recognized type in XPath, so some workaround is necessary to properly declare them as parameters.
  • #1
honestrosewater
Gold Member
2,142
6
This question is extremely annoying but not worth the time to properly investigate, so here is my best attempt to find an answer.

I'm writing something in XSLT and XPath (both 2.0). I want to declare a URI parameter.

<xsl:param name="foo" as="xsd:anyURI" select="http://foo.bar"/>[/indent]

But Saxon complains about that being a parse error (XPST0003: "QName cannot end with a colon {http:}"). So I thought maybe it wants me to enter the URI as a string. But no, that is also bad, a type error, as you would initially expect (XPTE0600). So what is a girl to do?

The value of select must be an XPath expression, but I cannot for the life of me figure out what kind of expression it is. It's not a quantified, conditional, for, comparison, arithmetic, or sequence expression. And of the primary expressions, the only one I can imagine it might be is a string literal, but that doesn't really even make sense. It's not a path expression, is it? URIs are atomic values, surely, right? xsd:anyURI is an atomic type. Bah.

I have solved this for now by just entering the URI as a string and casting it as a URI,

<xsl:param name="foo" as="xsd:anyURI" select="'http://foo.bar' cast as xsd:anyURI"/>​

but that is not satisfying.

How do you enter a URI in XSLT as an XPath expression?​
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Unfortunately, there is no straightforward answer to this question. The best solution is likely to use the cast as xsd:anyURI expression as mentioned above, or if possible, to avoid declaring the URI as a parameter and instead to hardcode it into the XSLT.
 
  • #3


According to the W3C XPath 2.0 specification, a URI can be entered as an XPath expression by using the "fn:uri" function, which takes a string as its argument and returns an xs:anyURI value. So in your case, the correct syntax would be:

<xsl:param name="foo" as="xsd:anyURI" select="fn:uri('http://foo.bar')"/>

This function can also be used to convert a string value to a URI, so your workaround of casting the string as a URI is not necessary. The correct syntax for that would be:

<xsl:param name="foo" as="xsd:anyURI" select="xs:anyURI('http://foo.bar')"/>

I hope this helps and resolves your issue. It's always important to consult the official documentation when encountering errors or issues in your code.
 

1. What is an AnyURI?

An AnyURI is a data type in XPath that represents a Uniform Resource Identifier (URI). It is used to identify a resource on the web, such as a website or a file.

2. How is AnyURI used in XPath expressions?

AnyURI can be used in XPath expressions to specify the location of a resource, such as an XML document or a web page, that is to be retrieved or accessed.

3. Can AnyURI be used as a node in an XPath expression?

Yes, AnyURI can be used as a node in an XPath expression. It can be used to identify a specific element or attribute in an XML document or to navigate to a specific location on a website.

4. What is the difference between AnyURI and a regular string in XPath?

The main difference between AnyURI and a regular string in XPath is that AnyURI is a specific data type used to represent URIs, while strings can represent any type of textual data. AnyURI also has additional functions and methods that are specific to URIs, such as resolving relative URIs and checking for the validity of a URI.

5. Are there any limitations to using AnyURI in XPath expressions?

Yes, there are some limitations to using AnyURI in XPath expressions. AnyURI can only represent absolute URIs, meaning it cannot be used to represent relative URIs. It also cannot be used to represent non-ASCII characters, as it only supports ASCII characters. Additionally, AnyURI may not work with all types of web protocols, so it is important to check the compatibility before using it in an XPath expression.

Similar threads

  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Quantum Physics
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • STEM Educators and Teaching
4
Replies
128
Views
41K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • Aerospace Engineering
Replies
2
Views
7K
Back
Top