0

I have invalid HTML that I am trying to transform into valid HTML using an XSLT transformation. For example, I want to turn some attributes into inline CSS. Consider the following:

    <table border="1" id="t01" width="100%">    
        .
        .
        .                               
    </table>

The border and width attributes on the table element are obsolete. So I want to use inline CSS instead, like this:

    <table style="border:1;width:100%;" id="t01">   
        .
        .
        .                               
    </table>

How can I do this with XSLT?

3
  • 1
    Note: border attribute is obsolete too and there are no CSS properties called rules or cellpadding (there is border and border-collapse though). Commented Aug 17, 2015 at 21:35
  • What code have you tried? Commented Aug 18, 2015 at 8:03
  • God, I hate it when StackOverflow makes me rephrase a comment to get past its censorship rules. Who do these people think they are, to deem the comment "What have you tried?" not acceptable. I think I've earned my right to say what I like. Commented Aug 18, 2015 at 8:05

1 Answer 1

1

You could certainly make this prettier, more generic, etc., but my first pass would be something like:

<xsl:template match="table">
  <table>
    <xsl:attribute name="style">
      <xsl:if test="@border">
        <xsl:value-of select="concat('border:', @border, '; ')"/>
      </xsl:if>
      <xsl:if test="@width">
        <xsl:value-of select="concat('width:', @width, '; ')"/>
      </xsl:if>
    </xsl:attribute>
    <xsl:copy-of select="@id"/>

    <!-- either this or apply-templates -->
    <xsl:copy-of select="*"/>

  </table>
</xsl:template>
Sign up to request clarification or add additional context in comments.

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.