Testing text node in XSLT - xslt

I have the following mix-content element:
<firstElement type="random">text1<secondElement>random_value</secondElement>text2</firstElement>
I want to make a for-each loop on <firstElement> child nodes with a nested if condition, for example:
<xsl:for-each select="child::*">
<xsl:if test="some test">
<xsl:copy>
</xsl:if>
</xsl:for-each>
How can I write a test that selects only the text nodes of <firstElement>?
I have tried with text(), but it only works for the child text node of the contextual node and self::text() doesn't seem to be a proper Xpath. I also tried to use XSLT 2.0 instance of to test against xs:string but it didn't work either.

How can I write a test that selects only the text nodes of
<firstElement>?
It is really difficult to understand your question:
First, a test does not select anything - I suppose you mean you want the test to pass text nodes only.
Next, when you do:
<xsl:for-each select="child::*">
you are selecting element nodes only - so any subsequent test that passes text nodes only will always return false.
It's also not clear why you need to select any nodes that are not text nodes in the first place, and then test them and pass only text nodes. But supposing that's really what you want to do, you can do it this way:
<xsl:template match="firstElement">
<output>
<!-- select all child nodes -->
<xsl:for-each select="node()">
<!-- pass only text nodes -->
<xsl:if test=". instance of text()">
<xsl:copy/>
</xsl:if>
</xsl:for-each>
</output>
</xsl:template>
This returns:
<output>text1text2</output>
which is the same result returned by the much simpler:
<xsl:template match="firstElement">
<output>
<xsl:copy-of select="text()"/>
</output>
</xsl:template>
I have tried with text(), but it only works for the child text node of the contextual node and self::text() doesn't seem to be a proper Xpath.
I am not sure why you think so.
<xsl:if test="self::text()">
works just as well - and has the advantage of being backward-compatible with XSLT 1.0.

Related

check if repeating node is empty in xslt

I have xml like defined below . The node EducationDetails can repeat (unbounded).
<PersonalDetailsResponse>
<FirstName></FirstName>
<LastName></LastName>
<EducationDetails>
<Degree></Degree>
<Institution></Institution>
<Year></Year>
</EducationDetails>
<EducationDetails>
<Degree></Degree>
<Institution></Institution>
<Year></Year>
</EducationDetails>
</PersonalDetailsResponse>
I want to create another xml from the above one using xslt.
My requirement is, if there is no data in any of the EducationDetails child nodes , then the resulting xml has to get data from another source.
My problem is , I am not able to check if all the EducationDetails child nodes are empty.
Since variable value cannot be changed in xslt , I tried using saxon with below code.
xmlns:saxon="http://saxon.sf.net/" extension-element-prefixes="saxon"
<xsl:variable name="emptyNode" saxon:assignable="yes" select="0" />
<xsl:when test="count(ss:education) > 0">
<xsl:for-each select="ss:education">
<xsl:if test="not(*[.=''])">
<saxon:assign name="emptyNode">
<xsl:value-of select="1" />
</saxon:assign>
</xsl:if>
</xsl:for-each>
<xsl:if test="$emptyNode = 0">
<!-- Do logic if all educationdetails node is empty-->
</xsl:if>
</xsl:when>
But it throwing exception "net.sf.saxon.trans.XPathException: Unknown extension instruction " .
It looks like saxon 9 jar is required for it ,which I am not able to get from my repository.
Is there a simpler way to check if all the child nodes of are empty.
By empty I mean, child nodes might be present, but no value in them.
Well, if you use <xsl:template match="PersonalDetailsResponse[EducationDetails[*[normalize-space()]]">...</xsl:template> then you match only on PersonalDetailsResponse element having at least one EducationDetails element having at least one child element with non whitespace data. As you seem to use an XSLT 2.0 processor you can also use the perhaps clearer <xsl:template match="PersonalDetailsResponse[some $ed in EducationDetails/* satisfies normalize-space($ed)]">...</xsl:template>.
Or perhaps if you want a variable inside the template use
<xsl:template match="PersonalDetailsResponse">
<xsl:variable name="empty-details" select="not(EducationDetails/*[normalize-space()])"/>
<xsl:if test="$empty-details">...</xsl:if>
</xsl:template>
With XSLT 2.0 the use of some or every satisfies might be easier to understand e.g.
<xsl:template match="PersonalDetailsResponse">
<xsl:variable name="empty-details" select="every $dt in EducationDetails/* satisfies not(normalize-space($dt))"/>
<xsl:if test="$empty-details">...</xsl:if>
</xsl:template>
But usually writing templates with appropriate match conditions saves you from using xsl:if or xsl:choose inside of a template.

Check any level of parent nodes with XPath and XSLT

I have searched, but might have missed something obvious just because I don't know what to search for. And I found it hard to explain my question as a simple question, so let me explain: I am using the following code (XSLT 1.0 & XPath) to check if the parent to this node belongs to the last grand-parent node or not:
<xsl:when test="count(parent::*/preceding-sibling::*)+1 = count(parent::*/parent::*/*)">
It works exactly like I want. What I would like though is to make it more general in one way or the other, to make it work with even more parent nodes. I can add another template match and add another test:
<xsl:when test ="count(parent::*/parent::*/preceding-sibling::*)+1 = count(parent::*/parent::*/*)">
Is there a way to add "parent::*/" in a recursive template loop instead of creating a lot of specific template matches? Or should I work out a better XPath code altogether?
Please note: I want to do a check for every level of parent nodes. Is the parent the last of the parents, is the grand-parent the last of the grand-parents, etc.
For clarity's sake, I use it like this:
<xsl:choose>
<xsl:when test="count(parent::*/preceding-sibling::*)+1 = count(parent::*/parent::*/*)">
<!-- show image A -->
</xsl:when>
<xsl:otherwise>
<!-- show image B -->
</xsl:otherwise>
</xsl:choose>
<xsl:when test="count(parent::*/preceding-sibling::*)+1 = count(parent::*/parent::*/*)">
can be simplified to:
<xsl:when test="not(../following-sibling::*)">
In plain English "my parent does not have a following element sibling".
That would be easily modifiable to:
<xsl:when test="not(../../following-sibling::*)">
In plain English "my parent's parent does not have a following element sibling". Etc.
To check all ancestors at the same time:
<xsl:when test="not(ancestor::*/following-sibling::*)">
In plain English "none of my ancestors has a following element sibling".
To check parent and grandparent at the same time:
<xsl:when test="not(ancestor::*[position() <= 2]/following-sibling::*)">
In plain English "none of my two closest ancestors has a following element sibling".
EDIT To check all ancestors individually, either use a recursive template (advantage: the position of the inner <xsl:apply-templates> determines if you effectively go up or down the list of ancestors):
<xsl:template match="*" mode="line-img">
<xsl:if test="following-sibling::*">
<!-- show image A -->
</xsl:if>
<xsl:if test="not(following-sibling::*)">
<!-- show image B -->
</xsl:if>
<xsl:apply-templates select=".." mode="line-img" />
</xsl:template>
<!-- and later... -->
<xsl:apply-templates select=".." mode="line-img" />
...or a simple for-each loop (always works in document order):
<xsl:for-each select="ancestor::*">
<xsl:if test="following-sibling::*">
<!-- show image A -->
</xsl:if>
<xsl:if test="not(following-sibling::*)">
<!-- show image B -->
</xsl:if>
</xsl:for-each>
...or, to be completely idiomatic (always works in document order):
<xsl:template match="*" mode="line-img">
<xsl:if test="following-sibling::*">
<!-- show image A -->
</xsl:if>
<xsl:if test="not(following-sibling::*)">
<!-- show image B -->
</xsl:if>
</xsl:template>
<!-- and later... -->
<xsl:apply-templates select="ancestor::*" mode="line-img" />

XSLT for-each counter - how to access data

for performance testing purposes I want to take a small XML file and create a bigger one from it - using XSLT. Here I plan to take each entity (Campaign node in the example below) in the original XML and copy it n times, just changing its ID.
The only way I can think of to realize this, is a xsl:for-each select "1 to n". But when I do this I do not seem to be able to access the entity node anymore (xsl:for-each select="campaigns/campaign" does not work in my case). I am getting a processor error: "cannot be used here: the context item is an atomic value".
It seems that by using the "1 to n" loop, I am loosing the access to my actual entity. Is there any XPath expression that gets me access back or does anyone have a completely different idea how to realize this?
Here is what I do:
Original XML
<campaigns>
<campaign id="1" name="test">
<campaign id="2" name="another name">
</cmpaigns>
XSLT I try to use
<xsl:template match="/">
<xsl:element name="campaigns">
<xsl:for-each select="1 to 10">
<xsl:for-each select="campaigns/campaign">
<xsl:element name="campaign">
<xsl:copy-of select="#*[local-name() != 'id']" />
<xsl:attribute name="id"><xsl:value-of select="#id" /></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:for-each>
</xsl:element>
</xsl:template>
Define a variable as the first thing in the match, like so:
<xsl:variable name="foo" select="."/>
This defines a variable $foo of type nodeset. Then access it like this
<xsl:for-each select="$foo/campaigns/campaign">
...
</xsl:for-each>

How to loop through a list of nodes and create new nodes based on the child nodes of those I'm looping through

This may be a basic question but this newbie has been struggling and Googling and hasn't been able to figure it out.
I have an xml document similar to this.
<x99:events xmlns:x99="http://www.foo.com/x99" xmlns:xl="http://www.w3.org/1999/xlink" pubdate="2012-05-29T11:14:14-06:00">
<x99:event xl:href="event.xml?event_id=255918" id="foo" status="new">
<x99:event_id>255918</x99:event_id>
<x99:custom_attribute xmlns:x99="http://www.foo.com/x99" status="new">
<x99:attribute_id>22</x99:attribute_id>
<x99:attribute_value>hi there</x99:attribute_value>
</x99:custom_attribute>
<x99:custom_attribute xmlns:x99="http://www.foo.com/x99" status="new">
<x99:attribute_id>26</x99:attribute_id>
<x99:attribute_value>this is a test</x99:attribute_value>
</x99:custom_attribute>
<x99:custom_attribute xmlns:x99="http://www.foo.com/x99" status="new">
<x99:attribute_id>12</x99:attribute_id>
<x99:attribute_value>Yes</x99:attribute_value>
</x99:custom_attribute>
</x99:event>
</x99:events>
And I have some xsl that transforms the xml.
In my xsl I need to be able to loop through the custom_attribute nodes and, for each attribute_id I find I need to create a node with some values based on child nodes of the custom_attribute node.
Here's my pseudo-code. I need something like this.
<xsl:for-each select="x99:custom_attribute">
<xsl:when test="number(x99:attribute_id) = 22>
<x99:text>You chose twenty two and your attribute value is <x99:attribute_value></x99:text>
</xsl:when>
<xsl:when test="number(x99:attribute_id) = 26>
<x99:text>Twenty six is a great answer! and your attribute value is <x99:attribute_value></x99:text></x99:text>
</xsl:when>
</xsl:for-each>
And here is my xsl.
My xsl skills are at the most basic level and my xml isn't much better either. Can some kind soul give me some advice? I'm in a bit over my head.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xl="http://www.w3.org/1999/xlink"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:b="http://www.someurl.com/b"
xmlns:s="http://www.someurl.com/s"
xmlns:c="http://foo.com/c"
xmlns:x99="http://foo.com/x99"
exclude-result-prefixes="xl x99">
<xsl:param name="base_url" select="''" />
<xsl:param name="session_id" select="''" />
<xsl:param name="TaskDir" select="''" />
<xsl:template match="/">
<xsl:call-template name="SendConfirmationEmail">
<xsl:with-param name="EventID" select="/x99:events/x99:event/x99:event_id"/>
<xsl:with-param name="SiteURL" select="'https://foobar.com/123Test/#details'" />
</xsl:call-template>
<xsl:apply-templates select="node()" />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template name="SendConfirmationEmail">
<xsl:param name="EventID" select="''"/>
<xsl:result-document>
<schedule>
<job>
<name>Email Confirmation</name>
<active>T</active>
<http>
<body>
<x99:email xmlns:x99="http://foo.com/x99">
<x99:mail>
<x99:body>
<x99:text>Your event ID is <xsl:value-of select="$EventID"/></x99:text>
// I want to be able to loop through my x99:attribute_id values here and create new x99:text nodes.
</x99:body>
</x99:mail>
</x99:email>
</body>
</http>
</job>
</schedule>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
So frankly I need to know how to...
Loop through the custom_attribute nodes and for each node create a x99:text node that contains
a string based on the value of the child attribute_id
the contents of the attribute_value node
First, instead of using a named template with parameters as if you were programming in FORTRAN using CALL to invoke subroutines, just define a template to handle the event_id element, such as <xsl:template match="event_id">. I assume you mean html, not http. You'll need to specify a href attribute on the <xsl:result-document> element so it knows where to put the document. You're apparently writing elements into your HTML from the x99 namespace--is that really what you want to do?
With regard to "looping" over the attribute elements, in an XSLT mindset it would be preferable to say "process" them. Under <x99:text> add a <xsl:apply-templates/> element, then define a template to handle the custom attribute elements (<xsl:template match="x99:custom-attribute">. Within that template, place something like what you have within the for-each of your pseudo-code, except that instead of saying number(x99:attribute_id) you'll just want number(.) since we are already at that node. However, note that <xsl:when> elements can only go within a <xsl:choose>.

XSL: How best to store a node in a variable and then use it in future xpath expressions?

I need to be able to store a node set in variable and then perform more filting/sorting on it afterward. All the examples I've seen of this involve either using XSL2 or extensions neither of which are really an option.
I've a list of hotels in my XML doc that can be sorted/filtered and then paged through 5 at a time. I'm finding though I'm repeating alot of the logic as currently I've not found a good way to store node-sets in xsl variable and then use xpath on them for further filtering/sorting.
This is the sort of thing I'm after (excuse the code written of the top of my head so might not be 100%):
<xsl:variable name="hotels" select="/results/hotels[active='true']" />
<xsl:variable name="3_star_or_less" select="/results/hotels[number(rating) <= 3]" />
<xsl:for-each select="3_star_or_less">
<xsl:sort select="rating" />
</xsl:for-each>
Has anyone got an example of how best to do this sort of thing?
Try this example:
<xsl:variable name="hotels" select="/results/hotels[active='true']" />
<xsl:variable name="three_star_or_less"
select="$hotels[number(rating) <= 3]" />
<xsl:for-each select="$three_star_or_less">
<xsl:sort select="rating" />
<xsl:value-of select="rating" />
</xsl:for-each>
There is no problem storing a node-set in a variable in XSLT 1.0, and no extensions are needed. If you just use an XPath expression in select attribute of xsl:variable, you'll end up doing just that.
The problem is only when you want to store the nodes that you yourself had generated in a variable, and even then only if you want to query over them later. The problem here is that nodes you output don't have type "node-set" - instead, they're what is called a "result tree fragment". You can store that to a variable, and you can use that variable to insert the fragment into output (or another variable) later on, but you cannot use XPath to query over it. That's when you need either EXSLT node-set() function (which converts a result tree fragment to a node-set), or XSLT 2.0 (in which there are no result tree fragments, only sequences of nodes, regardless of where they come from).
For your example as given, this doesn't seem to be a problem. Rubens' answer gives the exact syntax.
Another note, if you want to be able to use the variable as part of an XPath statement, you need to select into the variable with <xsl:copy-of select="."/> instead of <xsl:value-of select="."/>
value-of will only take the text of the node and you wont be able to use the node-set function to return anything meaningful.
<xsl:variable name="myStringVar">
<xsl:value-of select="."/>
</xsl:variable>
<!-- This won't work: -->
<Output>
<xsl:value-of select="node-set($myStringVar)/SubNode" />
</Output>
<xsl:variable name="myNodeSetVar">
<xsl:copy-of select="."/>
</xsl:variable>
<!-- This will work: -->
<Output>
<xsl:value-of select="node-set($myNodeSetVar)/SubNode" />
</Output>