Can i use a Select within a concat in xslt?
eg
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="concat('http://www.site.com/r&h=11', '&q=',<xsl:value-of select="Quantity" />, )" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
Try this:
<xsl:for-each select="root/OrderItems/lineitem">
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of
select="concat('http://www.site.com/r&h=11', '&q=', Quantity)" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
No, because it is not well formed XML, you cannot put a self closing XML element within a self closing XML element, or I suppose in this case you cannot use an XML element in the value of an XML attribute
Related
I have a variable #expectedLength. I need to assign it to a style attribute.
<xsl:if test="#expectedLength">
<xsl:attribute name="style">
<xsl:value-of select="'width:200px'"/>
</xsl:attribute>
</xsl:if>
I need to replace 200 with the value of #expectedLength. How can I use the variable?
You could change your snippet to
<xsl:if test="#expectedLength">
<xsl:attribute name="style">width: <xsl:value-of select="#expectedLength"/>;</xsl:attribute>
</xsl:if>
That should work with any version of XSLT.
In XSLT 2 and later you can also use the select expression
<xsl:if test="#expectedLength">
<xsl:attribute name="style" select="concat('width: ', #expectedLength, ';')"/>
</xsl:if>
I would prefer to and suggest to set up a template
<xsl:template match="#expectedLength">
<xsl:attribute name="style" select="concat('width: ', #expectedLength, ';')"/>
</xsl:template>
and then to make sure higher up that any attribute nodes are processed.
Given the XML snippet:
<rng:define name="starting_limits">
<rng:element name="limits">
<rng:ref name="Limits"/>
</rng:element>
</rng:define>
I want this:
<rng:define name="limits">
<rng:element name="limits">
<rng:ref name="Limits"/>
</rng:element>
</rng:define>
For all define elements. So I just want to remove the "starting_" prefix for all define element name attributes in the infoset.
you can use this:
<xsl:template match="#*[starts-with(., 'starting_')]">
<xsl:attribute name="{name()}" select="replace(., 'starting_', '')"/>
</xsl:template>
it matched all attribute start-with 'starting_' and remove it from output
In your XML the attribute is called name and has a value of starting_xxxx, so to match it, you would do this...
<xsl:template match="#name[starts-with(., 'starting_')]">
Or maybe this, in XSLT 2.0 and above
<xsl:template match="#name[matches(., 'starting_.*')]">
To remove the starting_attribute you can do one of the following, for example
<xsl:value-of select="substring-after(., 'starting_')"/>
<xsl:value-of select="substring(., 10)"/>
So, for example, your template may look like this
<xsl:template match="#name[starts-with(., 'starting_')]">
<xsl:attribute name="name">
<xsl:value-of select="substring(., 10)"/>
</xsl:attribute>
</xsl:template>
If you wanted to make it more generic, and match any attribute, not just one called name, you could change it to this
<xsl:template match="#*[starts-with(., 'starting_')]">
<xsl:attribute name="{name()}">
<xsl:value-of select="substring(., 10)"/>
</xsl:attribute>
</xsl:template>
I'm looking to get a custom attribute for anchor tag from xsl.
Is it possible to get the name of the attribute dynamically from xml?
Here is what I tried :
<xsl:attribute name="<xsl:value-of select="id"/>">
<xsl:value-of select="value"/>
</xsl:attribute>
Yes, it is possible. You can pass a variable as name value.
<xsl:variable name="attributeName" select="id"/>
<xsl:attribute name="{$attributeName}">
<xsl:value-of select="value"/>
</xsl:attribute>
You can simpify the solution from #Savard to
<xsl:attribute name="{id}">
<xsl:value-of select="value"/>
</xsl:attribute>
or if you are using XSLT 2.0, to
<xsl:attribute name="{id}" select="value"/>
Note, you can also mix-and-match dynamic and literal parts to a name:
<xsl:attribute name="{name}_example">Test</xsl:attribute>
I have a comma separated list coming from C#, which I am parsing in XSLT and loading it as drop down. After the user selects the option from the drop down and submits the page, If other fields are not filled in the page, I try to reload the page with the selected option for this drop down.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="parseString">
<xsl:param name="list"/>
<xsl:if test="contains($list, ',')">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:value-of select="substring-before($list, ',')"/>
<xsl:if test="substring-before($list, ',')=$carrier">
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
</xsl:if>
</xsl:element>
<xsl:call-template name="parseString">
<xsl:with-param name="list" select="substring-after($list, ',')"/>
</xsl:call-template>
</xsl:if>
But upon reload, the selected value in the drop down is not maintained.
But I can see the text - 'sel value' meeting the condition and displayed. For example in the Image you can see the text for carrier - Metro PCS.
Any Help would be appreciated.
Thanks.
EDIT: I have tried multiple ways for selected attribute like
<xsl:attribute name="SELECTED"></xsl:attribute>
<xsl:attribute name="SELECTED">True</xsl:attribute>
<xsl:attribute name="SELECTED">selected</xsl:attribute>
None of them seem to work.
Try swapping these two lines:
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
to be
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
I think you are trying to add an attribute to the ": sel value" text node which obviously won't work.
EDIT
Taking a closer look at your template I think it is an issue like suggested above (adding an attribute to a text node).
Try this:
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:if test="substring-before($list, ',')=$carrier">
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
</xsl:if>
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:element>
When your if is true it is trying to add an attribute to the text node added by the value-of. All of your attribute additions need to come before adding any child nodes, be they text or otherwise.
I have an xml with img tag
<img>
source
</img>
I want to generate:
<img src="source.jpg">
I tried something like that:
<img>
<xsl:attribute name="src">
<xsl:text>
<xsl:value-of select="node()" />.jpg
</xsl:text>
</xsl:attribute>
</img>
but it doesng work
Use:
<img src="{normalize-space()}.jpg"/>
This assumes the <img> element is the current node.
The reason why what you are doing does not work is that you cannot evaluate XSLT expressions inside of the <xsl:text> element.
<xsl:text> can only contain literal text, entity references, and #PCDATA.
If you move the <xsl:value-of> outside of the <xsl:text>, then the following will work:
<img>
<xsl:attribute name="src">
<xsl:value-of select="node()" />
<xsl:text>.jpg</xsl:text>
</xsl:attribute>
</img>
However, selecting <xsl:value-of select="node()> for the <img> in your example will include the carriage returns and whitespace characters inside of the <img> element, which is probably not what you want in your src attribute value.
That is why Dimitre Novatchev used normalize-space() in his answer. Applying that to the example above:
<img>
<xsl:attribute name="src">
<xsl:value-of select="normalize-space(node())" />
<xsl:text>.jpg</xsl:text>
</xsl:attribute>
</img>
If you get rid of the <xsl:text> as Fabiano's solution suggests, then you could also do this:
<img>
<xsl:attribute name="src"><xsl:value-of select="normalize-space(node())" />.jpg</xsl:attribute>
</img>
Just remove the tag xsl:text, in this case, you won't need it. Try this:
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat(node(), '.jpg')"/>
</xsl:attribute>
</img>
I didn't test it, but it should work. =)
<img>
<xsl:attribute name="src">
<xsl:value-of select="my_photo/#href" />
</xsl:attribute>
</img>
<my_photo href="folder/poster.jpg" />