1

I'm writing an xsl script in which I need to create the attribute dynamically.

Here is my sample code.

 <Table BORDERLINESTYLE="solid" BOTTOMMARGIN="12" NOTEGROUPSPACING="single" CELLPADDING="0" CELLSPACING="0" WIDTH="504"></Table>

Here BORDERLINESTYLE may or may not be available to all Table tags. and I want the output to be something like this.

<table style="border-style:solid; border-margin:12px; width:504px"></table>

Here are 2 things that I've tried.

1. Creating attributes

 <table>
    <xsl:if test="./@BORDERLINESTYLE">
        <xsl:attribute name="border" select="'1px solid'"/>
    </xsl:if>
    
     <xsl:if test="./@WIDTH">
        <xsl:attribute name="width" select="concat(./@WIDTH, 'px')"/>
    </xsl:if>
    
     <xsl:if test="./@BOTTOMMARGIN">
        <xsl:attribute name="border" select="'1px solid'"/>
    </xsl:if>
   <xsl:apply-templates/>
 </table>

the output that I get is as below.

<table border="1px solid" width="504px">

2. Inline adding

<table style="width:{current()/@WIDTH,'px'}; border:{./@BORDERLINESTYLE}; margin-bottom: {./@BOTTOMMARGIN, 'px'}; margin-top:{./@TOPMARGIN, 'px'}; "></table>

and the output is as below with some blank values like margin-top

<table style="width:504 px; border:solid; margin-bottom: 12 px; margin-top:px; ">

How can I add styles to style based on the attributes provided in the XML?

I'm using XSLT2.0.

0

3 Answers 3

1

I would use something like this:

  <xsl:if test="@BORDERLINESTYLE|@WIDTH|@BOTTOMMARGIN|@TOPMARGIN">
    <xsl:attribute name="style">
      <xsl:if test="@BORDERLINESTYLE">
        <xsl:text>border:1px solid;</xsl:text>
      </xsl:if>
      <xsl:if test="@WIDTH">
        <xsl:value-of select="concat('width:',@WIDTH, 'px;')"/>
      </xsl:if>
      <xsl:if test="@BOTTOMMARGIN">
        <xsl:value-of select="concat('margin-bottom:',@BOTTOMMARGIN, 'px;')"/>
      </xsl:if>
      <xsl:if test="@TOPMARGIN">
        <xsl:value-of select="concat('margin-top:',@TOPMARGIN, 'px;')"/>
      </xsl:if>
    </xsl:attribute>
  </xsl:if>

And depending on the business rules for mapping those source-attributes to style attributes change the code accordingly.

Btw.: ./@ is the same as @

Sign up to request clarification or add additional context in comments.

Comments

0

Use if else Function

For Example

margin-top: {if(//@TOPMARGIN!=0) then {./@TOPMARGIN, 'px'} else '12px'}

Comments

0

I need to create the attribute dynamically.

Actually, you want to populate the attribute dynamically - which could be done by applying a template to each input attribute you want to use:

<xsl:template match="Table">
    <table>
        <xsl:attribute name="style">
            <xsl:apply-templates select="@BORDERLINESTYLE | @BOTTOMMARGIN | @WIDTH"/>
        </xsl:attribute>
    </table>
</xsl:template>

<xsl:template match="@BORDERLINESTYLE">
    <xsl:text>border-style:</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>; </xsl:text>
</xsl:template>

<xsl:template match="@BOTTOMMARGIN">
    <xsl:text>border-margin:</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>px; </xsl:text>
</xsl:template>

<xsl:template match="@WIDTH">
    <xsl:text>width:</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>; </xsl:text>
</xsl:template>

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.