【问题标题】:Read and selecting Css Style attribute values读取并选择 Css Style 属性值
【发布时间】: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样式属性,例如&lt;xsl:for-each select="tokenize(@style, ';')"&gt;&lt;xsl:attribute name="{normalize-space(substring-before(., ':'))}" select="normalize-space(substring-after(., ':'))"/&gt;&lt;/xsl:for-each&gt;.
  • 考虑到预算限制的任何事情...... :) 如果某个应用程序不是 o/s,它们看起来需要商业许可证

标签: c# linq xslt .net-3.5 xpath


【解决方案1】:

使用众所周知的标记化模式,此样式表:

<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>

【讨论】:

猜你喜欢
  • 2020-01-02
  • 2015-09-21
  • 2010-12-30
  • 2019-09-08
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
相关资源
最近更新 更多