【发布时间】:2011-03-16 16:04:05
【问题描述】:
如何读取如下元素/标签 - 使用 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>
【问题讨论】:
-
使用 .NET 3.5,您可以考虑迁移到 XSLT 2.0(您可以在 Saxon 9 saxon.sourceforge.net、XQSharp xqsharp.com 或 AltovaXML Tools altova.com/altovaxml.html 之间进行选择),然后您可以处理 CSS样式属性,例如
<xsl:for-each select="tokenize(@style, ';')"><xsl:attribute name="{normalize-space(substring-before(., ':'))}" select="normalize-space(substring-after(., ':'))"/></xsl:for-each>. -
考虑到预算限制的任何事情...... :) 如果某个应用程序不是 o/s,它们看起来需要商业许可证
标签: c# linq xslt .net-3.5 xpath