我需要使用样式表转换将Solr服务器的响应转换为类似XML的格式。 Solr模式中的许多字段都是多值的,包括方括号,表示数组。出于显示目的,我想在字段值的开头和结尾删除这些括号。目前,我的XSLT看起来像这样(该示例引用了字段标题并处理没有标题的记录:
<pnx:title>
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string-length(field[@name='title']) > 0">
<xsl:value-of select="field[@name='title']" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="field[@name='text']" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- handle empty title -->
<xsl:choose>
<xsl:when test="string-length($title) = 0">
Untitled
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$title"/>
</xsl:otherwise>
</xsl:choose>
</pnx:title>
我通常会使用substring(string,number,number)从字符串中删除字符,但在这种情况下我不知道最后一个字符的位置。有什么想法吗?
谢谢, 我
答案 0 :(得分:5)
例如:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="abc">[11sdf sdsdf sdfds fsdf dsfsdf fsd2]</xsl:variable>
<xsl:variable name="length" select="string-length($abc)"/>
<xsl:message><xsl:value-of select=" $length"/></xsl:message>
<xsl:value-of select="substring($abc,2,($length - 2))"/>
</xsl:template>
</xsl:stylesheet>
产生输出:
11sdf sdsdf sdfds fsdf dsfsdf fsd2
答案 1 :(得分:1)
我已使用substring-before
和substring-after
成功解决了该问题。请参阅以下内容:
<pnx:title>
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string-length(field[@name='title']) > 0">
<xsl:value-of select="substring-before(substring-after(field[@name='title'],'['),']')" />
<xsl:otherwise>
<xsl:value-of select="field[@name='text']" />
</xsl:otherwise>
</xsl:when>
</xsl:choose>
</xsl:variable>
<!--handle empty title-->
<xsl:choose>
<xsl:when test="string-length($title) = 0">
Untitled
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$title"/>
</xsl:otherwise>
</xsl:choose>
</pnx:title>
谢谢,
我