在 XPath 1.0 中,如何测试长度为 1 的字符串是否在其他两个代码点之间?

发布于 2024-09-11 15:16:39 字数 282 浏览 0 评论 0原文

给定包含这些标签的文档,我如何判断是否“” < “H”<文档编码中的“z”?我正在尝试在 XPath 1.0 中执行此操作。

<text>H</text>
<range>
  <from>&#x20;</from>
  <to>z</to>
</range>

我什至可以摆脱使用 contains() 的麻烦,但是如何创建一个包含从“”到“z”的字符的字符串来进行测试呢?

Given a document containing these tags, how can I tell whether " " < "H" < "z" in the document's encoding? I'm trying to do this in XPath 1.0.

<text>H</text>
<range>
  <from> </from>
  <to>z</to>
</range>

I might even be able to get away with using contains(), but then how would I create a string containing the characters from " " through "z" to test against?

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

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

发布评论

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

评论(2

甜宝宝 2024-09-18 15:16:39

此转换查找某个 ascii 字符是否在任意两个 ascii 字符的(含)范围内

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vAscii"> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>

 <xsl:template match="/*">
  <xsl:call-template name="isInRange">
    <xsl:with-param name="pChar" select="text"/>
    <xsl:with-param name="pStarting" select="range/from"/>
    <xsl:with-param name="pEnding" select="range/to"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="isInRange">
  <xsl:param name="pChar"/>
  <xsl:param name="pStarting"/>
  <xsl:param name="pEnding"/>

  <xsl:value-of select=
   "contains($vAscii, $pChar[1])

   and

    string-length(substring-before($vAscii, $pChar[1]))
   >=
    string-length(substring-before($vAscii, $pStarting))

   and

    string-length(substring-before($vAscii, $pEnding))
   >=
    string-length(substring-before($vAscii, $pChar[1]))

   "/>
 </xsl:template>
</xsl:stylesheet>

应用于以下 XML 文档时(恰好包含所提供的 XML 片段) :

<t>
    <text>H</text>
    <range>
        <from> </from>
        <to>z</to>
    </range>
</t>

产生想要的结果

true

应用于此 XML 文档时

<t>
    <text>H</text>
    <range>
        <from>A</from>
        <to>G</to>
    </range>
</t>

再次产生正确的结果

false

This transformation finds if an ascii character is in the (inclusive) range of any two ascii characters:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:variable name="vAscii"> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>

 <xsl:template match="/*">
  <xsl:call-template name="isInRange">
    <xsl:with-param name="pChar" select="text"/>
    <xsl:with-param name="pStarting" select="range/from"/>
    <xsl:with-param name="pEnding" select="range/to"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="isInRange">
  <xsl:param name="pChar"/>
  <xsl:param name="pStarting"/>
  <xsl:param name="pEnding"/>

  <xsl:value-of select=
   "contains($vAscii, $pChar[1])

   and

    string-length(substring-before($vAscii, $pChar[1]))
   >=
    string-length(substring-before($vAscii, $pStarting))

   and

    string-length(substring-before($vAscii, $pEnding))
   >=
    string-length(substring-before($vAscii, $pChar[1]))

   "/>
 </xsl:template>
</xsl:stylesheet>

when applied on the following XML document (that contains exactly the provided XML fragment):

<t>
    <text>H</text>
    <range>
        <from> </from>
        <to>z</to>
    </range>
</t>

produces the wanted result:

true

When applied on this XML document:

<t>
    <text>H</text>
    <range>
        <from>A</from>
        <to>G</to>
    </range>
</t>

again the correct result is produced:

false
暗地喜欢 2024-09-18 15:16:39

我认为 XPath 1.0 不可能做到这一点。 2.0 中使用的是 fn:compare 函数: http://www. w3.org/2005/xpath-functions/#compare

我无法尝试,但我猜 XPath 会是:

fn:compare(text, range/from) > 0 and fn:compare(text, range/to) < 0

I don't think it's possible with XPath 1.0. It is with 2.0, though using the fn:compare function: http://www.w3.org/2005/xpath-functions/#compare

I'm not able to try it out but I guess the XPath would be:

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