XSLT-1.0 can a variable be used to access to other nodes? - variables

With a simple XML like this
<value>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</value>
I want to know if it is possible to access to a particular node from a node previously stored in a variable. The XSLT code is very short and explains better what I want to say
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node"/>
</root>
</xsl:template>
</xsl:stylesheet>
So I store the node in the variable "node". Then I can print the contents of the node with $node.
(EDIT) XML output
<root>
<num>
<accession>111</accession>
<sequence>AAA</sequence>
<score>4000</score>
</num>
</root>
What I want to do is to print the contents of a sub-node, like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value/num">
<xsl:variable name="node">
<xsl:copy-of select="current()"/>
</xsl:variable>
<root>
<xsl:copy-of select="$node/accession"/>
</root>
</xsl:template>
</xsl:stylesheet>
But it gives an error (Component returned failure code: 0x80600008 [nsIXSLTProcessor.transformToFragment]) (check here)
(EDIT) The XML that I would want is
<root>
<accession>111</accession>
</root>
NOTE: The question is not how can I get this output. The question is how, using a variable as in the XSLT provided, can I get this output.
(EDIT:SOLVED)
Actually it is possible, but as pointed out in the comments, the value of a variable has to be assigned with the "select" attribute if a node-set is required. So this code was not working since the variable had a tree fragment instead of a node-set stored in it (read more information here)
Thanks!

Try this:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/value">
<root>
<xsl:for-each select="num">
<xsl:variable name="node" select="current()" />
<xsl:copy-of select="$node/accession" />
</xsl:for-each>
</root>
</xsl:template>
</xsl:transform>
Note that I used xsl:transform instead of xsl:stylesheet. Also, consider using version 2.0 instead of 1.0 if you have a compliant processor, it adds a lot of useful features.
I still don't see your need for a variable, though.

Related

Need to create a function with argument in XSLT 1.0

Need small help in creating a function with parameter and that can be used in my template. Tried many things but nothing worked out. Below is my sample code :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:math="http://exslt.org/math"
xmlns:func="http://exslt.org/functions"
xmlns:my="http://exslt.org/my"
exclude-result-prefixes="my"
extension-element-prefixes="my math func">
<xsl:output indent="yes" method="xml" />
<xsl:variable name="functionTest11">
<xsl:value-of select="my:calXY('aa')" />
</xsl:variable>
<xsl:template match="/">
<svg xmlns="http://www.w3.org/2000/svg">
<xsl:variable name="functionTest22">
<xsl:value-of select="my:calXY" />
</xsl:variable>
<xsl:comment>functionTest <xsl:copy-of
select="$functionTest22" /></xsl:comment>
</svg>
</xsl:template>
<func:function name="my:calXY">
<xsl:comment>functionHello </xsl:comment>
<xsl:param name="string1" select="''"/>
<func:result>
<xsl:value-of select="concat(10,'|',10)" />
</func:result>
</func:function>
</xsl:stylesheet>
I don't have convenient access (that I know of) to Xalan, which appears to be the XSLT processor you are using. So I cannot test any conjectures about the origin of your problem.
But it would surprise me a great deal if putting an output instruction like xsl:comment before the first (and only) xsl:param were correct. (It surprises me that you don't report any error messages from the stylesheet compiler. But it's clear from the error message you quote that Xalan has not successfully compiled the function.) It's not clear what you are trying to accomplish by having your function return both a comment node and the string value '10|10', but this is probably not the way to accomplish it.
On the assumption that the xsl:comment instruction is an attempt to verify that the function has in fact been evaluated, I'd try something like
<func:function name="my:calXY">
<xsl:param name="string1" select="''"/>
<func:result select="concat(10,'|',10)" />
</
[In an earlier version of this answer, I speculated that func:result is not needed here; my error. It does appear to be needed.]
You ask for a complete stylesheet. The following stylesheet functions as expected when run with xsltproc. (As I noted above, I don't have a convenient way to run Xalan.)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:func="http://exslt.org/functions"
xmlns:my="http://example.org/my"
exclude-result-prefixes="my"
extension-element-prefixes="func">
<xsl:output indent="yes" method="xml" />
<xsl:template match="/">
<xsl:message>Result is <xsl:value-of select="my:dummy('foo')"/></xsl:message>
<output>
<test><xsl:value-of select="my:dummy('bar')"/></test>
<test><xsl:value-of select="my:dummy('baz')"/></test>
</output>
</xsl:template>
<func:function name="my:dummy">
<xsl:param name="s" select="'foo'"/>
<func:result select="concat($s,'|',$s)" />
</func:function>
</xsl:stylesheet>

Date comparison in XSLT using Java

I am using the below code snippet to fetch the current date in XSLT:
<xsl:variable name="currentDate">
<xsl:variable name="datePattern">yyyy-MM-dd'T'HH:mm:ss Z</xsl:variable>
<xsl:value-of select="java:format(java:java.text.SimpleDateFormat.new($datePattern), java:java.util.Date.new())" />
</xsl:variable>
Requirement is, to check if ShippedDate attribute value is equal to today's date, then include <Monitor> element, else exclude.
<Order>
<Monitors>
<Monitor ShippedDate="2015-11-10T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-03T00:00:00-05:00"/>
</Monitors>
</Order>
Can somebody please help me with what function needs to be used to achieve this?
Requirement is, to check if ShippedDate is equal to today's date, then
include <Monitor> element, else exclude.
I would suggest you do it this way:
XSLT 1.0 (+EXSLT)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Monitors">
<xsl:copy>
<xsl:apply-templates select="Monitor[substring(#ShippedDate, 1, 10) = substring(date:date(), 1, 10)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Test input:
<Order>
<Monitors>
<Monitor ShippedDate="2015-12-01T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-02T00:00:00-05:00"/>
<Monitor ShippedDate="2015-12-03T00:00:00-05:00"/>
</Monitors>
</Order>
Result, when applied on December 2, 2015:
<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Monitors>
<Monitor ShippedDate="2015-12-02T00:00:00-05:00"/>
</Monitors>
</Order>
Note:
This requires a processor that supports the EXSLT date:date() extension function (e.g. Xalan or Saxon 6.5);
Only the date component is compared. If you are running the transformation in a time zone that's different from the time zone used in the input, the result may be inaccurate. If you want to also consider the time zone, then there's more work to do.
Added:
As a continuation of this, if none of the ShippedDate satisfies
condition, then no output must be transformed by XSLT.
Try:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Order">
<xsl:variable name="current" select="Monitors/Monitor[substring(#ShippedDate, 1, 10) = substring(date:date(), 1, 10)]"/>
<xsl:if test="$current">
<Order>
<Monitors>
<xsl:copy-of select="$current"/>
</Monitors>
</Order>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

XSLT: How to select multiple sub nodes into a single list

How can I extract sub nodes to a single list for processing in a template?
Consider the following XML.
<document>
<menu></menu>
<feature>
<header>Header 1</header>
<article>article 1</article>
<article>article 2</article>
</feature>
<feature>
<header>Heading 2</header>
<article>article 1a</article>
<article>article 2a</article>
</feature>
</document>
I'd like to extract all the article nodes to a single list for processing in a template.
I need article nodes to be available at once, because I need to do calculations based on the number of articles there are.
you can try the following stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="list">
<articles>
<xsl:copy-of select="descendant::article"/>
</articles>
</xsl:variable>
<xsl:variable name="vPass1" select="ext:node-set($list)"/>
<xsl:apply-templates select="$vPass1/*"/>
</xsl:template>
<xsl:template match="articles">
<xsl:copy>
<xsl:text>Number of articles: </xsl:text><xsl:value-of select="count(article)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when applied your input, it produces:
<articles>Number of articles: 4</articles>

How to use parameter or variable values as node name?

I am trying to use the value of a parameter or variable as a node name inside a value-of select but so far failed..
So my XML is as below.
<Data>
<Name>John Smith</Name>
<Date>28112012</Date>
<Phone>iphone</Phone>
<Car>BMW</Car>
</Data>
And my incomplete xslt looks like below.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="{$nodename}"/>
</Output>
</xsl:template>
</xsl:stylesheet>
Ideally I want the out put to be
<Output>John Smith</Output>
Is there any way I can do this using XSLT?
I want to be able to select appropriate node based on a users choice.
Thanks
SK
A wild guess, let me know if it works:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="//*[name()=$nodename]" />
</Output>
</xsl:template>
</xsl:stylesheet>

XSLT Data Map with the character < in the data

I'm writing an XSLT data map from one XML schema to another XML schema. The target schema has a String field that will have a copy of the original XML in it. I know that I must convert the control characters in the XML to < and >. I have done that. The issue I'm having is that the data in the XML also has < and > in it as well. This causes problems because I'm sending the XML to a WCF-SQL adapter and when it hits the < in my data it attempts to convert that to < causing XML validation failure.
Here is a snippet of my XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="*" mode="serialize">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
<xsl:apply-templates mode="serialize"/>
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="/">
<proc_Insert xmlns="http://schemas.microsoft.com/Sql/2008/05/TypedProcedures/dbo">
<StagingXML>
<xsl:variable name="nodestring">
<xsl:apply-templates select="*" mode="serialize"/>
</xsl:variable>
<xsl:value-of select="$nodestring"/>
</StagingXML>
</proc_Insert>
</xsl:template>
</xsl:stylesheet>
You need to quote the content, a truely ancient bit of code (predating xslt 1.0 I think:-) that does that is to be found
http://www.openmath.org/cdfiles2/xsl/verb.xsl
although there are newer versions around as well/