XPath: Query an attribute from a nodeset in a variable - xslt

Within an XSL sheet, I have a nodeset in a variable, $context.
How do I properly query an attribute of the topmost node in $context? Obviously, if $context was the current node, I would just write "#attname", but it is not. Currently I do $context/../#attname which is doesn't look great to me.
EDIT
Ok, the data.
Here is what I see in the VS.
In case you wonder how I got this strange thing: I UNION'ed a node with it's subnodes and an attribute selected out from a different node:
<xsl:with-param name="context" select=". | ../#res" />.
I'm not completely aware what results from this, but I can see that it works. Highlighted is the attribute I'm interested in.
Maybe that creates an attribute attached to nothing, if that makes sence at all :|

$context/../#attname
does not make too much sense. You can't go "up" here, as this would bring you "outside of" $context.
If the node-set contains something like this (a single node)
<node attname="foo">
<bar />
</node>
then:
$context/#attname
If it is like this (a list of nodes):
<node attname="foo">
<bar />
</node>
<node attname="foo">
<bar />
</node>
then:
$context[1]/#attname
All of this does not work if the variable contains a result tree fragment (RTF). In this case, you need to employ an extension function called node-set(). Most XSLT processors provide this function.
EDIT: Your variable holds a union of the current node and a naked attribute node from its parent:
<xsl:with-param name="context" select=". | ../#res" />
The result of a union will always be in document order, so even though you selected the attribute after the context node in the XPath, in the resulting node set it will come before - the parent of the context node is before the context node in document order.
You need $context[1] to grab the attribute node, and $context[2] to grab the other node.
I must say that this is some strange and probably unnecessary complicated use of variables. I'm sure there is a way to do this in a less painful fashion. For example you could do
<xsl:with-param name="context" select="." />
and then use $context/../#res in the called template. That would be a lot more straight-forward than what you are trying now.
Also, if the <xsl:with-param> you show here is part of an <xsl:call-template>, you can drop that param entirely. When a template is called (instead of applied), then the context node does not change, passing it in is redundant.

In addition to tomalak's answer if you ever do need to propagate back up to the root you could try:
ancestor::*[not(..)]
Would love to hear of a situation where you might want/need this though.
Try it yourself by pasting:
//pet/ancestor::*[not(..)]
in this online Xpath test tool.

As you have <xsl:with-param name="context" select=". | ../#res" /> the 'res' attribute is part of the node-set (XPath 1.0) or sequence (XPath 2.0) the variable named 'context' is bound to. With XPath 2.0 you could use $context/self::attribute(res) but XPath 1.0 has no comparable expression so what you have already ($context[1] or $context/../#res) is all you can do in my view.

Related

How can you apply concat(...) in a value-of directive in case of multiple nodes?

I am outputting the name node of each property node in a ; delimited string as following:
<xsl:value-of select="properties/property/name" separator=";" />
I want to alter this such that each element is prefixed with _. An example output should be:
_alpha;_beta;_gamma
I tried the following:
<xsl:value-of select="concat('_', properties/property/name)" separator=";" />
I want to use this to create an output node containing that string:
<my_node>
<xsl:value-of select="concat('_', properties/property/name)" separator=";" />
</my_node>
This gives an error when there are multiple properties:
XPTY0004: A sequence of more than one item is not allowed
as the second argument of fn:concat() (<name>, <name>)
Is there a way to get this working in XSLT 2.0/3.0?
I could resort to the XSLT 1.0 for-each solution as given in https://stackoverflow.com/a/57856287/12042211 (in which we are manually adding the separator), but I am wondering if something elegant in XSLT 2.0/3.0 is possible.
The answer is yes. XSLT 2.0 allows you to write expressions like this...
<xsl:value-of select="properties/property/concat('_', name)" separator=";" />
So, for each property it selects the concatenation of "_" with the name element.
Such syntax is not valid in XSLT 1.0 though.
In XSLT 3.0 I would tend to write this as
<xsl:value-of select="properties/property ! ('_' || name)" separator=";" />
and perhaps use string-join() instead of xsl:value-of. You haven't shown the context, but try to use xsl:value-of only when you really want a text node, not when you just want a string.

XSLT: Can a node be a variable and used elsewhere?

xsl
<xsl:variable name="varName>
<xsl:value-of select="/can/be/a/long/path/down/xml/item[#id=1] />
</xsl:variable>
xml
<xml>
<item id="1" text="Yes">
<item id="2" text="No">
</xml>
use
I was thinking I could use like this:
<xsl:when test="$varName/#text = 'Yes'">
blah
</xsl:when>
but blank space is generated in place of variable. Is this even possible, have a node as a variable and use elsewhere?
<xsl:variable name="varName">
<xsl:value-of select="/can/be/a/long/path/down/xml/item[#id=1]" />
</xsl:variable>
This is one of the most common XSLT errors I see. Usually what people intended is:
<xsl:variable name="varName" select="/can/be/a/long/path/down/xml/item[#id=1]"/>
And most of the time, the code works just fine, except that it's a lot slower than it needs to be. But sometimes the fact that the two constructs are quite different beneath the covers comes back to bite you.
To understand the difference, xsl:variable with a select attribute binds the variable to whatever the select expression evaluates to, which in this case is a set of zero or more item elements. By contrast, xsl:variable with nested instructions creates a document node (XSLT 2.0) or result tree fragment (XSLT 1.0) whose content is a COPY of whatever those instructions produce. In this case, because the content is an xsl:value-of instruction, the variable contains a copy of the string-value of the selected node.
And of course, the string value of the selected node doesn't have any attributes, so test="$varname/#text = 'x'" will always return false.

Does node-set() have an ancestor node?

I feel, this is a basic XSLT question. From how I understand, a node-set is data structure in itself. From a node-set() I cannot navigate to any node that is outside of the set. i.e I cannot reach the parent, the beginning of the xml nor its siblings. Is this right?
or
Is there a way to get a parent of a node-set()
Code
<neighbourhood>
<parent name = "xyz">
<child address=10> a </child>
<child address=10> b </child>
<child address=15> c </child>
</parent>
</neighbourhood>
I have a set of child nodes. I need to eliminate nodes with duplicate 'address'.
There can be n number of 'parent' and m number of 'child' and there could be a grandparent node for the 'child'.
The best and the logical way is probably go by each parent and process their children. But, it is an existing code base and this is an oversimplified example. I do not want to break too many things by touching the caller function and other templates.
So, my question was if I could get the 'parent' with the set of 'child' nodes I have
Thanks for all your responses
Does node-set() have an ancestor node?
node-set() is a (extension) function -- not a node. A function cannot have ancestor, because it is not a node.
I suppose that by "node-set()" in the question, you mean the value that is returned by the xxx:node-set() function (where the prefix "xxx" is bound to a vendor-specific namespace). If so, here is the wanted answer:
By definition, the xxx:node-set() function returns the document-node() (also known as root-node in XPath 1.0) of a temporary tree, which is obtained by converting the RTF (Result Tree Fragment), passed as the only argument to this function.
A document-node by definition is at the top of the document hierarchy and is the only node in an XML document, that doesn't have a parent.
Therefore, the node returned by a called xxx:node-set() function doesn't have any ancestors.
From a node-set() I cannot navigate to any node that is outside of the set. i.e I cannot reach the parent, the beginning of the xml nor its siblings. Is this right?
Yes, without calling another function that returns a node from another document (such as the standard XPath function id() or the standard XSLT function document()), or referencing a variable/parameter, it is not possible to navigate to a node from another document only by using XPath location steps.
or
Is there a way to get a parent of a node-set()
No, the node returned by the xxx:node-set() function is a document node and a document node doesn't have a parent (or any other ancestor) node.
Don't mix up node sets and node-set()s.
What do I mean by this? Well, a node set is a set of nodes. In normal, unextended XSLT 1.0, this means a selection of nodes from your input document. If I do this:
<!-- a node set -->
<xsl:variable name="my-node-set"
select="/indoc/level1/level2"/>
the variable $my-node-set contains a set of level2 nodes, but those nodes still live in the input document. If I subsequently do a for-each like so:
<nodeset-from-indoc>
<xsl:for-each select="$my-node-set/level3">
<parent>
<xsl:value-of select="local-name(..)"/>
</parent>
<grandparent>
<xsl:value-of select="local-name(../..)"/>
</grandparent>
</xsl:for-each>
</nodeset-from-indoc>
I will get the names of the parents and grandparents of each node:
<nodeset-from-indoc>
<parent>level2</parent><grandparent>level1</grandparent>
<parent>level2</parent><grandparent>level1</grandparent>
<parent>level2</parent><grandparent>level1</grandparent>
</nodeset-from-indoc>
If, however, I hard-code nodes into a variable:
<!-- a result-tree fragment -->
<xsl:variable name="my-rtf">
<level2>
<level3>1</level3>
</level2>
<level2>
<level3>2</level3>
</level2>
<level2>
<level3>3</level3>
</level2>
</xsl:variable>
this is not a node set, but a result tree fragment, since they weren't selected from the input document. The problem with result tree fragments is that you can't use XPath on them. I can't, for example, do this:
<xsl:for-each select="$my-rtf/level3">
This is where the node-set() function comes in. It is an extension to XSLT 1.0, which comes from some extension namespace, depending on your XSLT processor. Many processors choose to implement this in the namespace defined by EXSLT.
As Dmitre points out, the node-set() function returns a magic document node of a temporary tree, allowing you to use XPath. However, this causes a subtle shift in how the select needs to be done. Because of the magic document node, I have to include level2 in my selection:
<nodeset-from-rtf>
<xsl:for-each select="exsl:node-set($my-rtf)/level2/level3">
<parent>
<xsl:value-of select="local-name(..)"/>
</parent>
<grandparent>
<xsl:value-of select="local-name(../..)"/>
</grandparent>
</xsl:for-each>
</nodeset-from-rtf>
And in this case, the level3 nodes will have parents, but no grandparents:
<nodeset-from-rtf>
<parent>level2</parent><grandparent/>
<parent>level2</parent><grandparent/>
<parent>level2</parent><grandparent/>
</nodeset-from-rtf>
A node-set is a set of nodes. Each node in the node-set has ancestors. The node-set itself does not. If $NS is a node-set, you can do $NS/ancestor::node(): this will give you all the ancestors of all the nodes in the node-set, with duplicates eliminated.

Constructing, not selecting, XSL node set variable

I wish to construct an XSL node set variable using a contained for-each loop. It is important that the constructed node set is the original (a selected) node set, not a copy.
Here is a much simplified version of my problem (which could of course be solved with a select, but that's not the point of the question). I've used the <name> node to test that the constructed node set variable is in fact in the original tree and not a copy.
XSL version 1.0, processor is msxsl.
Non-working XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:variable name="entries">
<xsl:for-each select="//entry">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="entryNodes" select="msxsl:node-set($entries)"/>
<xsl:for-each select="$entryNodes">
<xsl:value-of select="/root/name"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML input:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<name>X</name>
<entry>1</entry>
<entry>2</entry>
</root>
Wanted output:
X1X2
Actual output:
12
Of course the (or a) problem is the copy-of, but I can't work out a way around this.
There isn't a "way around it" in XSLT 1.0 - it's exactly how this is supposed to work. When you have a variable that is declared with content rather than with a select then that content is a result tree fragment consisting of newly-created nodes (even if those nodes are a copy of nodes from the original tree). If you want to refer to the original nodes attached to the original tree then you must declare the variable using select. A better question would be to detail the actual problem and ask how you could write a suitable select expression to find the nodes you want without needing to use for-each - most uses of xsl:if or xsl:choose can be replaced with suitably constructed predicates, maybe involving judicious use of xsl:key, etc.
In XSLT 2.0 it's much more flexible. There's no distinction between node sets and result tree fragments, and the content of an xsl:variable is treated as a generic "sequence constructor" which can give you new nodes if you construct or copy them:
<xsl:variable name="example" as="node()*">
<xsl:copy-of select="//entry" />
</xsl:variable>
or the original nodes if you use xsl:sequence:
<xsl:variable name="example" as="node()*">
<xsl:sequence select="//entry" />
</xsl:variable>
I wish to construct an XSL node set variable using a contained
for-each loop.
I have no idea what that means.
It is important that the constructed node set is the original (a
selected) node set, not a copy.
This part I think I understand a little better. It seems you need to replace:
<xsl:variable name="entries">
<xsl:for-each select="//entry">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
with:
<xsl:variable name="entries" select="//entry"/>
or, preferably:
<xsl:variable name="entries" select="root/entry"/>
The resulting variable is a node-set of the original entry nodes, so you can do simply:
<xsl:for-each select="$entries">
<xsl:value-of select="/root/name"/>
<xsl:value-of select="."/>
</xsl:for-each>
to get your expected result.
Of course, you could do the same thing by operating directly on the original nodes, in their original context - without requiring the variable.
In response to the comments you've made:
We obviously need a better example here, but I think I am getting a vague idea of where you want to go with this. But there are a few things you must understand first:
1.
In order to construct a variable which contains a node-set of nodes in their original context, you must use select. This does not place any limits whatsoever on what you can select. You can do your selection all at once, or in stages, or even in a loop (here I mean a real loop). You can combine the intermediate selections you have made in any way sets can be combined: union, intersection, or difference. But you must use select in all these steps, otherwise you will end up with a set of new nodes, no longer having the context they did in the source tree.
IOW, the only difference between using copy and select is that the former creates new nodes, which is precisely what you wish to avoid.
2.
xsl:for-each is not a loop. It has no hierarchy or chronology. All the nodes are processed in parallel, and there is no way to use the result of previous iteration in the current one - because no iteration is "previous" to another.
If you try to use xsl:for-each in order to add each of n processed nodes to a pre-existing node-set, you will end up with n results, each containing the pre-existing node-set joined with one of the processed nodes.
3.
I think you'll find the XPath language is quite powerful, and allows you to select the nodes you want without having to go through the complicated loops you hint at.
It might help if you showed us a problem that can't be trivially solved in XSLT 1.0. You can't solve your problem the way you are asking for: there is no equivalent of xsl:sequence in XSLT 1.0. But the problem you have shown us can be solved without such a construct. So please explain why you need what you are asking for.

Refer to specific cell in xslt import/export filter for Calc

I am using xslt filter for importing/exporting data from Calc worksheet. Is it possible to refer to a specific cell address ? For example, if we want to export data from cell B2, how do we refer to this cell address in export xslt ?
Without knowing much about Openoffice or their xslt filter function, I can tell you that you're probably going to need a fairly simple XPath to reference a specific Cell's data - I doubt it would be as simple as calling getCell('B2') unless they have provided you with some custom xslt functions (I'm assuming they've put you in a raw XSLT environment).
Anyway, I think this question may be more about XSLT and xpath, than it is about openoffice. With that in mind, I'm going to fashion my own sample xml and examples and hopefully that will be enough to get you started.
For an input xml that looks something like this:
<ooo_calc_export>
<ooo_sheet num="1" name="sheet1">
<ooo_row num="2">
<fisrtCell>Oh</firstCell>
<secondCell>Hai</secondCell>
<thirdCell>There</thirdCell>
</ooo_row>
<ooo_row num="3">
<fisrtCell>Oh</firstCell>
<secondCell>Hello</secondCell>
<thirdCell>Back!</thirdCell>
</ooo_row>
</ooo_sheet>
</ooo_calc_export>
An absolute XPath to access cell B2's data would look like this ooo_calc_export/ooo_sheet/ooo_row[#num='2']/secondCell/text()
But the above is an absolute path and in XSLT, we would often author relative xpaths as we are in the midst of processing a document. Imagine you're in a template which matches on the ooo_calc_export node and you wanted to store Cell B2's data in a variable for later use. Consider this example:
<xsl:template match="/ooo_calc_export">
<!-- a relative xpath does not being with a forward slash -->
<xsl:variable name="B2" select="ooo_sheet/ooo_row[#num='2']/secondCell/text()" />
</xsl:template>
Now lets imagine you wanted a template to match on the cell B2 node itself:
<xsl:template match="ooo_row[#num='2']/secondCell">
<!-- a relative xpath does not being with a forward slash -->
<xsl:variable name="B2_text" select="text()" />
</xsl:template>
This is a good tutorial on XSLT to get you started. Also, the W3 Schools references on XPath and XSLT aren't the worst.