关于 xml:XSL 使用某个键对某些元素进行计数 | 珊瑚贝

XSL count of certain elements using a key


我无法获取使用键查找的元素总数。

有 rdf:Description 元素的类型属性为”#Concept”或另一个不是概念的元素,例如”#A”。

不属于概念类型的

rdf:Description 元素有一个子元素 hasConcept,其属性 rdf:resource 包含一个值,该值对应于父 rdf:Description/@:rdf:type 的属性 hasConceptUI/@rdf:resource 的值”#A” – 举个例子 – 说那种类型的 rdf:Descriptions 有多个 ‘hasTerm’ 元素。

我想通过选择一个特定的非概念 rdf:Description 来计算这些 hasTerm 元素的数量。

有多个 rdf:Description rdf:type”#A” 元素。所以我的问题是我只找到每个描述的计数,而不是所有描述的计数。

XSL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version=“1.0” encoding=“UTF-8”?>
<xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
           xmlns:rdf=“http://www.w3.org/1999/02/22-rdf-syntax-ns#”>
<xsl:output method=“text”/>

<xsl:key name =“terms” match =“rdf:Description[@rdf:type = ‘#Concept’]” use =“hasConceptUI”/>

<xsl:template match=“/”>
    <xsl:apply-templates select=“rdf:RDF/rdf:Description[@rdf:type = ‘#A’]”/>
</xsl:template>

<xsl:template match =“rdf:Description[@rdf:type = ‘#A’]”>
    <xsl:variable name =“test” select =“key(‘terms’, substring(hasConcept/@rdf:resource, 2))”/>
    <xsl:for-each select=“$test/hasTerm”>
        <xsl:value-of select=“concat(rdf:Bag/hasTermName/text(), ‘ ‘)”/>
    </xsl:for-each>
    <xsl:value-of select=“concat(‘Term Count: ‘, count($test/hasTerm), ‘ Position:’, position(), ‘ ‘, ‘ ‘)”/>
</xsl:template>

<!– Exclude Classes not matched –>
<xsl:template match=“rdf:Description[@rdf:type != ‘#A’]”>
</xsl:template>

XML 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<rdf:Description rdf:ID=“M0006190” rdf:type=“#Concept”>
      <hasConceptUI>M0006190</hasConceptUI>
      <hasTerm>
         <rdf:Bag>
            <hasTermUI>T011956</hasTermUI>
            <hasTermName>Diagnostic Imaging</hasTermName>
         </rdf:Bag>
      </hasTerm>
      <hasTerm>
         <rdf:Bag>
            <hasTermUI>T011955</hasTermUI>
            <hasTermName>Imaging, Diagnostic</hasTermName>
         </rdf:Bag>
      </hasTerm>
</rdf:Description>

<rdf:Description rdf:ID=“D010284” rdf:type=“#C”>
      <hasParentRecord rdf:resource=“#D012480” rdf:type=“#C”/>
      <hasConcept rdf:resource=“#M0015934”/>
</rdf:Description>

这是我目前使用上述样式表的输出示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Abdomen
Abdomens
Abdomen
Term Count: 3 Position:1

Abdominal Muscles
Abdominal Muscle
Muscle, Abdominal
Muscles, Abdominal
Bauchmuskeln
Term Count: 5 Position:2

Abducens Nerve
Nerve, Abducens
Sixth Cranial Nerve
Cranial Nerve, Sixth
Nerve, Sixth Cranial
Nerves, Sixth Cranial
Sixth Cranial Nerves
Nerve VI

(deleted terms here for brevity)
Nerve VIs, Cranial
Nervus abducens
Hirnnerv VI
VI. Hirnnerv
Term Count: 23 Position:3

Abomasum
Abomasums
Labmagen
Term Count: 3 Position:4

Acanthocytes
Acanthocyte
Akanthozyten
Term Count: 3 Position:5

无论如何我可以使用我的密钥来计算文档中的所有术语,而不是现在只计算每个 rdf:Description 查找的术语(不是概念)的方式。我很感激任何帮助。

  • 您是否仅限于 XSLT 1?由于键值是使用子字符串选择的,因此很难在 XPath 1.0 中将其写为一个表达式,而在使用 XPath 2.0 的 XSLT 2 中,您当然可以使用例如count(//foo/key(‘key-name’, substring(…))/bar),即在路径表达式的一个步骤中调用key函数。
  • 如果我不需要更改任何其他内容,那么这不是限制
  • 当我尝试将 XSLT 2 与您的表达式一起使用时,我收到以下错误:不允许将多个项目的序列作为 fn:substring() (“#M0000009″、”#M000615276”) 的第一个参数。你知道为什么这发生在 XSLT 2 而不是 XSLT 1 中吗?
  • key(‘terms’, substring(hasConcept/@rdf:resource, 2)) 会发生这种情况吗?是否有多个 hasConcept 孩子?然后重写为key(‘terms’, hasConcept/@rdf:resource/substring(., 2))。 XSLT 1 或具有 XSLT 1 向后兼容性的 XSLT 2 将字符串函数应用于第一个选定项目,而在 XSLT 2 中,尝试将它们应用于多个项目的序列是错误的,您必须显式选择一个或调用如图所示,每个项目上的功能,在路径的最后一步(或者如果需要/需要,使用 for .. return )。


我认为,如果您想使用密钥并且至少可以使用 XSLT 2,那么您可以选择

1
2
3
4
5
6
7
8
9
10
<xsl:stylesheet version=“2.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”
            xmlns:rdf=“http://www.w3.org/1999/02/22-rdf-syntax-ns#”>
<xsl:output method=“text”/>

<xsl:key name =“terms” match =“rdf:Description[@rdf:type = ‘#Concept’]” use =“hasConceptUI”/>

<xsl:template match=“/”>
    <xsl:value-of select=“count(rdf:RDF/rdf:Description[@rdf:type = ‘#A’]/key(‘terms’, hasConcept/@rdf:resource/substring(., 2))/hasTerm)”/>

</xsl:template>


来源:https://www.codenong.com/61142930/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9211.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?