首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Regex列表向HTML添加锚点

使用Regex列表向HTML添加锚点
EN

Stack Overflow用户
提问于 2011-10-06 23:10:14
回答 1查看 266关注 0票数 0

我正在尝试添加锚点到我的html输出。html是使用XSL2.0创建的,用于将xml转换为html。我需要能够将正则表达式的列表传递到我的样式表中,并使regex列表的每个匹配实例都成为锚点。我有适用于单个正则表达式的代码,但是当我运行正则表达式列表时,我得到相同段落的倍数。我绝对不是XSL2.0的专家。我不确定这样做是否可行。我也可以使用c#,如果这样更容易的话。如果有人认为这会是一个更好的解决方案,尽管我不确定它是不是。

适用于单个正则表达式的代码为:

代码语言:javascript
复制
<xsl:template match="text()" mode="content">
    <xsl:variable name="text">
        <xsl:value-of select="."></xsl:value-of>
    </xsl:variable>
    <!--
        IndexTerms is a parameter passed into the sheet it is a list of regex expressions seperated by semi colons
    -->
    <xsl:for-each select="tokenize($IndexTerms, ';')">
        <xsl:call-template name="IndexTerm">
            <xsl:with-param name="matchedRegex">
                <xsl:text>(.*)(</xsl:text>
                <xsl:value-of select="."></xsl:value-of>
                <xsl:text>)(.*)</xsl:text>
            </xsl:with-param>
            <xsl:with-param name="text">
                <xsl:value-of select="$text"></xsl:value-of>
            </xsl:with-param>
        </xsl:call-template>
    </xsl:for-each>
</xsl:template>

<xsl:template name="IndexTerm">
    <xsl:param name="matchedRegex">
        <xsl:text>asdf</xsl:text>
    </xsl:param>
    <xsl:param name="text"></xsl:param>
    <xsl:analyze-string select="$text" regex="{$matchedRegex}" flags="m">
        <xsl:matching-substring>
            <xsl:call-template name="IndexTerm">
                <xsl:with-param name="text">
                    <xsl:value-of select="regex-group(1)"></xsl:value-of>
                </xsl:with-param>
                <xsl:with-param name="matchedRegex">
                    <xsl:value-of select="$matchedRegex"></xsl:value-of>
                </xsl:with-param>
            </xsl:call-template>
                <xsl:element name="a">
                    <xsl:attribute name="class">
                        <xsl:text>IndexAnchor</xsl:text>
                    </xsl:attribute>
                    <xsl:value-of select="regex-group(2)"></xsl:value-of>
                </xsl:element>
                <xsl:value-of select="regex-group(3)"></xsl:value-of>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
            <xsl:value-of select="."></xsl:value-of>
        </xsl:non-matching-substring>
    </xsl:analyze-string>
</xsl:template>

示例输入:

代码语言:javascript
复制
<body>
<sec sec-type="intro">
    <title>INTRODUCTION</title>
    <p>Digital Television is the most advanced version of Television
        technology improved in the last century. Digital TV provides
        customers more choices and interactivity. New technology called
        Internet Protocol-based Television (IPTV) uses digital TV technology
        and transmits it over IP based networks (Driscol, 2008),
            (<xref ref-type="bibr" rid="r15">Moawad, 2008</xref>). IPTV is a
        technique that transmits TV and video content over a network that
        uses the IP networking protocol. With increasing the number of
        users, performance becomes more important in order to provide
        interest in video content applications and relative services. The
        requirement for new video applications on traditional broadcast
        networks (cable, terrestrial transmitters, and satellite) opens a
        new perspective for the developed use of IP networks to satisfy the
        new service demands (Driscol,
        2008</p>
    <sec>
        <title>More Introducing</title>
        <p>Internet Protocol Television, IPTV, Telco TV, or broadband TV is
            delivering high quality broadcast television and/or on-demand video
            and audio content over a broadband network. On the other hand, IPTV
            is a mechanism applied to deliver old TV channels, movies, and
            video-on-demand contents over a private network. The official
            definition approved by the International Telecommunication Union
            focus group on IPTV (ITU-T FG IPTV) is as: &#x201C;IPTV is
            defined as multimedia services such as
            television/video/audio/text/graphics /data delivered over IP based
            networks managed to provide the required level of quality of service
            and experience, security, interactivity and reliability&#x201D;
            (Driscol, 2008,
            pp.2).</p>
    </sec>
</sec>

使用Regex输入"Digital be?;Internet“的示例输出为:

代码语言:javascript
复制
<body>
<h1>INTRODUCTION</h1>
<p><a class="IndexAnchor">Digital Television</a> is the most advanced version of Television
    technology improved in the last century. Digital TV provides
    customers more choices and interactivity. New technology called
    <a class="IndexAnchor">Internet</a> Protocol-based Television (IPTV) uses digital TV technology
    and transmits it over IP based networks (Driscol, 2008),
        (Moawad, 2008). IPTV is a
    technique that transmits TV and video content over a network that
    uses the IP networking protocol. With increasing the number of
    users, performance becomes more important in order to provide
    interest in video content applications and relative services. The
    requirement for new video applications on traditional broadcast
    networks (cable, terrestrial transmitters, and satellite) opens a
    new perspective for the developed use of IP networks to satisfy the
    new service demands (Driscol,
    2008</p>
<h2>More Introducing</h2>
    <p><a class="IndexAnchor">Internet</a> Protocol Television, IPTV, Telco TV, or broadband TV is
        delivering high quality broadcast television and/or on-demand video
        and audio content over a broadband network. On the other hand, IPTV
        is a mechanism applied to deliver old TV channels, movies, and
        video-on-demand contents over a private network. The official
        definition approved by the International Telecommunication Union
        focus group on IPTV (ITU-T FG IPTV) is as: &#x201C;IPTV is
        defined as multimedia services such as
        television/video/audio/text/graphics /data delivered over IP based
        networks managed to provide the required level of quality of service
        and experience, security, interactivity and reliability&#x201D;
        (Driscol, 2008,
        pp.2).</p>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-07 01:27:49

坦率地说,我强烈建议使用正则表达式语言字符"|“来分隔备选模式,而不是用分号分隔不同的模式。然后,您可以简单地将完整的参数提供给analyze-string:

代码语言:javascript
复制
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  version="2.0"
  exclude-result-prefixes="xs">

  <xsl:param name="patterns" as="xs:string" select="'Digital Televisions?|Internet'"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@*, node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:analyze-string select="." regex="{$patterns}">
      <xsl:matching-substring>
        <a class="IndexAnchor">
          <xsl:value-of select="."/>
        </a>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="."/>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

</xsl:stylesheet>

这有帮助吗?如果你需要将分号分隔的列表转换为分隔栏,那么可以这样做,例如<xsl:param name="patterns" as="xs:string" select="string-join(tokenize($yourParam, ';'), '|')"/>

我没有使用模式,也没有看过您可能想要做的任何其他转换,但当然,如果需要,您应该能够使用我提供的模式模板。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7676412

复制
相关文章

相似问题

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