如何像下面这样读取元素/标记--使用xsl + xpath,我试图格式化xml输出以从样式属性读取对齐标记,但我无法理解它.(最后一招c#)
编辑:为了说明我的答案,我可能正在阅读许多html文档,我很可能会有一个允许的标记列表,我需要解析,所以并不是所有的东西都需要解析,因为我正在使用xslt将文档转换成xml,到目前为止我正在编写我的解决方案,只考虑到xsl + xpath,但是如果我要使用c#和linq (在文档上迭代)-显然,这将设置所有的样式,然后我将用其他的添加来转换我的文档。
<h1 style="text-align: right;">Another chapter</h1>我的Xsl:
<xsl:template match="h1">
<fo:block color="black" font-family="Arial, Verdana, sans-serif">
<xsl:call-template name="set-alignment"/>
</fo:block>
</xsl:template>
<xsl:template name="set-alignment">
<xsl:choose>
<xsl:when test="@align='left'">
<xsl:attribute name="text-align">start</xsl:attribute>
</xsl:when>
<xsl:when test="@align='center'">
<xsl:attribute name="text-align">center</xsl:attribute>
</xsl:when>
<xsl:when test="@align='right'">
<xsl:attribute name="text-align">end</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:template>注意,下面只定义了一种样式,但可能有很多种.(下划线等)
期望产出:
<h1 text-align="start" text-decoration="underline" >Another chapter</h1>发布于 2011-03-16 17:02:47
使用众所周知的标记化模式,这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="h1">
<xsl:copy>
<xsl:apply-templates select="@*[name()!='style']"/>
<xsl:call-template name="tokenize-style"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="tokenize-style">
<xsl:param name="pString" select="string(@style)"/>
<xsl:choose>
<xsl:when test="not($pString)"/>
<xsl:when test="contains($pString,';')">
<xsl:call-template name="tokenize-style">
<xsl:with-param name="pString"
select="substring-before($pString,';')"/>
</xsl:call-template>
<xsl:call-template name="tokenize-style">
<xsl:with-param name="pString"
select="substring-after($pString,';')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{normalize-space(
substring-before($pString,':')
)}">
<xsl:value-of select="normalize-space(
substring-after($pString,':')
)"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>输出:
<h1 text-align="right">Another chapter</h1>https://stackoverflow.com/questions/5328095
复制相似问题