Trying to work out how to select the following-sibling of an XSLT node when the node has been sorted in XSLT 1.0. I've searched but can't find anything for sorted nodes, as it only selects the sibling of the unsorted node.
Data
<data>
<number order='4'>Four</number>
<number order='1'>One</number>
<number order='3'>Three</number>
<number order='2'>Two</number>
</data>
Code
<xsl:for-each select="/data/number">
<xsl:sort select="#order"/>
<xsl:if test="position() mod 2 = 1">
<xsl:value-of select="text()"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="following-sibling::*/text()"/>
</xsl:if>
</xsl:for-each>
Expected output
One - Two
Three - Four
Actual Output
One - Three
Three - Two
When you sort a sequence of nodes, you get the same nodes in a new sequence. Because they are the same nodes, they have the same siblings that they always had. If you copy the nodes to a result tree, then the copies will have new siblings, but that's because of the action of writing them to a result tree, not because of the sorting action.
Another way of putting this: you are processing a sequence of nodes that aren't siblings, so you can't use following-sibling to get the next node in the sequence.
Processing a sorted sequence of nodes becomes much easier in XSLT 2.0, which allows such a sequence to be bound to a variable. XSLT 1.0 only has node-sets, so sequences of nodes in a particular order can only exist transiently.
But in this particular case, it seems easy enough to do
<xsl:value-of select="."/>
<xsl:if test="position() mod 2 = 1">
<xsl:text> - </xsl:text>
</xsl:if>
it only selects the sibling of the unsorted node.
That is correct. Why don't you do simply:
<xsl:for-each select="/data/number">
<xsl:sort select="#order"/>
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<xsl:text>-</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
Note that the default sort data-type is text; you probably want to make it:
<xsl:sort select="#order" data-type="number"/>
Related
I need some help understanding how I can select only certain nodes having same parent (or uncle) element value. My XML looks like this
<shipment>
<goodslines>
<goodsline>
<position>1</position>
<packagenumbers>
<packagenumber>123</packagenumber>
</packagenumbers>
</goodsline>
<goodsline>
<position>1</position>
<packagenumbers>
<packagenumber>456</packagenumbers>
</packagenumbers>
</goodsline>
<goodsline>
<position>2</position>
<packagenumbers>
<packagenumber>789</packagenumbers>
</packagenumbers>
</goodsline>
</goodslines>
</shipment>
and the desired output would be:
123,456
789
So I would need to do for-each to "packagenumber" - level so, that it would take in consideration the "position" - element from upper level
The XSL might be something like this?
<xsl:for-each select="shipment/goodslines/goodsline[some condition here?]/packagenumbers/packagenumber">
<xsl:value-of select="current()"/>
<xsl:if test="not(position() = last())">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:text>
</xsl:text>
</xsl:for-each>
Any help would be appreciated.
If your xml always has the same structure you can use this:
<xsl:template match="goodsline[position = preceding-sibling::goodsline[1]/position]">
<xsl:text>,</xsl:text>
<xsl:value-of select="packagenumbers/packagenumber"/>
</xsl:template>
<xsl:template match="goodsline">
<xsl:text>
</xsl:text>
<xsl:value-of select="packagenumbers/packagenumber"/>
</xsl:template>
More specific templates (the one with the condition) will always hit first and have higher priority. There is probably another simple solution with for-each-group.
I have short question.
I have 2 Lines but why it dispays 'mixed' one time?
<xsl:variable name="relItems-nodes">
<xsl:for-each select="Lines/Line">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($relItems-nodes)">
mixed
</xsl:for-each>
Your variable relItems-nodes is a result tree fragment with a root node containing various Line elements, the use of msxsl:node-set($relItems-nodes) converts that into a node-set with a root node containing various Line elements so if you don't want to process the root node but the contained Line elements use <xsl:for-each select="msxsl:node-set($relItems-nodes)/Line">...</xsl:for-each>.
By the way,
<xsl:for-each select="Lines/Line">
<xsl:copy-of select="."/>
</xsl:for-each>
could be shortened to <xsl:copy-of select="Lines/Line"/>.
<handlingInstruction>
<handlingInstructionText>CTAC | MARTINE HOEYLAERTS</handlingInstructionText>
</handlingInstruction>
<handlingInstruction>
<handlingInstructionText>PHON | 02/7225235</handlingInstructionText>
</handlingInstruction>
I have The above given xml structure I concatenate them and use a comma as a separator using below code
> <xsl:value-of
> select="concat(handlingInstruction[1]/handlingInstructionText,
> ',',
> handlingInstruction[2]/handlingInstructionText)"/>
I would like to ask how will I make the comma separator appear only once the 2nd exist the shortest way possible. Thanks in advance
If you don't want to use xsl:for-each, try:
<xsl:template match="/root">
<xsl:apply-templates select="handlingInstruction/handlingInstructionText"/>
</xsl:template>
<xsl:template match="handlingInstructionText">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>
(continued from here: https://stackoverflow.com/a/34679465/3016153)
<xsl:for-each select="handlingInstruction">
<xsl:value-of select="handlingInstructionText"/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
This will iterate over all handlingInstruction elements and output the value of the handlingInstructionText element. It will add to the end of each element, if it is not the last one (which the first one would be if there was only one), a comma.
In your example, you only used two handlingInstruction elements. If you want to only use two with this method, do
<xsl:for-each select="handlingInstruction[position()<3]">
<xsl:value-of select="handlingInstructionText"/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
Note the < there. That is actually a less than sign (<), but we can't use that in xml so we use the entity defined for it.
Here is a second way to do it, which avoids the for-each loop.
If you are using xslt version 2, there is a string-join function which could be used like:
<xsl:value-of select="string-join(//handlingInstruction/handlingInstructionText,',')"/>
The string-join method takes a sequence of strings (which the nodes selected will be converted to by taking their content) and concatenates them with the separator. If there is only one string, a separator will not be added.
Alternatively, xslt 2 also provides a separator attribute on the value-of element. Thus
<xsl:value-of select="//handlingInstruction/handlingInstructionText" separator=","/>
produces the same result.
Problem I have is I want to loop round the parents making them bold then get the children via the id:pid (parent id) and list them. My second loop doesn't work.
XML
XSL
<xsl:choose>
<xsl:when test="#PARENT_OBH_ID">
<b><xsl:value-of select="#TITLE"/></b>
<xsl:for-each select="FOOTER">
-<xsl:value-of select="#TITLE"/>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:for-each>
Thanks
You're probably better off restructuring this to use templates, the system you're using at the moment means that the context data is becoming confused (you're xslt parser isn't sure which element it should read attributes from inside the second loop)
<xsl:choose>
<xsl:when test="#PARENT_OBH_ID">
<b><xsl:value-of select="#TITLE"/></b>
<xsl:apply-templates select="FOOTER" />
</xsl:when>
</xsl:choose>
<xsl:template match="FOOTER">
<xsl:value-of select="#TITLE"/>
</xsl:template>
apply-templates restarts the context with the footer element as the main focus (so #TITLE refers to the title attribute on footer, which is what you were aiming for I am guessing?)
I have this simple code:
<xsl:for-each select="GroupsServed">
<xsl:value-of select="."/>,<br/>
</xsl:for-each></font>
I'm trying to add a comma for each item added.
This has 2 flaws:
Case of when there's only 1 item: the code would unconditionally add a comma.
Case of when there's more than 1 item: the last item would have a comma to it.
What do you think is the most elegant solution to solve this?
I'm using XSLT 2.0
If you're using XSLT 2.0, the canonical answer to your problem is
<xsl:value-of select="GroupsServed" separator=", " />
On XSLT 1.0, the somewhat CPU-expensive approach to finding the last element in a node-set is
<xsl:if test="position() = last()" />
Final answer:
<xsl:for-each select="GroupsServed">
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test="position() != last()">,<br/></xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:variable name="GROUPS_SERVED_COUNT" select="count(GroupsServed)"/>
<xsl:for-each select="GroupsServed">
<xsl:value-of select="."/>
<xsl:if test="position() < $GROUPS_SERVED_COUNT">
,<br/>
</xsl:if>
</xsl:for-each></font>
Insert the column delimiter before each new item, except the first one. Then insert the line break outside the for-each loop.
<xsl:for-each select="GroupsServed">
<xsl:if test="position() != 1">,</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
<br/>
In other words, treat every item like the last item. The exception is that the first item does not need a comma separator in front of it. The loop ends after the last item is processed, which also tells us where to put the break.