开发者

Need to validate html styles

i am converting docx file into html using xslt. My resulting html contains styles like margin-top:NaN pt;, the style value NaN is ignored in bro开发者_StackOverflow社区wser by default.But i have to validate for the presence of such attributes and have to remove before viewing in browser...

Please help me...Thanks in advance...


Have you tried W3C CSS validator?

  • http://jigsaw.w3.org/css-validator/

You can use it programatically thanks to a SOAP Web service:

  • http://jigsaw.w3.org/css-validator/api.html


You should check for 'NaN' before adding inline styles.

E.g. consider this XML:

<?xml version="1.0"?>
<t>
    <Number>adsfdasf</Number>
    <Number></Number>
    <Number>100</Number>
    <Number>1.234234</Number>
</t>

Then you can:

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

    <xsl:template match="Number">
        <xsl:value-of select="concat(., ' : ')"/>
        <xsl:if test="not(string(number()) = 'NaN')">valid</xsl:if>
        <xsl:if test="string(number()) = 'NaN'">invalid</xsl:if>
        <xsl:text>&#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Result:

adsfdasf : invalid
 : invalid
100 : valid
1.234234 : valid


It is too-late to chack for NaN in the generated result.

Producing unwanted output should be prevented!

Here is an example, that avoids generating NaN:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="num[number(.) = number(.)]">
  <span style="margin-top:{.}"/>
 </xsl:template>

 <xsl:template match="num[not(number(.) = number(.))]"/>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>
    <num>helo</num>
    <num></num>
    <num>100</num>
    <num>1.234234</num>
</t>

only correct output (no NaN) is produced:

<t>
   <span style="margin-top:100"/>
   <span style="margin-top:1.234234"/>
</t>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜