如何使用XSLT将链接转换为小写

时间:2014-05-18 21:15:33

标签: html xml xslt

在我的XML工作流程中,我将其设置为构建指向我们应用程序中定义的特定术语的超链接。我想将link属性转换为小写以标准化命名约定。

XSLT已经通过将多个单词与“_”下划线连接来创建链接。但是我怎样才能同时将大写单词转换为小写?

我已经设置了一个用于转换单词的变量,但是如何将其添加到现有模板?

以下是一些示例XML:

<APPENDIX>
    <Subsection>
        <DL>
            <DT>Committee</DT> 
            <DD>The <em>Committee</em> is the appropriate <em>Committee</em> of the <em>Governing Body</em></DD>
            <DT>Golf Skill or Reputation</DT> 
            <DD><text>It is a matter for the <em>Governing Body</em> to decide whether a particular <em>amateur golfer</em> has <em>golf skill or reputation</em>. </text></DD>
            <DT>Governing Body</DT>  
            <DD><text>The <em>Governing Body</em> for the administration of the Rules of Amateur Status in any country is the national golf union or association of that country.  </text></DD>
        </DL>
    </Subsection>
</APPENDIX>

以下是将文本转换为链接的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">

        <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
        <xsl:variable name="uppercase" select="''" />

        <xsl:apply-templates select="APPENDIX"/>
    </xsl:template>

    <xsl:template match="em">
        <a href="{concat('#',translate(normalize-space(.), ' ', '_'))}">
            <em><xsl:value-of select="normalize-space(.)"/></em>
        </a>
    </xsl:template>

</xsl:stylesheet>

我已经停止了转换DLDTDD元素的模板。如果你不需要它们,我会保持简单。基本上,这些元素只是按原样带回来。

我只需将像href="Governing_Body"这样的链接转换为小写,如:href="governing_body"

由于

2 个答案:

答案 0 :(得分:2)

XSLT 2.0具有fn:upper-case()fn:lower-case()功能。

但是,如果您使用的是XSLT 1.0,则可以使用translate()

<xsl:template match="/">
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    <xsl:value-of select="translate(doc, $smallcase, $uppercase)" />
</xsl:template>

答案 1 :(得分:2)

如果您希望em模板看到变量,则需要在顶层声明变量。之后,您所要做的就是在另一个concat()内拨打现有translate来转换案例:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="UTF-8"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <xsl:template match="/">
        <xsl:apply-templates select="APPENDIX"/>
    </xsl:template>

    <xsl:template match="em">
        <a href="{translate(concat('#',translate(normalize-space(.), ' ', '_')), $uppercase, $smallcase)}">
            <em><xsl:value-of select="normalize-space(.)"/></em>
        </a>
    </xsl:template>

</xsl:stylesheet>
相关问题