here is my problem:
I have HTML Document with CSS Styles. I need to move these styles into the elements in the body and remove them from style tag.
I made helper that makes xpath for each css style and value which needs to be put there. Then it generates XSL documents and applies it to the HTML.
The thing is sometimes there are multiple xsl-templates that needs to be applied to the same Node (same match). And just the first one is getting applied, others are ignored.
How i can apply them all?
Here is one example of the HTML:
<html lang="en">
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8"/>
<style type="text/css">
.right-aligned { text-align: right !important; }
.full-width { width: 100% !important; display: inline-block; }
</style>
</head>
<body>
<table class="header">
<tr>
<td class="icon">
<img alt="Minor" src="Picture_SRC" />
</td>
<td>
<div class="secondary full-width right-aligned">Blah: Test</div>
</td>
</tr>
</table>
</body>
</html>
And there is the XSL Transformations that needed to be done for this HTML:
<xsl:template match="//*[contains(@class,'right-aligned') and @style]">
<xsl:attribute name="style">
<xsl:value-of select="concat(., 'text-align: right !important;')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="//*[contains(@class,'right-aligned') and not(@style)]">
<xsl:copy>
<xsl:attribute name="style">text-align: right !important;</xsl:attribute>
<xsl:apply-templates select="@*| node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//*[contains(@class,'full-width') and @style]">
<xsl:attribute name="style">
<xsl:value-of select="concat(., 'width: 100% !important; display: inline-block;')"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="//*[contains(@class,'full-width') and not(@style)]">
<xsl:copy>
<xsl:attribute name="style">width: 100% !important; display: inline-block;</xsl:attribute>
<xsl:apply-templates select="@*| node()"/>
</xsl:copy>
</xsl:template>
Important thing: the HTML is not static and it changes ... so i can't make static XSL.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…