1

我正在处理应用于 HTML 文档的 XSL 转换。相关 HTML 示例:

<div class="row home-row">
    <p>Hello</p>
</div>

我选择与以下 XSL 匹配的项目(运行良好):

<xsl:template match="//*[contains(concat(' ', normalize-space(@class), ' '), ' row ')]" priority="1">

然后,如果我之前检查过的条件匹配,我将“row”替换为“row-fluid”,如下所示:

<xsl:variable name="original-row-class" select="string(@class)" />
<xsl:variable name="row-class">
                  <xsl:call-template name="replace">
                        <xsl:with-param name="text" select="$original-row-class" />    
                        <xsl:with-param name="replace" select="' row '" />
                        <xsl:with-param name="by" select="' row-fluid '" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:copy>
                    <xsl:attribute name="class"><xsl:value-of select="$row-class" /></xsl:attribute>
                    <xsl:apply-templates select="@*[local-name() != 'class']|node()[local-name() != 'class']"/>
                </xsl:copy>

这也很有效,除了它将所有提到的“row”替换为“row-fluid”,即使它们在“home-row”内。我想做的是将“行”类更改为“行流体”,但这样做时忽略“主行”类。这可能吗?

我应该提到我被锁定在 XSLT 1.0 中。

更新:在下面添加替换模板(早该想到的!):

<xsl:template name="replace">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="replace">
          <xsl:with-param name="text" select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>

谢谢,乔纳森

4

3 回答 3

3

我认为您必须使用递归模板调用将类字符串拆分为单词。 例如。并比较/替换每个单词。

好的,这是一个基于递归方法的解决方案。

        <xsl:template match="test" >
            <xsl:variable name ="new_class">
                <xsl:call-template name="replace_words">
                    <xsl:with-param name="replace" select="'xx'"/>
                    <xsl:with-param name="by" select="'yyy'"/>
                    <xsl:with-param name="words" select="'xx xx-a xx-b xx xx'"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:value-of select="$new_class"/>

        </xsl:template>


        <xsl:template name="replace_words">
            <xsl:param name="replace"/>
            <xsl:param name="by"/>
            <xsl:param name="words"/>
            <xsl:choose>
                <xsl:when test="contains($words,' ')">
                    <!-- try replace first word-->
                    <xsl:call-template name="replace_words">
                        <xsl:with-param name="replace" select="$replace"/>
                        <xsl:with-param name="by" select="$by"/>
                        <xsl:with-param name="words" select="substring-before($words,' ')"/>
                    </xsl:call-template>
                    <!-- dlimeter -->
                    <xsl:text> </xsl:text>
                    <xsl:call-template name="replace_words">
                        <xsl:with-param name="replace" select="$replace"/>
                        <xsl:with-param name="by" select="$by"/>
                        <xsl:with-param name="words" select="substring-after($words,' ')"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:choose>
                        <xsl:when test="$replace = $words">
                            <xsl:value-of select="$by"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$words"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
于 2013-04-12T17:17:36.703 回答
1

只需对您的原始转换进行一些触摸,它现在就可以按照想要的方式工作 - 不需要扩展函数:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="*[contains(concat(' ', @class, ' '), ' row ')]">
   <xsl:variable name="original-row-class" select="string(@class)" />
   <xsl:variable name="row-class">
     <xsl:call-template name="replace">
        <xsl:with-param name="text" select=
        "concat(' ', $original-row-class, ' ')" />
        <xsl:with-param name="replace" select="' row '" />
        <xsl:with-param name="by" select="' row-fluid '" />
     </xsl:call-template>
   </xsl:variable>
   <xsl:copy>
     <xsl:attribute name="class">
      <xsl:value-of select="normalize-space($row-class)" />
     </xsl:attribute>
     <xsl:apply-templates select=
      "@*[local-name() != 'class']|node()"/>
   </xsl:copy>
 </xsl:template>

    <xsl:template name="replace">
        <xsl:param name="text" />
        <xsl:param name="replace" />
        <xsl:param name="by" />
        <xsl:choose>
          <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="replace">
              <xsl:with-param name="text" select="substring-after($text,$replace)" />
              <xsl:with-param name="replace" select="$replace" />
              <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$text" />
          </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<div class="row home-row">
    <p>Hello</p>
</div>

产生了想要的正确结果

<div class="row-fluid home-row">
    <p>Hello</p>
</div>
于 2013-04-13T05:25:37.123 回答
0

这似乎对我有用。跳过分隔线并将其全部粘贴到下面。

虽然我在发布以下答案之前就可以正常工作,但我确实尝试了那个,但它并没有立即工作。我想在这一点上我会很好奇这是否有什么严重的问题,但否则我会坚持下去。至少在下面给出了很棒的方向。

<xsl:template match="//*[contains(concat(' ', normalize-space(@class), ' '), ' row ')]" priority="1">

    <xsl:param name="separator" select="' '" />

    <xsl:variable name="class-array">
        <xsl:attribute name="name">
            <xsl:value-of select="$original-row-class"/>
        </xsl:attribute>
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="$original-row-class" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="items" select="ext:node-set($class-array)" />

    <xsl:variable name="row-class">
        <xsl:for-each select="$items/item">
          <xsl:choose>
            <xsl:when test="position() = 1">
                <xsl:choose>
                    <xsl:when test=". = 'row'">row-fluid</xsl:when>
                    <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
                </xsl:choose>    
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test=". = 'row'"><xsl:value-of select="concat($separator, 'row-fluid') "/></xsl:when>
                    <xsl:otherwise><xsl:value-of select="concat($separator, .) "/></xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
    </xsl:variable>

    <xsl:copy>
        <xsl:attribute name="class"><xsl:value-of select="$row-class" /></xsl:attribute>
        <xsl:value-of select="original-row-class" />
        <xsl:apply-templates select="@*[local-name() != 'class']|node()[local-name() != 'class']"/>
    </xsl:copy>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text" select="."/>
    <xsl:param name="sep" select="' '"/>
    <xsl:choose>
        <xsl:when test="not(contains($text, $sep))">
            <item>
                <xsl:value-of select="normalize-space($text)"/>
            </item>
        </xsl:when>
        <xsl:otherwise>
            <item>
                <xsl:value-of select="normalize-space(substring-before($text, $sep))"/>
            </item>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $sep)"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
于 2013-04-12T19:49:04.053 回答