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.
Related
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 know the following xslt will work:
<xsl:attribute name="test">
<xsl:value-of select="substring(title, 1, 4000)"/>
</xsl:attribute>
But not sure what to do if there is something like the following and you want the substring over the whole attribute value not just the title or the substitle.
<xsl:attribute name="test">
<xsl:value-of select="title"/>
<xsl:if test="../../sub_title != ''">
<xsl:text> </xsl:text>
<xsl:value-of select="../sub_title"/>
</xsl:if>
</xsl:attribute>
Is it even possible to apply a substring function over multiple lines that define an attribute?
I think what you are saying is that you want to build up a long string, consisting of the values of a number of other elements, and then truncate the result.
What you could do, is use the concat function to build the attribute value, and then do a substring on that.
<xsl:attribute name="test">
<xsl:value-of select="substring(concat(title, ' ', ../sub_title), 1, 4000)" />
</xsl:attribute>
In this case, if sub_title was empty, you would end up with a space at the end of the test attribute, so you might want to add a normalize-space to this expression
<xsl:value-of select="normalize-space(substring(concat(title, ' ', ../sub_title), 1, 4000))" />
An alternate approach, if you did want to use a more complicated expression, is to do the string calculation in a variable first
<xsl:variable name="test">
<xsl:value-of select="title"/>
<xsl:if test="../../sub_title != ''">
<xsl:text> </xsl:text>
<xsl:value-of select="../sub_title"/>
</xsl:if>
</xsl:variable>
<xsl:attribute name="test">
<xsl:value-of select="substring($test, 1, 4000)" />
</xsl:attribute>
As an aside, you can simplify your code by using "Attribute Value Templates" here, instead of using the more verbose xsl:attribute command. Simply do this..
<myElement test="{substring($test, 1, 4000)}">
Here, the curly braces indicate an expression to be evaluated, rather than output literally.
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.
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