XSLT 问题:在字符串中搜索子字符串“&”或“>”
我正在学习 XSLT,目前我真的陷入困境,无法找到解决方案。如果您能帮我解决这个问题,我将不胜感激。 示例
在下面您可以找到$string 的
<img src="Afbeeldingen_Hotpot/beer.jpg" alt="afbeelding van een beer" title="beer" width="170" height="144" style="display:block; margin-left:auto; margin-right:auto; text-align:center;" style="float:center;" />
值:我的目标是过滤掉图像的高度...在此示例中为“144”。 我从这个网站获得的 functx:index-of-string: http://www .xsltfunctions.com/xsl/functx_index-of-string.html
这是我现在使用的 XSLT 代码:
<xsl:variable name="positionHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($string,'height=') + 8"/></xsl:variable>
<xsl:variable name="partStringHeight" as="xs:string"><xsl:value-of select="substring($string,$positionHeight)"/></xsl:variable>
<xsl:variable name="lengthHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($partStringHeight,'style=' - 3)"/></xsl:variable>
<xsl:variable name="height" as="xs:string"><xsl:sequence select="substring($partStringHeight,1,$lengthHeight)"/></xsl:variable>
在我想要使用的 XSLT 代码下方,但给出了错误,因为“””的字符串索引为零”。
<xsl:variable name="positionHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($string,'height=') + 8"/></xsl:variable>
<xsl:variable name="partStringHeight" as="xs:string"><xsl:value-of select="substring($string,$positionHeight)"/></xsl:variable>
<xsl:variable name="lengthHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($partStringHeight,'"' - 1)"/></xsl:variable>
<xsl:variable name="height" as="xs:string"><xsl:sequence select="substring($partStringHeight,1,$lengthHeight)"/></xsl:variable>
有没有办法获取子字符串的索引,例如:
" and &#x003E;
因为我有很多字符串,所以我需要高度,但其中“style=”不发生。
I'm learning XSLT and at the moment I'm really stuck and can't find a solution. I really would appreciate if you could help me with this.
Beneath you can find an example
value of $string:
<img src="Afbeeldingen_Hotpot/beer.jpg" alt="afbeelding van een beer" title="beer" width="170" height="144" style="display:block; margin-left:auto; margin-right:auto; text-align:center;" style="float:center;" />
My goal is to filter out the height of the image... In this example "144".
the functx:index-of-string I got from this website: http://www.xsltfunctions.com/xsl/functx_index-of-string.html
This the XSLT code I now use:
<xsl:variable name="positionHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($string,'height=') + 8"/></xsl:variable>
<xsl:variable name="partStringHeight" as="xs:string"><xsl:value-of select="substring($string,$positionHeight)"/></xsl:variable>
<xsl:variable name="lengthHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($partStringHeight,'style=' - 3)"/></xsl:variable>
<xsl:variable name="height" as="xs:string"><xsl:sequence select="substring($partStringHeight,1,$lengthHeight)"/></xsl:variable>
Beneath the XSLT code I want to use, but gives an error because the index-of-string for """ is zero".
<xsl:variable name="positionHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($string,'height=') + 8"/></xsl:variable>
<xsl:variable name="partStringHeight" as="xs:string"><xsl:value-of select="substring($string,$positionHeight)"/></xsl:variable>
<xsl:variable name="lengthHeight" as="xs:integer"><xsl:value-of select="functx:index-of-string($partStringHeight,'"' - 1)"/></xsl:variable>
<xsl:variable name="height" as="xs:string"><xsl:sequence select="substring($partStringHeight,1,$lengthHeight)"/></xsl:variable>
Is there a way to get the index of a substrings like:
" and >
Because I have many strings were I need the height but where "style=" does not occur..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XSLT 中通常的解决方案是将不方便的字符串放入变量的值中(不使用
select
):然后
另外,请注意您已放入
- 3
或- 1
在index-of-string()
括号内。您确实想从index-of-string()
的返回值中减去 3 或 1 吗?因为从"
中减去 3 或 1 并没有什么意义。另外,在像这样的变量定义主体中使用
通常是多余的。最后,您可以使用
substring-before()
和substring-after()
轻松获得您想要的内容:实际上我'我不确定你的问题是否输入字符串您在顶部显示的值是在 XML 解析之前还是之后,如果是在解析之后,即字符串数据本身包含
"
而不是"
,则放入。"
但您需要将&
转义为&
:在这种情况下,您的代码中不会有引号字符串,因此您不需要将值放入变量中像这样;但这对于可读性来说可能是个好主意。
同样,
>
需要转义为>
。The usual solution in XSLT is to put the inconvenient string into the value of a variable (not using
select
):Then
Also, notice that you've put the
- 3
or- 1
inside theindex-of-string()
parentheses. Surely you want to subtract 3 or 1 from the return value ofindex-of-string()
? Because it doesn't really make sense to subtract 3 or 1 from"
.Also, using
<xsl:value-of />
inside a variable definition body like that is usually redundant. I removed it.Finally, you can use
substring-before()
andsubstring-after()
to get what you want with less hassle:Actually I'm not sure from your question whether the input string value you showed at the top was before or after XML parsing. If it was after parsing, i.e. the string data itself contains
"
rather than"
, then put in"
but you need to escape the&
as&
:In this case you would not have quotation marks in your strings, so you wouldn't need to put the values in variables like that; but it's probably a good idea for readability.
Similarly
>
would need to be escaped as&#x003e
.