我正在尝试从一个大字符串中为某个变量分配一个特定的标记。我首先将字符串标记化,然后针对每个标记检查它是否包含某个子字符串。如果是这样,我想将该令牌分配给变量。
最后,我使用该变量设置div的属性。
我在下面尝试了此代码,它为我提供了所需的确切输出oXygen XML Editor
。但是,当我在IE
(11)中运行XML / XSLT文件时,它只是简单地打印出整个原始字符串,即xhtmlVar
在下面的XSLT中进行打印。div甚至没有显示出来(它可能在DOM中,但是我看不到它-我会暂时重新检查)。
XSLT
<!-- xhtmlVar variable is a large string -->
<xsl:variable name="xhtmlVar" select="metadata[@element='xhtml_head_item']"></xsl:variable>
<xsl:variable name="quoteChar">"</xsl:variable> <!-- for cleaning token below -->
<xsl:variable name="tokenized" select="tokenize($xhtmlVar,' ')"/>
<xsl:variable name="doi">
<xsl:for-each select="$tokenized">
<xsl:variable name="curtoken" select="."/>
<!-- if token contains the string 'doi', assign it to the variable -->
<xsl:if test="contains($curtoken, 'doi')">
<!-- return value while stripping some stuff (token looks like this: doi:asdasdasd") -->
<xsl:value-of select="translate(replace($curtoken, 'doi:', ''),$quoteChar,'')"></xsl:value-of>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- pass $doi variable as attribute value in a div -->
<div type='medium' class='embed' handle='{$doi}'></div>
我怎样才能实现自己想要的?难道我做错了什么?任何有关如何更优雅地编写上述代码的技巧,也将受到赞赏!
提前致谢!
更新:
我已将代码更改为使用EXSLT,如下面的Martin Honnen所建议。
但是,现在标记化模板似乎只是删除了指定的分隔符,而不是实际将其用作分隔符。另外,我不知道如何使用空格作为定界符:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="exsl str"
exclude-result-prefixes="xs"
version="1.0">
<xsl:import href="str/str.xsl" />
<xsl:import href="str.tokenize/str.tokenize.template.xsl" />
...
<xsl:variable name="quoteChar">"</xsl:variable>
<xsl:variable name="spaceChar"> </xsl:variable>
<xsl:variable name="tokenized">
<xsl:call-template name="str:tokenize">
<xsl:with-param name="string" select="$xhtmlVar" />
<xsl:with-param name="delimiters" select="','" />
</xsl:call-template>
</xsl:variable>
<!-- prevent tree fragment error with exsl:node-set -->
<xsl:for-each select="exsl:node-set($tokenized)">
<xsl:variable name="curtoken" select="."/>
<xsl:value-of select="$curtoken"/>
<xsl:text> Ha </xsl:text>
<!-- Nevermind checking if each token contains what I want for now...
<xsl:if test="contains($curtoken, 'doi')">
<xsl:value-of select="translate(str:replace($curtoken, 'doi:', ''),$quoteChar,'')"></xsl:value-of>
</xsl:if>-->
</xsl:for-each>
上面的代码不会打印出由单词“ Ha”分隔的每个令牌,而是打印出整个字符串(每个令牌),但逗号分隔符“,”将被删除。然后在最后出现“ Ha”。我是否可能node-set
不正确使用该功能?
另外,如果我尝试使用像这样的定界符$spaceChar
或整个词,例如'than'
,我经常会遇到“模板指令堆栈溢出”错误。
每个michael.hor的答案的代码。
使用,str:replace()
像这样
<xsl:value-of select="translate(str:replace($curtoken, 'doi:', ''),$quoteChar,'')"/>
但是,在oXygen XML中给我这个错误:
Severity: fatal
Description: java.lang.NoSuchMethodException: For extension function, could not find method org.apache.xalan.lib.ExsltStrings.replace([ExpressionContext,] #NODESET, #STRING, #STRING).
Checked both static and instance methods. - For extension function, could not find method org.apache.xalan.lib.ExsltStrings.replace([ExpressionContext,] #NODESET, #STRING, #STRING).
Checked both static and instance methods.
重新更新您的问题:
如果您的处理器支持EXSLT str:tokenize()扩展功能,则:
尝试使用以下样式表作为测试(它可用于任何输入):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="input" select="'some,comma,delimited,string'" />
<xsl:template match="/">
<xsl:variable name="tokenized" select="str:tokenize($input, ',')" />
<output>
<xsl:for-each select="$tokenized">
<xsl:value-of select="." />
<xsl:text> Ha </xsl:text>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句