首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取和选择Css样式属性值

读取和选择Css样式属性值
EN

Stack Overflow用户
提问于 2011-03-16 16:04:05
回答 1查看 688关注 0票数 0

如何像下面这样读取元素/标记--使用xsl + xpath,我试图格式化xml输出以从样式属性读取对齐标记,但我无法理解它.(最后一招c#)

编辑:为了说明我的答案,我可能正在阅读许多html文档,我很可能会有一个允许的标记列表,我需要解析,所以并不是所有的东西都需要解析,因为我正在使用xslt将文档转换成xml,到目前为止我正在编写我的解决方案,只考虑到xsl + xpath,但是如果我要使用c#和linq (在文档上迭代)-显然,这将设置所有的样式,然后我将用其他的添加来转换我的文档。

代码语言:javascript
运行
复制
<h1 style="text-align: right;">Another chapter</h1>

我的Xsl:

代码语言:javascript
运行
复制
<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>

注意,下面只定义了一种样式,但可能有很多种.(下划线等)

期望产出:

代码语言:javascript
运行
复制
<h1 text-align="start" text-decoration="underline" >Another chapter</h1>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-16 17:02:47

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

代码语言:javascript
运行
复制
<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>

输出:

代码语言:javascript
运行
复制
<h1 text-align="right">Another chapter</h1>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5328095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档