I would like to pass a for each loop as a parameter to another template
<xsl:template name="dataTableGroup">
<xsl:call-template name="test">
<xsl:with-param name="pContent1">
<xsl:for-each
select="NewDataSet/Table[generate-id() = generate-id(key('countryKey', concat(Unit, ReportingBusUnitDesc)))]">
<tr>
<td class="columnTextRight">
<xsl:value-of select="ReportingBusUnitDesc"/>
</td>
</tr>
</xsl:for-each>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
and use it here
<xsl:template name="test">
<xsl:param name="pContent1"/>
<xsl:for-each select="$pContent1">
</xsl:for-each>
</xsl:template>
but instead of nodes I get only values. Idealy I would like to have it work with xslt 1.0.
If you use an XSLT 1.0 processor then the parameter value is a result tree fragment so you need to use a processor specific extension function like exsl:node-set (http://exslt.org/exsl/functions/node-set/index.html) first to convert it into a node set e.g.
<xsl:template name="test">
<xsl:param name="pContent1"/>
<xsl:for-each select="exsl:node-set($pContent1)/tr" xmlns:exsl="http://exslt.org/common">
</xsl:for-each>
</xsl:template>
Node that doing a copy of a result tree fragment is possible without using any extension e.g. <xsl:copy-of select="$pContent1"/> should do.
Also for the completeness of the example I have declared the namespace for the extension function on the xsl:for-each element, normally you would put it on the stylesheet's root element xsl:stylesheet/transform and additionally use exclude-result-prefixes="exsl" to ensure the namespace doesn't occur on any result elements.
With an XSLT 2 or 3 processor you shouldn't have any problems to use the variable directly as a fragment node containing your tr elements.
Related
I'm trying to convert a datetime string to a node based datetime in XSLT 1.0.
basically I want to go from
31-12-2014
to:
<Date>
<Day>31</Day
<Month>12</Month>
<Year>2014</Year>
</Date>
To achieve this I created this template:
<xsl:template name="ToDTNodes">
<xsl:param name="dateTimeString"/>
<xsl:variable name="date" select="substring($dateTimeString,1,10)"/>
<xsl:variable name="result">
<DtNode>
<Year>
<xsl:value-of select="substring($date,7,4)"/>
</Year>
<Month>
<xsl:value-of select="substring($date,4,2)"/>
</Month>
<Day>
<xsl:value-of select="substring($date,1,2)"/>
</Day>
</DtNode>
</xsl:variable>
<xsl:copy-of select="msxsl:node-set($result)/DtNode"/>
</xsl:template>
I try to make the template return a node/set instead of a fragment. Note that I also tried this without the /DtNode on the end. That would enable me to call this template without using the node-set function with eacht call.
Sadly I get an exception when trying to access a child:
XslTransformException: To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function
when I try to do this:
<xsl:variable name="result">
<xsl:call-template name="ToDTNodes">
<xsl:with-param name="dateTimeString" select="$SomeNode/BeginDate" />
</xsl:call-template>
</xsl:variable>
<Value>
<xsl:value-of select="$result/Year"/>
</Value>
Is there any way to get a template to return a node-set instead of a string or result tree fragement?
No, with XSLT 1.0 you would need to use an extension element like http://exslt.org/func/elements/result/index.html within an http://exslt.org/func/elements/function/index.html to be able to return a node set and not a result tree fragment. A template will always return a result tree fragment.
I have defined a variable $NodeVariable,
for instance:
<xsl:variable name="NodeVariable">
<aT>
<aT2>foo</aT2>
<aT3>bar</aT3>
</aT>
</xsl:variable>
and in different parts of the code I want to "apply"
different templates to myVariable.
Unfortunately,
I don't know what's the syntax for this.
I've tried the following:
<xsl:for-each select="$NodeVariable">
<xsl:call-template name="ns:ExtractInfo1"/>
</xsl:for-each>
<xsl:copy-of select="$NodeVariable">
<xsl:call-template name="ns:ExtractInfo2"/>
</xsl:for-each>
<xsl:copy-of select="$NodeVariable">
<xsl:call-template name="ns:ExtractInfo3"/>
</xsl:for-each>
which doesn't work.
How to apply a template to a tree fragment?
Assuming you use an XSLT 1.0 processor, you need to convert the result tree fragment to a node set first:
<xsl:variable name="NodeVariable">
<aT>
<aT2>foo</aT2>
<aT3>bar</aT3>
</aT>
</xsl:variable>
<xsl:variable name="NodeSet" select="exsl:node-set($NodeVariable)"/>
(where the stylesheet declares xmlns:exsl="http://exslt.org/common"), then you can apply-templates in different modes as needed e.g.
<xsl:apply-templates select="$NodeSet/aT" mode="m1"/>
and write templates for that mode e.g.
<xsl:template match="aT" mode="m1">
<xsl:value-of select="aT2"/>
</xsl:template>
Of course if you really want to call named templates you could do that as well, but using apply-templates and modes for different processing steps is the preferred way in XSLT in my view.
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>
Apart from rewriting a lot of XSLT code (which I'm not going to do), is there a way to find the position of an element within its parent, when the context is arbitrarily set to something else? Here's an example:
<!-- Here are my records-->
<xsl:for-each select="/path/to/record">
<xsl:variable name="record" select="."/>
<!-- At this point, I could use position() -->
<!-- Set the context to the current record -->
<xsl:for-each select="$record">
<!-- At this point, position() is meaningless because it's always 1 -->
<xsl:call-template name="SomeTemplate"/>
</xsl:for-each>
</xsl:for-each>
<!-- This template expects the current context being set to a record -->
<xsl:template name="SomeTemplate">
<!-- it does stuff with the record's fields -->
<xsl:value-of select="SomeRecordField"/>
<!-- How to access the record's position in /path/to or in any other path? -->
</xsl:template>
NOTE: This is a simplified example. I have several constraints keeping me from implementing obvious solutions, such as passing new parameters to SomeTemplate, etc. I can really only modify the internals of SomeTemplate.
NOTE: I'm using Xalan 2.7.1 with EXSLT. So those tricks are available
Any ideas?
You could use
<xsl:value-of select="count(preceding-sibling::record)" />
or even, generically,
<xsl:value-of select="count(preceding-sibling::*[name() = name(current())])" />
Of course this approach will not work if you process a list of nodes that is not uniform, i.e.:
<xsl:apply-templates select="here/foo|/somewhere/else/bar" />
Position information is lost in such a case, unless you store it in a variable and pass that to the called template:
<xsl:variable name="pos" select="position()" />
<xsl:for-each select="$record">
<xsl:call-template name="SomeTemplate">
<xsl:with-param name="pos" select="$pos" />
</xsl:call-template>
</xsl:for-each>
but obviously that would mean some code rewriting, which I realize you want to avoid.
Final hint: position() does not tell you the position of the node within its parent. It tells you the position of the current node relative to the list of nodes you are processing right now.
If you only process (i.e. "apply templates to" or "loop over") nodes within one parent, this happens to be the same thing. If you don't, it's not.
Final hint #2: This
<xsl:for-each select="/path/to/record">
<xsl:variable name="record" select="."/>
<xsl:for-each select="$record">
<xsl:call-template name="SomeTemplate"/>
</xsl:for-each>
</xsl:for-each>
is is equivalent to this:
<xsl:for-each select="/path/to/record">
<xsl:call-template name="SomeTemplate"/>
</xsl:for-each>
but the latter works without destroying the meaning of position(). Calling a template does not change context, so . will refer to the correct node withing the called template.
I'm trying to iterate through an xml document using xsl:foreach but I need the select=" " to be dynamic so I'm using a variable as the source. Here's what I've tried:
...
<xsl:template name="SetDataPath">
<xsl:param name="Type" />
<xsl:variable name="Path_1">/Rating/Path1/*</xsl:variable>
<xsl:variable name="Path_2">/Rating/Path2/*</xsl:variable>
<xsl:if test="$Type='1'">
<xsl:value-of select="$Path_1"/>
</xsl:if>
<xsl:if test="$Type='2'">
<xsl:value-of select="$Path_2"/>
</xsl:if>
<xsl:template>
...
<!-- Set Data Path according to Type -->
<xsl:variable name="DataPath">
<xsl:call-template name="SetDataPath">
<xsl:with-param name="Type" select="/Rating/Type" />
</xsl:call-template>
</xsl:variable>
...
<xsl:for-each select="$DataPath">
...
The foreach threw an error stating: "XslTransformException - To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function."
When I use the msxsl:node-set() function though, my results are blank.
I'm aware that I'm setting $DataPath to a string, but shouldn't the node-set() function be creating a node set from it? Am I missing something? When I don't use a variable:
<xsl:for-each select="/Rating/Path1/*">
I get the proper results.
Here's the XML data file I'm using:
<Rating>
<Type>1</Type>
<Path1>
<sarah>
<dob>1-3-86</dob>
<user>Sarah</user>
</sarah>
<joe>
<dob>11-12-85</dob>
<user>Joe</user>
</joe>
</Path1>
<Path2>
<jeff>
<dob>11-3-84</dob>
<user>Jeff</user>
</jeff>
<shawn>
<dob>3-5-81</dob>
<user>Shawn</user>
</shawn>
</Path2>
</Rating>
My question is simple, how do you run a foreach on 2 different paths?
Try this:
<xsl:for-each select="/Rating[Type='1']/Path1/*
|
/Rating[Type='2']/Path2/*">
Standard XSLT 1.0 does not support dynamic evaluation of xpaths. However, you can achieve your desired result by restructuring your solution to invoke a named template, passing the node set you want to process as a parameter:
<xsl:variable name="Type" select="/Rating/Type"/>
<xsl:choose>
<xsl:when test="$Type='1'">
<xsl:call-template name="DoStuff">
<xsl:with-param name="Input" select="/Rating/Path1/*"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$Type='2'">
<xsl:call-template name="DoStuff">
<xsl:with-param name="Input" select="/Rating/Path2/*"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
...
<xsl:template name="DoStuff">
<xsl:param name="Input"/>
<xsl:for-each select="$Input">
<!-- Do stuff with input -->
</xsl:for-each>
</xsl:template>
The node-set() function you mention can convert result tree fragments into node-sets, that's correct. But: Your XSLT does not produce a result tree fragment.
Your template SetDataPath produces a string, which is then stored into your variable $DataPath. When you do <xsl:for-each select="$DataPath">, the XSLT processor chokes on the fact that DataPath does not contain a node-set, but a string.
Your entire stylesheet seems to be revolve around the idea of dynamically selecting/evaluating XPath expressions. Drop that thought, it is neither possible nor necessary.
Show your XML input and specify the transformation your want to do and I can try to show you a way to do it.