XSLT 问题:在字符串中搜索子字符串“&”或“>”

发布于 2024-11-06 00:55:29 字数 2218 浏览 0 评论 0原文

我正在学习 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,'&quot;' - 1)"/></xsl:variable>
    <xsl:variable name="height" as="xs:string"><xsl:sequence select="substring($partStringHeight,1,$lengthHeight)"/></xsl:variable>

有没有办法获取子字符串的索引,例如:

&quot; and &amp;#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:

&#x003C;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;" /&#x003E;

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 &#x003E;

Because I have many strings were I need the height but where "style=" does not occur..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2024-11-13 00:55:29

XSLT 中通常的解决方案是将不方便的字符串放入变量的值中(不使用 select):

<xsl:variable name="quot">"</xsl:variable>

然后

<xsl:variable name="lengthHeight" as="xs:integer"
   select="functx:index-of-string($partStringHeight,$quot) - 1" />

另外,请注意您已放入 - 3- 1 index-of-string() 括号内。您确实想从 index-of-string()返回值中减去 3 或 1 吗?因为从 " 中减去 3 或 1 并没有什么意义。

另外,在像这样的变量定义主体中使用 通常是多余的。

最后,您可以使用 substring-before()substring-after() 轻松获得您想要的内容:

<xsl:variable name="beforeHeight" as="xs:integer">height="</xsl:variable>
<xsl:variable name="afterHeight" as="xs:string">" style=</xsl:variable>
<xsl:variable name="height" as="xs:string"
  select="substring-before(substring-after($string, $beforeHeight), $afterHeight)" />

实际上我'我不确定你的问题是否输入字符串您在顶部显示的值是在 XML 解析之前还是之后,如果是在解析之后,即字符串数据本身包含 " 而不是 ",则放入。 " 但您需要将 & 转义为 &

<xsl:variable name="beforeHeight" as="xs:integer">height=&quot;</xsl:variable>
<xsl:variable name="afterHeight" as="xs:string">&quot; style=</xsl:variable>

在这种情况下,您的代码中不会有引号字符串,因此您不需要将值放入变量中像这样;但这对于可读性来说可能是个好主意。

同样,&#x003e 需要转义为 &#x003e

The usual solution in XSLT is to put the inconvenient string into the value of a variable (not using select):

<xsl:variable name="quot">"</xsl:variable>

Then

<xsl:variable name="lengthHeight" as="xs:integer"
   select="functx:index-of-string($partStringHeight,$quot) - 1" />

Also, notice that you've put the - 3 or - 1 inside the index-of-string() parentheses. Surely you want to subtract 3 or 1 from the return value of index-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() and substring-after() to get what you want with less hassle:

<xsl:variable name="beforeHeight" as="xs:integer">height="</xsl:variable>
<xsl:variable name="afterHeight" as="xs:string">" style=</xsl:variable>
<xsl:variable name="height" as="xs:string"
  select="substring-before(substring-after($string, $beforeHeight), $afterHeight)" />

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 &:

<xsl:variable name="beforeHeight" as="xs:integer">height=&quot;</xsl:variable>
<xsl:variable name="afterHeight" as="xs:string">&quot; style=</xsl:variable>

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 &#x003e would need to be escaped as &amp;#x003e.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文