开发者

Need to take recent style from html tag through xslt

开发者 https://www.devze.com 2023-02-11 21:11 出处:网络
<TD style=\"vertical-align:top;padding-left:5.4pt; padding-right:5.4pt;border-top-color:#000000;border-top-color:#5F497A;width:159.60000000000002pt;\">
<TD style="vertical-align:top;padding-left:5.4pt; padding-right:5.4pt;border-top-color:#000000;border-top-color:#5F497A;width:159.60000000000002pt;">    
    <P style="margin-bottom:0pt;">
      <SPAN style="font-weight:bold;">One</SPAN>
 开发者_运维知识库      <SPAN style="font-weight:bold;">: 3pt blue</SPAN>
   </P>
</TD>

Hi all,

This is my sample portion of input html and i am using xslt 1.0. Here, the border-top-color occurs twice.But in xslt, i have to take the recent style(border-top-color:#5F497A;). How to do it?.or any other solution(pre-processing through java).. Please help me..Thanks in advance..


More semantically correct, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="vReverse">
            <xsl:call-template name="reverse">
                <xsl:with-param name="pString"
                     select="concat(';',/TD/@style,';')"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="vAfter">
            <xsl:call-template name="reverse">
                <xsl:with-param name="pString"
                    select="substring-before($vReverse,':roloc-pot-redrob;')"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="substring-before($vAfter,';')"/>
    </xsl:template>
    <xsl:template name="reverse">
        <xsl:param name="pString"/>
        <xsl:if test="$pString">
            <xsl:call-template name="reverse">
                <xsl:with-param name="pString" select="substring($pString,2)"/>
            </xsl:call-template>
            <xsl:value-of select="substring($pString,1,1)"/>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Output:

#5F497A

An XPath 2.0 expression:

substring-after(
   tokenize(/TD/@style,';')[
      contains(.,'border-top-color')
   ][last()],
   ':'
)


You should use a combination of substring-after and substring-before, like this:

<xslt:value-of select="substring-before(substring-after(@style, 'border-top-color'), ';')"/>

This assumes the style value will be followed by a semicolon. If this will not always be the case, you can use the xslt contains function to check.

XSLT Function Reference


As far as you use xslt 1.0 try this:

<xsl:variable name="attr" select="'border-top-color'"/>
<xsl:value-of select="concat(substring-before(@style, $attr),
                             $attr,
                             substring-after(substring-after(@style, $attr), $attr))" 
/>

In case you want to fetch only color code then it can be done as follows:

<xsl:value-of select="concat('#', substring-before(substring-after(substring-after(@style, '#'), '#'), ';'))"/>
0

精彩评论

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