2

I want to append value to some attribute in given node that matches a xsl-template

Here is my code. Can someone tell me why is's not working? :)

    <xsl:template match="//*[contains(@class,'right-aligned') and @style]">
       <xsl:variable name="currentAttributeValue" select="@style"/>
       <xsl:attribute name="style">
          <xsl:value-of select="concat($currentAttributeValue, 'text-align: right !important;')"  />
       </xsl:attribute>
    </xsl:template>

I've also tried with calling a "helper" template with parameters:

    <xsl:template match="//*[contains(@class,'full-width') and @style]">
       <xsl:variable name="currentAttributeValue" select="@style"/>
       <xsl:call-template name="concat">
          <xsl:with-param name="first" select="$currentAttributeValue"/>
          <xsl:with-param name="second">width: 100% !important; display: inline-block;</xsl:with-param>
       </xsl:call-template>
    </xsl:template>

and here is the helper:

    <xsl:template name="concat">
       <xsl:param name="first" />
       <xsl:param name="second" />
       <xsl:attribute name="style">
          <xsl:value-of select="$first" />
           <xsl:value-of select="$second" />
       </xsl:attribute>
    </xsl:template>

But this is not working either ... Any suggestions?

1
  • p.s. this is the XML input that i want to change: <div style="width: 100% !important; display: inline-block;" class="secondary full-width right-aligned">Some Text</div> Commented Aug 23, 2017 at 12:54

2 Answers 2

2

I would suggest to write a template for the attribute e.g. http://xsltransform.net/jxDigUy which does

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[contains(@class,'full-width')]/@style">
        <xsl:attribute name="style" select="concat(., 'text-align: right !important;')"/>
    </xsl:template>
</xsl:transform>

With XSLT 1.0 you don't have the select attribute for xsl:attribute and therefore need to implement that template as

    <xsl:template match="*[contains(@class,'full-width')]/@style">
        <xsl:attribute name="style">
          <xsl:value-of select="concat(., 'text-align: right !important;')"/>
        </xsl:attribute>
    </xsl:template>
Sign up to request clarification or add additional context in comments.

5 Comments

Hey Martin, thanks for the reply. I wanted to ask ... Can the problem be that I'm trying to apply more templates in one node? In this case the div tag has 3 classes and for each class i have some xsl-template ... can that be a problem?
Unless you use different modes for the different templates or you use XSLT 2.0 and xsl:next-match or you have templates imported and you use apply-imports you will not be able to apply more than one template to the same node. So yes, if you have several templates with a match attribute and you want to apply them to the same nodes you have to ensure they are all used.
Here is the total of what I'm trying to do. I have HTML document with <style> which contains a CSS. I want all that CSS to make it inline in the elements of the body. The thing is for one element there can be multiple CSS styles and I want to apply them all. So i parsed them, make xpaths to locate all the elements and values that needs to be added. For each CSS row there is a XSL template. I'm not using 2.0 XSLT so, how can i do this with different modes. I can assign mode to each template, but how will it match with the node in the XML?
I am afraid it is difficult to solve a problem that you describe in a comment. You might consider asking a new question with minimal but complete samples of XML input, XSLT code you have, output you get and output you want.
1

From here: https://www.sitepoint.com/community/t/xslt-appending-to-a-attribute/1669

The recommendation is:

<xsl:attribute name="attr">

    <!-- copy the current value of attr -->
    <xsl:value-of select="@attr" />

    <!-- add more to attr here -->
    blahblah

</xsl:attribute>

For more on the @: https://www.w3schools.com/xml/xpath_syntax.asp

@ Selects attributes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.