Using xsl:value-of in xsl:template 's - xslt

I am passing an xml file to my fo file which looks like:
<?xml version="1.0"?>
<activityExport>
<resourceKey>
<key>monthName</key>
<value>January</value>
</resourceKey>
So if I directly use:
<xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>
I can see "January" in my PDF file just fine.
However, if I use it like this in a template I have:
<xsl:template name="format-month">
<xsl:param name="date"/>
<xsl:param name="month" select="format-number(substring($date,6,2), '##')"/>
<xsl:param name="format" select="'m'"/>
<xsl:param name="month-word">
<xsl:choose>
<xsl:when test="$month = 1"><xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>
</xsl:when>
Then I do not see "January" when I call:
<xsl:variable name="monthName">
<xsl:call-template name="format-month">
<xsl:with-param name="format" select="'M'"/>
<xsl:with-param name="month" select="#monthValue"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($monthName,' ',#yearValue)"/>
I know my template works because if I have a static string in:
<xsl:choose>
<xsl:when test="$month = 1">Januaryyy</xsl:when>
Then I can see Januaryyyy fine.
So the template works, the resource exists, but the value-of-select does not work inside of call-template or xsl:choose or xsl:when test
Any help?
Regards!

Your template is probably fine, except that you are calling it from an unsuitable position in your XML. Therefore, the XPath you are using to set month-word does not find anything - it is a path to nothing.
For example, the following XSLT stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="format-month">
<xsl:param name="date"/>
<xsl:param name="month" select="format-number(substring($date,6,2), '##')"/>
<xsl:param name="format" select="'m'"/>
<xsl:param name="month-word">
<xsl:choose>
<xsl:when test="$month = 1">
<xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>
</xsl:when>
</xsl:choose>
</xsl:param>
<xsl:value-of select="$month-word"/>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="monthName">
<xsl:call-template name="format-month">
<xsl:with-param name="month" select=" '1' "/>
<xsl:with-param name="format" select="'M'"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($monthName,' ',#yearValue)"/>
</xsl:template>
</xsl:stylesheet>
applied to this XML:
<activityExport>
<resourceKey>
<key>monthName</key>
<value>January</value>
</resourceKey>
</activityExport>
produces this output:
January
Note that I have replaced the month parameter to your template with the value 1. No elements in this input XML have a #monthValue attribute (which is what lead me to believe you are calling the template from an unsuitable location) and so month-word would not get set either because of the xsl:choose.
To make things work with your real input XML, you could try replacing the XPath with "//activityExport/resourceKey[key='monthName']/value" where the double-slash defines a path to anywhere within the XML document. This should be fine if there is only one activityExport node. Otherwise, you will need to work out the suitable XPath.

Related

Reorder nodes returned from xsl:choose

What would be an efficient way to reorder a group of nodes selected using xsl:choose (XSLT 1.0).
Below is the sample source XML:
<Universe>
<CObj>
<Galaxies>
<Galaxy>
<Profile>
<Name>MilkyWay</Name>
<Age>12.5</Age>
</Profile>
<PlanetarySystem>
<Name>Solar</Name>
<Location></Location>
<Planet>
<Name>Earth</Name>
<Satellite>Y</Satellite>
...
...
...
</Planet>
...
...
...
</PlanetarySystem>
<PlanetarySystem>
...
...
...
</PlanetarySystem>
</Galaxy>
<Galaxy>
...
...
...
</Galaxy>
</Galaxies>
</CObj>
</Universe>
XSL snippet:
<xsl:template name="get-galaxy-types">
<xsl:variable name="galaxy_age1" select ="1" />
<xsl:variable name="galaxy_age2" select ="5" />
<xsl:variable name="galaxy_age3" select ="10" />
<xsl:for-each select="Galaxies/Galaxy/Profile/Age">
<xsl:choose>
<xsl:when test=".=$galaxy_age2">
<GalaxyType2>
<xsl:value-of select="../Profile/Name"/>
</GalaxyType2>
</xsl:when>
<xsl:when test=".=$galaxy_age3">
<GalaxyType3>
<xsl:value-of select="../Profile/Name"/>
</GalaxyType3>
</xsl:when>
<xsl:when test=".=$galaxy_age1">
<GalaxyType1>
<xsl:value-of select="../Profile/Name"/>
</GalaxyType1>
</xsl:when>
</xsl:choose>
</xsl:for-each>
Above XSL template is called from main template like:
<xsl:template match="Universe">
<GalaxyTypes>
<xsl:call-template name="get-galaxy-types"/>
</GalaxyTypes>
</xsl:template>
Output XML:
Note that the order of <GalaxyType> cannot be changed.
<Universe>
...
...
...
<GalaxyTypes>
<GalaxyType2>xxxxxx</GalaxyType2>
<GalaxyType3>xxxxxx</GalaxyType3>
<GalaxyType1>xxxxxx</GalaxyType1>
</GalaxyTypes>
...
...
...
</Universe>
Since xsl:choose returns the XML nodes as and when it finds a match I am unable to find a straight forward way to control the order in which I want GalaxyType to appear in the output XML.
How can I have a generic template to perform reordering for any elements that might get added in the future that may fall in to similar requirement. I am fine with having a remapping template within this XSL but I am not really sure how to accomplish this in a really elegant and efficient way.
I am going to guess you want to put the galaxies matching galaxy-age1 first, then the ones matching galaxy-age2 next, and finally the ones for galaxy-age3. I am also assuming the ages specified may not be in ascending order (that is to say, galaxy-age3 could be less that galaxy-age1.
To start with, it might be more natural to do your xsl:for-each over the Galaxy elements
<xsl:for-each select="Galaxies/Galaxy">
Then, to do your customisable sort, you could first define a variable like so...
<xsl:variable name="sortAges"
select="concat('-', $galaxy_age1, '-', $galaxy_age2, '-', $galaxy_age3, '-')" />
Note the order the parameters appear in the concat statement corresponds to the order they need to be output.
Then, your xsl:for-each could look this this...
<xsl:for-each select="Galaxies/Galaxy">
<xsl:sort select="string-length(substring-before($sortAges, concat('-', Profile/Age, '-')))" />
But this is not very elegant. It might be better to simply have a template that matches Galaxy and use three separate xsl:apply-templates to select the Galaxy; one for each age.
Try this XSLT too
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:param name="galaxy_age1" select="1" />
<xsl:param name="galaxy_age2" select="5" />
<xsl:param name="galaxy_age3" select="10" />
<xsl:template match="Universe">
<GalaxyTypes>
<xsl:apply-templates select="Galaxies/Galaxy[Profile/Age = $galaxy_age1]">
<xsl:with-param name="num" select="1" />
</xsl:apply-templates>
<xsl:apply-templates select="Galaxies/Galaxy[Profile/Age = $galaxy_age2]">
<xsl:with-param name="num" select="2" />
</xsl:apply-templates>
<xsl:apply-templates select="Galaxies/Galaxy[Profile/Age = $galaxy_age3]">
<xsl:with-param name="num" select="3" />
</xsl:apply-templates>
</GalaxyTypes>
</xsl:template>
<xsl:template match="Galaxy">
<xsl:param name="num" />
<xsl:element name="Galaxy{$num}">
<xsl:value-of select="Profile/Name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
EDIT: To make this more efficient, consider using a key to look up the Galaxy elements by their name. Try this XSLT too
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:param name="galaxy_age1" select="1" />
<xsl:param name="galaxy_age2" select="10" />
<xsl:param name="galaxy_age3" select="5" />
<xsl:key name="galaxy" match="Galaxy" use="Profile/Age" />
<xsl:template match="Universe">
<GalaxyTypes>
<xsl:apply-templates select="key('galaxy', $galaxy_age1)">
<xsl:with-param name="num" select="1" />
</xsl:apply-templates>
<xsl:apply-templates select="key('galaxy', $galaxy_age2)">
<xsl:with-param name="num" select="2" />
</xsl:apply-templates>
<xsl:apply-templates select="key('galaxy', $galaxy_age3)">
<xsl:with-param name="num" select="3" />
</xsl:apply-templates>
</GalaxyTypes>
</xsl:template>
<xsl:template match="Galaxy">
<xsl:param name="num" />
<xsl:element name="Galaxy{$num}">
<xsl:value-of select="Profile/Name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It's very difficult to navigate between the scattered snippets of your code. Still, it seems to me you should change your strategy to something like:
<xsl:template match="?">
...
<GalaxyTypes>
<xsl:apply-templates select="??/???/Galaxy">
<xsl:sort select="Profile/Age" data-type="number" order="ascending"/>
</xsl:apply-templates=>
</GalaxyTypes>
...
</xsl:template>
<xsl:template match="Galaxy">
<xsl:choose>
<xsl:when test="Profile/Age=$galaxy_age1">
<GalaxyType1>
<xsl:value-of select="Profile/Name"/>
</GalaxyType1>
</xsl:when>
<xsl:when test="Profile/Age=$galaxy_age2">
<GalaxyType2>
<xsl:value-of select="Profile/Name"/>
</GalaxyType2>
</xsl:when>
<xsl:when test="Profile/Age=$galaxy_age3">
<GalaxyType3>
<xsl:value-of select="Profile/Name"/>
</GalaxyType3>
</xsl:when>
</xsl:choose>
</xsl:template>
--
Note that your output would be much better formatted if all galaxies were a uniform <Galaxy> element, with a type attribute to tell them apart.

replacing copyright symbol and other symbols with elements in xslt

Input:
<text>
Please see the registered mark® .
Please see the copy right ©.
Please see the Trade mark™.
</text>
Output:
<text>
Please see the registered mark<registeredTrademark></registeredTrademark>.
Please see the copy right <copyright></copyright>.
Please see the Trade mark <trademark></trademark>.
</text>
I need to replace all special symbols with the elements as shown above
Can any one help.
Thanks
As this is XSLT 1.0, you are going to have to use a recursive named template to check each character in turn.
Firstly, it may be more flexible to create a sort of 'look-up' in your XSLT where you can specify a list of symbols and the required element name to replace them with
<lookup:codes>
<code symbol="®">registeredTrademark</code>
<code symbol="©">copyright</code>
<code symbol="™">trademark</code>
</lookup:codes>
(The 'lookup' namespace could actually be named anything, just as long as it is declard in the XSLT).
Then, to access this, you could define a variable to access this look-up
<xsl:variable name="lookup" select="document('')/*/lookup:codes"/>
And, to look-up an actually code based on a symbol would do something like this (where $text) is a variable that contains the text you are checking.
<xsl:variable name="char" select="substring($text, 1, 1)"/>
<xsl:variable name="code" select="$lookup/code[#symbol = $char]"/>
All the named template would do, is check the first character of the text, replacing it with an element if it exists in the lookup, and then recursively call the template with the remaining part of the text.
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lookup="lookup" exclude-result-prefixes="lookup">
<xsl:output method="xml" indent="no"/>
<lookup:codes>
<code symbol="®">registeredTrademark</code>
<code symbol="©">copyright</code>
<code symbol="™">trademark</code>
</lookup:codes>
<xsl:variable name="lookup" select="document('')/*/lookup:codes"/>
<xsl:template match="text[text()]">
<text>
<xsl:call-template name="text"/>
</text>
</xsl:template>
<xsl:template name="text">
<xsl:param name="text" select="text()"/>
<xsl:variable name="char" select="substring($text, 1, 1)"/>
<xsl:variable name="code" select="$lookup/code[#symbol = $char]"/>
<xsl:choose>
<xsl:when test="$code"><xsl:element name="{$code}" /></xsl:when>
<xsl:otherwise>
<xsl:value-of select="$char"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($text) > 1">
<xsl:call-template name="text">
<xsl:with-param name="text" select="substring($text, 2, string-length($text) - 1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is output
<text>
Please see the registered mark<registeredTrademark /> .
Please see the copy right <copyright />.
Please see the Trade mark<trademark />.
</text>
This transformation is more efficient by avoiding char-by-char recursion and using "biggest-possible-step" recursion:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<my:reps>
<r char="®">registeredTrademark</r>
<r char="©">copyright</r>
<r char="™">trademark</r>
</my:reps>
<xsl:variable name="vReps" select="document('')/*/my:reps/*"/>
<xsl:template match="text()" name="multReplace">
<xsl:param name="pText" select="."/>
<xsl:param name="pReps" select="$vReps"/>
<xsl:if test="$pText">
<xsl:variable name="vTarget" select="$pReps[1]/#char"/>
<xsl:choose>
<xsl:when test="not($vTarget)">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vReplacement" select="$pReps[1]"/>
<xsl:call-template name="multReplace">
<xsl:with-param name="pText" select=
"substring-before(concat($pText, $vTarget), $vTarget)"/>
<xsl:with-param name="pReps" select="$pReps[position() >1]"/>
</xsl:call-template>
<xsl:if test="contains($pText, $vTarget)">
<xsl:element name="{$vReplacement}"/>
<xsl:call-template name="multReplace">
<xsl:with-param name="pText" select="substring-after($pText, $vTarget)"/>
<xsl:with-param name="pReps" select="$pReps"/>
</xsl:call-template>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied to the provided XML document:
<text>
Please see the registered mark® .
Please see the copy right ©.
Please see the Trade mark™.
</text>
the correctly-replaced text is produced:
Please see the registered mark<registeredTrademark/> .
Please see the copy right <copyright/>.
Please see the Trade mark<trademark/>.

Using a Map in XSL for expanding abbreviations

I saw a similar question on creating a Map.
That answer has this code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<xsl:variable name="map">
<map>
<entry key="key-1">value1</entry>
<entry key="key-2">value2</entry>
<entry key="key-3">value3</entry>
</map>
</xsl:variable>
<output>
<xsl:value-of select="msxsl:node-set($map)/map/entry[#key='key-1']"/>
</output>
</xsl:template>
I would like to replace the output command to use a value in my XML to see if it is a key in the map and then replace it with the value.
Is the best way to do a for-each select on the map and compare with contains?
Here is a snippet of the XML:
<document>
<content name="PART_DESC_SHORT" type="text" vse-streams="2" u="22" action="cluster" weight="1">
SCREW - ADJUST
</content>
</document>
The content node value may have a string containing an abbreviation that I want to replace with the full value.
Thanks,
Paul
No need to use a for-each - this XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output indent="yes" method="xml" />
<xsl:variable name="abbreviations">
<abbreviation key="Brkt Pivot R">Bracket Pivot R</abbreviation>
<abbreviation key="Foo">Expanded Foo</abbreviation>
<abbreviation key="Bar">Expanded Bar</abbreviation>
</xsl:variable>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="text" select="."/>
<xsl:variable name="abbreviation" select="msxsl:node-set($abbreviations)/*[#key=$text]"/>
<xsl:choose>
<xsl:when test="$abbreviation">
<xsl:value-of select="$abbreviation"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
will convert any XML in an exact copy expanding all the text matching an abbreviation defined in the abbreviations variable at the top.
If you want to expand the abbreviations only within specific elements you can modify the second template match="..." rule.
On the other hand if you want to expand ANY occurance of all the abbreviations within the text you need loops - that means recursion in XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output indent="yes" method="xml" />
<xsl:variable name="abbreviations">
<abbreviation key="Brkt">Bracket</abbreviation>
<abbreviation key="As">Assembly</abbreviation>
<abbreviation key="Foo">Expanded Foo</abbreviation>
<abbreviation key="Bar">Expanded Bar</abbreviation>
</xsl:variable>
<!-- Replaces all occurrences of a string with another within a text -->
<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($text,$from)">
<xsl:value-of select="concat(substring-before($text,$from),$to)"/>
<xsl:call-template name="replace">
<xsl:with-param name="text" select="substring-after($text,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Replace all occurences of a list of abbreviation with their expanded version -->
<xsl:template name="replaceAbbreviations">
<xsl:param name="text"/>
<xsl:param name="abbreviations"/>
<xsl:choose>
<xsl:when test="count($abbreviations)>0">
<xsl:call-template name="replaceAbbreviations">
<xsl:with-param name="text">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="from" select="$abbreviations[1]/#key"/>
<xsl:with-param name="to" select="$abbreviations[1]"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="abbreviations" select="$abbreviations[position()>1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:call-template name="replaceAbbreviations">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="abbreviations" select="msxsl:node-set($abbreviations)/*"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Applying this second XSLT to
<document>
<content name="PART_DESC_SHORT" type="text" vse-streams="2" u="22" action="cluster" weight="1">
Brkt Pivot R
</content>
</document>
produces
<document>
<content name="PART_DESC_SHORT" type="text" vse-streams="2" u="22" action="cluster" weight="1">
Bracket Pivot R
</content>
</document>
Note that:
this solution assumes that no abbreviation ovelap (e.g. two separate abbreviations Brk and Brkt)
it uses XSLT 1.0 - a better solution is probably possible with XSLT 2.0
this kind of heavy string processing is likely quite inefficient in XSLT, it is probably better to write an extension function in some other language and call it from the XSLT.

xsl: Can a named template return a node list?

I am using a recursive template that searches for some specific elements like this:
<xsl:template name="GetProdDependency">
<xsl:param name="TechProd"></xsl:param>
<xsl:param name="BeatenPath"></xsl:param>
<xsl:variable name="TechProdArch" select="$TechProd/pro:own_slot_value[pro:slot_reference='technology_product_architecture']/pro:value"></xsl:variable>
<xsl:variable name="TechProdArchNode" select="/node()/pro:simple_instance[pro:name=$TechProdArch]"></xsl:variable>
<xsl:variable name="TechProdCompList" select="$TechProdArchNode/pro:own_slot_value[pro:slot_reference='contained_techProd_components']/pro:value"/>
<xsl:for-each select="$TechProdCompList">
<xsl:variable name="TechProdAsRole" select="/node()/pro:simple_instance[pro:name=current()]/pro:own_slot_value[pro:slot_reference='technology_product_as_role']/pro:value"/>
<xsl:variable name="TechProdRole" select="/node()/pro:simple_instance[pro:name=$TechProdAsRole]/pro:own_slot_value[pro:slot_reference='role_for_technology_provider']/pro:value"/>
<xsl:variable name="DepTechProd" select="/node()/pro:simple_instance[pro:name=$TechProdRole]"/>
<!-- Check for beaten Path -->
<!-- if $DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value in BeatenPath -->
<xsl:if test="not($BeatenPath[string(.)=string($DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value)])">
<!-- Do the recursion here! -->
<!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/> (type: <xsl:value-of select="$DepTechProd/pro:type"/> and Class: <xsl:value-of select="$DepTechProd/pro:name"/>)-->
<!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/>-->
<xsl:value-of select="$DepTechProd"/>
<xsl:call-template name="GetProdDependency">
<xsl:with-param name="TechProd" select="$DepTechProd"></xsl:with-param>
<xsl:with-param name="BeatenPath" select="$TechProd|$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
<xsl:with-param name="Rev" select="$Rev + 1"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:template>
This is working fine in the searching etc.
But when I get the result in the original caller, I was expecting to get a list of nodes from the calling.
I call it like:
<xsl:variable name="DelPlist">
<xsl:call-template name="GetProdDependency">
<xsl:with-param name="TechProd" select="$TechProd"></xsl:with-param>
<xsl:with-param name="BeatenPath" select="$TechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
<xsl:with-param name="Rev" select="1"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
And I was expecting to get a list of nodes that I can iterate through with <xsl:for-each>. But If I check for the count($DelPlist), I get 1 as result and I can not iterate.
Can somebody help?
The answer to your question is: in XSLT 2.0 yes, in XSLT 1.0 no.
In XSLT 2.0 both templates and functions can return any value. The type of the result can be specified using the as attribute (for example as="node()*"), and you can use the xsl:sequence instruction to set the result to be the result of any XPath expression.
In XSLT 1.0, if you capture the result of xsl:call-template in a variable, the value of the variable will always be a result tree fragment.
You must specify the type of the result of the template in its as attribute.
If unspecified, the type is document-node() and then in order to iterate through the result, you need to get the children of the result.
Solution: Specify the return type of the template with an as attribute.
Here is a complete example:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vNodes" as="element()*">
<xsl:call-template name="genNodes"/>
</xsl:variable>
<xsl:for-each select="$vNodes">
<xsl:value-of select="concat('
', position(), ': ')"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template name="genNodes" as="element()*">
<a/>
<b/>
<c/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on any XML document (not used), the wanted, correct result is produced:
1: <a/>
2: <b/>
3: <c/>
Sorry to bother.
I could achieve what meets my requirement in the following amnner:
<xsl:template name="GetProdDependency">
<xsl:param name="TechProd"></xsl:param>
<xsl:param name="BeatenPath"></xsl:param>
<xsl:variable name="TechProdArch" select="$TechProd/pro:own_slot_value[pro:slot_reference='technology_product_architecture']/pro:value"></xsl:variable>
<xsl:variable name="TechProdArchNode" select="/node()/pro:simple_instance[pro:name=$TechProdArch]"></xsl:variable>
<xsl:variable name="TechProdCompList" select="$TechProdArchNode/pro:own_slot_value[pro:slot_reference='contained_techProd_components']/pro:value"/>
<xsl:for-each select="$TechProdCompList">
<xsl:variable name="TechProdAsRole" select="/node()/pro:simple_instance[pro:name=current()]/pro:own_slot_value[pro:slot_reference='technology_product_as_role']/pro:value"/>
<xsl:variable name="TechProdRole" select="/node()/pro:simple_instance[pro:name=$TechProdAsRole]/pro:own_slot_value[pro:slot_reference='role_for_technology_provider']/pro:value"/>
<xsl:variable name="DepTechProd" select="/node()/pro:simple_instance[pro:name=$TechProdRole]"/>
<!-- Check for beaten Path -->
<!-- if $DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value in BeatenPath -->
<xsl:if test="not($BeatenPath[string(.)=string($DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value)])">
<!-- Do the recursion here! -->
<!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/> (type: <xsl:value-of select="$DepTechProd/pro:type"/> and Class: <xsl:value-of select="$DepTechProd/pro:name"/>)-->
<!--<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"/>-->
<!--<xsl:value-of select="$DepTechProd"/>-->
<xsl:element name="TProd">
<xsl:value-of select="$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value" />
</xsl:element>
<xsl:call-template name="GetProdDependency">
<xsl:with-param name="TechProd" select="$DepTechProd"></xsl:with-param>
<xsl:with-param name="BeatenPath" select="$TechProd|$DepTechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
<xsl:with-param name="Rev" select="$Rev + 1"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:for-each>
</xsl:template>
And could iterate through the nodelist as:
<xsl:variable name="DelPlist">
<xsl:call-template name="GetProdDependency">
<xsl:with-param name="TechProd" select="$TechProd"></xsl:with-param>
<xsl:with-param name="BeatenPath" select="$TechProd/pro:own_slot_value[pro:slot_reference='name']/pro:value"></xsl:with-param>
<xsl:with-param name="Rev" select="1"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="$DelPlist/node()">
<xsl:value-of select="current()" />
</xsl:for-each>
It meets my current requirement.
Possible in xsl 1.0 with exslt:node-set (supported by most xslt processors):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="exslt msxml"
extension-element-prefixes="exslt msxml"
version="1.0">
<!-- support for exslt prefix for msxml, ignored by other processors which however support exsl natively anyway -->
<msxml:script language="JScript" implements-prefix="exslt">this['node-set']=function(x){return x}</msxml:script>
<xsl:template match="/">
<xsl:variable name="elementsReturnedFromCallTemplate">
<xsl:call-template name="getElements"/>
</xsl:variable>
<xsl:for-each select="exslt:node-set($elementsReturnedFromCallTemplate)/*">
<xsl:copy/>
</xsl:for-each>
</xsl:template>
<xsl:template name="getElements">
<a/>
<b/>
</xsl:template>
</xsl:stylesheet>
Output (tested in libxslt, xalan, saxon, msxml):
<a/>
<b/>

xslt convert xml string in xml elements

here's a tricky one.
I have the following XML
<test>
<testElement SomeAttribute="<otherxml><otherElement>test test</otherElement></otherxml>">
</testElement>
</test>
Using XSLT, I want to transform this XML to have the following result.
<test>
<testElement>
<SomeAttributeTransformedToElement>
<otherxml>
<otherElement>test test</otherElement>
</otherxml>
</SomeAttributeTransformedToElement>
</testElement>
</test>
Basically, some text in an attribute must be transformed to actual elements in the final XML
Any ideas how to achieve that in XSLT?
Alex
You can achieve that by disabling output escaping. However, note that your input document is not a valid XML document (< is illegal in attribute values and needs escaping). I therefore changed your input document as follows:
Input document
<?xml version="1.0" encoding="utf-8"?>
<test>
<testElement SomeAttribute="<otherxml><otherElement>test test</otherElement></otherxml>">
</testElement>
</test>
XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#SomeAttribute">
<SomeAttributeTransformedToElement>
<xsl:value-of select="." disable-output-escaping="yes"/>
</SomeAttributeTransformedToElement>
</xsl:template>
</xsl:stylesheet>
Be aware that with disable-output-escaping="yes" there is no longer a guarantee that the produced output document is a well-formed XML document.
I had the same need, and I finally managed to build something working.
However, it's far from perfect. But as this solution works for me (and is very useful in my case), I give it them to share with anyone who could be interrested too.
I hope that XSL-purists will forgive me for this :
This XSLT named-template takes a text variable as input, and generates XML elements on the output.
Warning : There are some limitations :
The XML must not have self-contained elements (<a><b><a>...</a></b></a> is forbidden, as the 'a' element contains another 'a')
The XML must be normalized (a single space between the element name and the attributes, so <elmt attr1="1" attr2="2"> is allowed, but not <elmt attr1="1" attr2="2"> (this can be fixed)
&ltelement /> style is not handled yet (sorry, I didn't need that, and I'm kinda busy on y current project), but this can easily be fixed.
So you've been warned : don't put this in production before having tested it and being sure that it's illegible to your case. Also, if you happen to fix or improve this script, please let me know.
Here are the templates :
<xsl:template name="t-convert">
<xsl:param name="TEXT"/>
<xsl:choose>
<xsl:when test="starts-with($TEXT,'<?')">
<xsl:call-template name="t-convert">
<xsl:with-param name="TEXT" select="substring-after($TEXT,'?>')"/>
</xsl:call-template>
</xsl:when>
<!-- Si le texte contient encore des elements -->
<xsl:when test="contains($TEXT,'<')">
<xsl:variable name="before-first-open" select="substring-before($TEXT,'<')"/>
<xsl:variable name="after-first-open" select="substring-after($TEXT,'<')"/>
<!-- On ecrit le texte qui précéde l'élément -->
<xsl:value-of select="$before-first-open"/>
<!-- Le nom de l'élément -->
<xsl:variable name="TRAD" select="translate($after-first-open,'>',' ')"/>
<!-- TODO : Gere le cas <ELEMENT /> -->
<xsl:variable name="ELEMENT" select="substring-before($TRAD,' ')"/>
<xsl:variable name="suite" select="substring-after($after-first-open,$ELEMENT)"/>
<xsl:variable name="DEFINITION" select="substring-before($suite,'>')"/>
<xsl:variable name="CONTENT" select="substring-after(substring-before($suite,concat('</',$ELEMENT)),concat($DEFINITION,'>'))"/>
<xsl:variable name="FOLLOWING">
<xsl:choose>
<xsl:when test="substring($DEFINITION,string-length($DEFINITION))='/'"><!-- ends-with($DEFINITION,'/') not compatible with all XSLT version -->
<xsl:value-of select="substring-after($suite,'/>')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after(substring-after($suite,concat('</',$ELEMENT)),'>')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$ELEMENT}">
<xsl:call-template name="t-attribs">
<xsl:with-param name="TEXT" select="$DEFINITION"/>
</xsl:call-template>
<xsl:call-template name="t-convert">
<xsl:with-param name="TEXT" select="$CONTENT"/>
</xsl:call-template>
</xsl:element>
<xsl:call-template name="t-convert">
<xsl:with-param name="TEXT" select="$FOLLOWING"/>
</xsl:call-template>
</xsl:when>
<!-- no more element -->
<xsl:otherwise>
<xsl:value-of select="$TEXT"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="t-attribs">
<xsl:param name="TEXT"/>
<xsl:if test="contains($TEXT,'=')">
<!-- Assert TEXT=' NAME="VALUE".*' -->
<xsl:variable name="NAME" select="substring-after(substring-before($TEXT,'='),' ')"/>
<xsl:variable name="afterName" select="substring-after($TEXT,'="')"/>
<xsl:variable name="VALUE" select="substring-before($afterName,'"')"/>
<xsl:variable name="FOLLOWING" select="substring-after($afterName,'"')"/>
<xsl:attribute name="{$NAME}">
<xsl:value-of select="$VALUE"/>
</xsl:attribute>
<xsl:call-template name="t-attribs">
<xsl:with-param name="TEXT" select="FOLLOWING"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
And it's called with :
<xsl:call-template name="t-convert">
<xsl:with-param name="TEXT" select="//content"/>
</xsl:call-template>
I hope this will be helful to at least one perso in the world (it was for me !)