variable inside a variable in xslt coding [closed] - xslt

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to create a variable inside a variable in xslt??
Is the above thing possible???

Is this what you mean?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0">
<xsl:variable name="a">
<xsl:variable name="b" select="10"/>
<xsl:value-of select="$b"/>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$a"/>
</xsl:template>
</xsl:stylesheet>
The answer is yes, but the inner variable is in scope only within the definition of the outer variable. So if the definition of the outer variable calls for some complex expression that you'd like to store in a temporary (possibly for debugging purposes), then this is a way to do it.

The answer is: yes. This stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="vOuter">
<xsl:variable name="vInner">
<xsl:value-of select="'Content'"/>
</xsl:variable>
<xsl:value-of select="concat('Some ',$vInner)"/>
</xsl:variable>
<xsl:value-of select="$vOuter"/>
</xsl:template>
</xsl:stylesheet>
Output:
Some Content

Related

XSL selective copy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this XML file:
<bases>
<marker>
<name>Main</nane>
<city>Hartford</city>
<tag>current</tag>
</marker>
<marker>
<name>Secondary</nane>
<city>Boston</city>
<tag>none</tag>
</marker>
<marker>
<name>Tertiary</nane>
<city>Bedford</city>
</marker>
</bases>
I want to copy only those markers where the tag element matches "current". So, the output would be this:
<bases>
<marker>
<name>Main</nane>
<city>Hartford</city>
<tag>current</tag>
</marker>
</bases>
I'm an XSLT novice and I can't figure it out. Help, please.
You could do it simply like this :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Remove all marker elements where element tag value is not 'current' -->
<xsl:template match="marker[not(tag='current')]"/>
<!-- Identity template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/aiynev/2

How to group nearby same elements using XSLT version 1 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to transform an XML structure to another XML structure by grouping nearby elements using XSLT version 1 and store into a variable for later process.
My current solution:
https://xsltfiddle.liberty-development.net/naZXVF1/3
XML
<?xml version="1.0"?>
<Items>
<Parameter>1</Parameter>
<Parameter>2</Parameter>
<Menu>1</Menu>
<Parameter>3</Parameter>
<Parameter>4</Parameter>
<Menu>2</Menu>
<Menu>3</Menu>
<Parameter>5</Parameter>
<Parameter>6</Parameter>
<Parameter>7</Parameter>
<Parameter>8</Parameter>
</Items>
Expected Result
<Items>
<Parameters>
<Parameter>1</Parameter>
<Parameter>2</Parameter>
</Parameters>
<Menus>
<Menu>1</Menu>
</Menus>
...
</Items>
Either use sibling recursion or use a complicated key to identify the elements belonging together:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="group" match="*[name(preceding-sibling::*[1]) = name()]"
use="generate-id(preceding-sibling::*[name() = name(current())][name(preceding-sibling::*[1]) != name()][1])"/>
<xsl:template match="Items">
<xsl:copy>
<xsl:apply-templates select="*[name(preceding-sibling::*[1]) != name()]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Items/*">
<xsl:element name="{name()}s">
<xsl:copy-of
select=". | key('group', generate-id())"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/naZXVF1/4

XPath expression of xslt [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a question about if statemnt for xslt if statement
https://xsltfiddle.liberty-development.net/bwe3c5/38
Here is a link to mine html structure and xslt code.
I do not understand why if statemnt is called only once however i have several circumstances at which statement can be called.
P.S.
Sorry for the wrong first code. Somehow it has shared the wrong one
Your <html> element is in a default namespace xmlns="http://www.w3.org/1999/xhtml". So you have to declate it in your XSLT file and on all of the elements using it:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
<?startSampleFile ?>
<!-- xq61.xsl: converts xq60.xml into xq62.xml -->
<xsl:template match="/xhtml:html/xhtml:body/xhtml:table">
<xsl:for-each select="xhtml:tr">
<xsl:value-of select="xhtml:td" />
<xsl:if test="xhtml:td[1] != 'skip'">
<xsl:call-template name="first_column" />
</xsl:if>
</xsl:for-each>
</xsl:template>
<?endSampleFile ?>
<xsl:template name="first_column">
I am here
</xsl:template>
<!-- Copy the other nodes -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
P.S.: You changed your code after I first retrieved the XSLT and XML files. This is against the rules of SO. Include the current state of your code into the question.
It's only being called 1 time because the match='/' only matches the root element in the XML.
This template is never run
<xsl:template match="#*|node()">
<xsl:apply-templates select="*"/>
</xsl:template>

XSLT: How to imitate a flag or break statement in xslt:foreach whiling iterating a List of variable [duplicate]

This question already has an answer here:
How to break out of xsl:for-each loop in XSLT?
(1 answer)
Closed 5 years ago.
I'm new to xslt, pardon me for any mistakes.
In the xsl program I've got a list of values with variable named as "foo:vars", which contains a list of color values.
There is a variable declared as matchWith, which can contain any values (not necessarily present in foor:var list)
The Program Should output as:
if the variable matchWith contains a value which is present in the list "foo:vars" then the value should appear in the tag
<color_found> with the matching value.
else, the value present in the variable matchWith should appear in another tag named as <color_not_found>
Below is the program, that able to give the correct output for case 1, but I fail condition any flag for case 2.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:foo="http://foo.com" exclude-result-prefixes="foo">
<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
<foo:vars>
<foo:var name="a1">Yellow</foo:var>
<foo:var name="b1">red</foo:var>
<foo:var name="c1">green</foo:var>
<foo:var name="d1">blue</foo:var>
</foo:vars>
<xsl:variable name="matchWith">Yellow</xsl:variable>
<xsl:template match="/">
<xsl:for-each select="document('')/xsl:stylesheet/foo:vars/foo:var">
<xsl:variable name="temp">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$temp=$matchWith">
<color_found>
<xsl:value-of select="$matchWith"/>
</color_found>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The problem is that you are using for-each when you should just check directly whether there is a matching value.
Like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://foo.com" exclude-result-prefixes="foo">
<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
<foo:vars>
<foo:var name="a1">Yellow</foo:var>
<foo:var name="b1">red</foo:var>
<foo:var name="c1">green</foo:var>
<foo:var name="d1">blue</foo:var>
</foo:vars>
<xsl:variable name="matchWith">Yellow</xsl:variable>
<xsl:template match="/">
<xsl:variable name="options"
select="document('')/xsl:stylesheet/foo:vars/foo:var" />
<xsl:variable name="isMatch" select="$matchWith = $options" />
<xsl:element name="color_{ substring('not_', 1, 4 * not($isMatch)) }found">
<xsl:value-of select="$matchWith" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Split a string in xslt

I need to split a string "ab|bc|cd>xy|yz|zx>pq|rs|tu>
help me if any one can...
here is the code....
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" indent="yes" />
<xsl:template match="/products/product">
<xsl:variable name="ImageString" select="properties/property[#name='Brand']"></xsl:variable>
<xsl:variable name="ImageFolderName" select="substring-before($ImageString,'#')" ></xsl:variable>
Folder Name:-<xsl:value-of select="$ImageFolderName" ></xsl:value-of>
<br/>
<xsl:variable name="ImageStringName" select="substring-after($ImageString,'#')" ></xsl:variable>
Final String:-<xsl:value-of select="$ImageStringName" ></xsl:value-of>
<xsl:variable name="values">
<xsl:text><xsl:value-of select="$ImageStringName" ></xsl:value-of></xsl:text>
</xsl:variable>
<xsl:call-template name="str:split">
<xsl:with-param name="string" select="$values" />
<xsl:with-param name="pattern" select="'|'" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
To summarise the linked question below:
If you are using XSLT 2.0 then you can use tokenize(string, separator) method.
If you are using XSLT 1.0 then you'd need to write a recursive method to achieve this.
or
Use the tokenize() method if your template supports EXSLT.
See this question for full details of the options.