在我的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>
我已经离开了那个转换模板DL
,DT
和DD
元素。如果您不需要它们,我将使其保持简单。基本上,这些元素只是按原样带回。
我只需要隐藏链接,例如href="Governing_Body"
小写:href="governing_body"
谢谢
如果希望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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句