Create new element with fixed value and dynamic attribute using xslt - xslt

if i'm trying to understand xslt where i'm stuck at a place where i want to add an element with a fixed value and a dynamic attribute
input:
<newsItem parentGUID="fakeGuid">
</newsItem>
desired output:
<newsItem>
<parent Key="fakeGuid">News</parent>
<newsItem>
Current Xslt (value isn't in the actual output)
<xsl:template match="NewsItem">
<xsl:element name="Parent">
<xsl:attribute name="Key">
<xsl:value-of select="#parentGUID"/>
</xsl:attribute>
<xsl:value-of select="News"/>
</xsl:element>
</xsl:template>
Can someone point me out what i'm doing wrong?
Kind regards

The instruction:
<xsl:value-of select="News"/>
looks for a child element named News to extract its string-value. To output the literal text "News", try:
<xsl:text>News</xsl:text>
Note also that you don't need to use xsl:element to create a literal result element. To get the result you show, you could do:
<xsl:template match="newsItem">
<xsl:copy>
<Parent key="{#parentGUID}">News</Parent>
</xsl:copy>
</xsl:template>
Read about attribute value templates to understand how this works.

Related

My XSLT Variable doesn't load in template

So I'm trying to create a variable and then later on use this in my template.
This is the code I have now:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="items">
<xsl:call-template name="item" />
</xsl:template>
<xsl:template name="item">
<xsl:for-each select="item">
<xsl:variable name="currentItemTheme">
test variable
</xsl:variable>
<xsl:if test="title = name">
Showvariable: {$currentItemTheme} <br/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have no idea why it doesn't work. My file does output the "Showvariable: ", but it just doesn't show the test values. So the for-each loops and template aren't the problem.
Is there something I'm missing? Does someone know how I get this to work?
I think you want <xsl:value-of select="$currentItemTheme"/> instead of {$currentItemTheme}, unless you are mixing the XSLT code with some other language that provides the {$currentItemTheme} syntax.
<xsl:copy-of select="$currentItemTheme"/> is another option if you build nodes in your variable and want to output them instead of just the string value as a text node.

XSLT - How to refer to a current node value using xsl:choose?

I try to create a variable, which I can use in a later template:
<xsl:variable name="fc">
<xsl:choose>
<xsl:when test="self::node()='element1'">gray</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Unfortunately it does not work.
<xsl:template match="element1">
<h1><font color="{$fc}"><xsl:value-of select="self::node()"/></font></h1>
</xsl:template>
What am I doing wrong?
Here is the extensive code:
XML:
<root
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com scheme.xsd" xmlns="http://www.test.com" xmlns:tst="http://www.test.com">
<elementA>
<elementB tst:name="name">
<elementC tst:name="name">
<element1> Test1 </element1>
<element2> Test2 </element2>
</elementC >
</elementB>
</elementA>
</root>
All the elements are qualified and part of the namespace "http://www.test.com".
XSLT:
<xsl:template match="/">
<html>
<body><xsl:apply-templates select="tst:root/tst:elementA/tst:elementB/tst:elementC/tst:element1"/>
</body>
</html>
</xsl:template>
<xsl:variable name="var_fc">
<xsl:choose>
<xsl:when test="local-name(.)='tst:element1'">gray</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="tst:element1">
<h2><font color="{$var_fc}"><xsl:value-of select="self::node()"/></font></h2>
</xsl:template>
As a result, element1 should turn gray, but it always turn red.
You can't use a variable for this, as the content of an xsl:variable is evaluated just once at definition time, whereas you want to evaluate some logic every time the variable is referenced, in the current context at the point of reference.
Instead you need a template, either a named one:
<xsl:template name="fc">
<xsl:choose>
<xsl:when test="local-name()='element1'">gray</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:template>
or (better) a pair of matching templates with a mode, to let the template matcher do the work:
<!-- match any node whose local name is "element1" -->
<xsl:template mode="fc" match="node()[local-name() = 'element1']">gray</xsl:template>
<!-- match any other node -->
<xsl:template mode="fc" match="node()">red</xsl:template>
When you want to use this logic:
<h1>
<font>
<xsl:attribute name="color">
<xsl:apply-templates select="." mode="fc" />
</xsl:attribute>
Seeing as you have the tst prefix mapped in your stylesheet you could check the name directly instead of using the local-name() predicate:
<xsl:template mode="fc" match="tst:element1">gray</xsl:template>
<xsl:template mode="fc" match="node()">red</xsl:template>
XSLT variables are designed not to be changeable. Actually they could be named constants. If your variable fc is created global, it will use the root element for choose. You have to use choose in the actual template to be tested against the current element. If you want to have "red" and "gray" defined only once, create two variables with just that text content and use these instead the plain text in the choose.
Maybe it is a typo:
<xsl:when test=self::node()='element1'">gray</xsl:when>
should be:
<xsl:when test="self::node()='element1'">gray</xsl:when>
there is a missing quote.
I think instead of test="self::node()='element1'" you want test="self::element1" or test="local-name(.) = 'element1'".
A couple of other errors in your code:
(1) self::node() = 'element1'
tests whether the content of the element is "element1", not whether its name is "element1"
(2) local-name(.)='tst:element1'
will never be true because the local name of a node never contains a colon.
Experienced users would often write this code using template rules:
<xsl:template mode="var_fc" match="tst:element1">gray</xsl:template>
<xsl:template mode="var_fc" match="*">red</xsl:template>
and then
<xsl:apply-templates select="." mode="var_fc"/>

copying node and replace value in XSLT

I want to copy and replace a value in xlst but i get a blank value
<xsl:template match="/Tests/test/comment">
<xsl:copy>
<xsl:value-of select="NEW"/>
</xsl:copy>
</xsl:template>
I get
<comment></comment>
i want
<comment>NEW</comment>
value-of takes a value of a node named NEW in the current context. I don't think this is that you meant to do.
Try
<xsl:template match="/Tests/test/comment">
<xsl:copy>NEW</xsl:copy>
</xsl:template>
Put your word NEW in single quotes like this:
<xsl:value-of select="'NEW'"/>

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>

Efficient code for replacing a text node with some other text using XSLT?

I have an XML where a text node with value "null tag" appears (randomly) at the different places in the file.
My question is how to replace the text with some other text, where the element (and parent node) name is unknown. I have already created an XSLT file which looks a bit bulky and I am not sure about its efficiency in transformation time.
This is the sample test XML I have created:
<root>
<sub_root>abc</sub_root>
<sub_root>
<child>test value</child>
<child2>test value</child2>
<sub_child>
<node1>data</node1>
<node2>data2</node2>
<node3>
<grand_child>test value</grand_child>
</node3>
<node4>test value</node4>
</sub_child>
</sub_root>
</root>
This is the XSLT :
<xsl:template match="#*|*|text()">
<xsl:copy>
<xsl:choose>
<xsl:when test="text()='test value'">
<xsl:apply-templates select="#*|*"/>
<xsl:text>replaced</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="#*|*|text()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
This is the desired output, I am trying to generate:
<root>
<sub_root>abc</sub_root>
<sub_root>
<child>replaced</child>
<child2>replaced</child2>
<sub_child>
<node1>data</node1>
<node2>data2</node2>
<node3>
<grand_child>replaced</grand_child>
</node3>
<node4>replaced</node4>
</sub_child>
</sub_root>
</root>
can this code be written in yet more better way(in any terms)? Or is that my code is better enough?
Just add this to the identity transform:
<xsl:template match="text()[. = 'test value']">
<xsl:text>replaced</xsl:text>
</xsl:template>
The result will copy every node in the input, unmodified, to the output - except for text nodes whose value is test value, which will be transformed into text nodes with the value replaced.