gpt4 book ai didi

xml - 计算 XML 文档中特定节点的出现频率

转载 作者:数据小太阳 更新时间:2023-10-29 02:32:19 25 4
gpt4 key购买 nike

这可能以前已经介绍过,但经过大量谷歌搜索后,我似乎找不到任何示例。本质上,我是想统计一本书被借出的次数。下面给出了我的 XML 树,以及 XSL 和结果输出。

    <?xml-stylesheet type="text/xsl" href="LoanStyler.xsl"?>
<loans>
<loan id="0001">
<summary>
<user>AAA</user>
<dateOut>2011-01-01</dateOut>
<dateDue>2011-01-14</dateDue>
</summary>
<details>
<books>
<name>Book7</name>
<name>Book4</name>
</books>
</details>
</loan>
<loan id="0002">
<summary>
<user>BBB</user>
<dateOut>2011-01-10</dateOut>
<dateDue>2011-01-24</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book2</name>
<name>Book4</name>
<name>Book6</name>
</books>
</details>
</loan>
<loan id="0003">
<summary>
<user>CCC</user>
<dateOut>2011-01-14</dateOut>
<dateDue>2011-01-28</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book3</name>
<name>Book4</name>
<name>Book7</name>
<name>Book8</name>
</books>
</details>
</loan>
<loan id="0004">
<summary>
<user>DDD</user>
<dateOut>2011-02-01</dateOut>
<dateDue>2011-02-14</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book2</name>
<name>Book4</name>
</books>
</details>
</loan>
</loans>

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<xsl:key
name="books-by-name"
match="//loans/loan/details/books"
use="name"
/>


<xsl:template match="/">
<html>
<body>
<h1>Loan Records Log</h1>
<table Border="1">
<tr>
<th>Loan ID</th>
<th>User</th>
<th>Date Out</th>
<th>Date Due</th>
</tr>
<xsl:for-each select="//loans/loan">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="summary/user"/></td>
<td><xsl:value-of select="summary/dateOut"/></td>
<td><xsl:value-of select="summary/dateDue"/></td>
</tr>
</xsl:for-each>
</table>

<table Border="1">
<tr>
<th>Book name</th>
<th>Count</th>
</tr>

<xsl:for-each select="books[count(. | key('books-by-name', name)[1]) = 1]">
<tr>
<td><xsl:value-of select="name" /></td>
<td><xsl:value-of select="count(key('books-by-name', name))" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Loan Records Log
Loan ID User Date Out Date Due
0001 AAA 2011-01-01 2011-01-14
0002 BBB 2011-01-10 2011-01-24
0003 CCC 2011-01-14 2011-01-28
0004 DDD 2011-02-01 2011-02-14
Book name Count

如您所见,Muenchian Grouping 的尝试似乎没有奏效。有人能解释一下我做错了什么吗,因为我能找到的唯一例子似乎没有帮助。

最佳答案

问题就在这里:

   <xsl:for-each select="books
[count(. | key('books-by-name', name)[1]) = 1]">

必须是:

   <xsl:for-each select="loans/loan/details/books
[count(. | key('books-by-name', name)[1]) = 1]">

在指定上述指令的位置,当前节点为/并且/没有子元素books

另外,还有一个逻辑错误。 key 必须不同。

这是一个更正的解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="books-by-name"
match="books/name" use="." />
<xsl:template match="/">
<html>
<body>
<h1>Loan Records Log</h1>
<table Border="1">
<tr>
<th>Loan ID</th>
<th>User</th>
<th>Date Out</th>
<th>Date Due</th>
</tr>
<xsl:for-each select="//loans/loan">
<tr>
<td>
<xsl:value-of select="@id"/>
</td>
<td>
<xsl:value-of select="summary/user"/>
</td>
<td>
<xsl:value-of select="summary/dateOut"/>
</td>
<td>
<xsl:value-of select="summary/dateDue"/>
</td>
</tr>
</xsl:for-each>
</table>
<table Border="1">
<tr>
<th>Book name</th>
<th>Count</th>
</tr>
<xsl:for-each select="loans/loan/details/books/name
[count(. | key('books-by-name', name)[1]) = 1]">
<tr>
<td>
<xsl:value-of select="." />
</td>
<td>
<xsl:value-of select="count(key('books-by-name',.))" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<loans>
<loan id="0001">
<summary>
<user>AAA</user>
<dateOut>2011-01-01</dateOut>
<dateDue>2011-01-14</dateDue>
</summary>
<details>
<books>
<name>Book7</name>
<name>Book4</name>
</books>
</details>
</loan>
<loan id="0002">
<summary>
<user>BBB</user>
<dateOut>2011-01-10</dateOut>
<dateDue>2011-01-24</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book2</name>
<name>Book4</name>
<name>Book6</name>
</books>
</details>
</loan>
<loan id="0003">
<summary>
<user>CCC</user>
<dateOut>2011-01-14</dateOut>
<dateDue>2011-01-28</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book3</name>
<name>Book4</name>
<name>Book7</name>
<name>Book8</name>
</books>
</details>
</loan>
<loan id="0004">
<summary>
<user>DDD</user>
<dateOut>2011-02-01</dateOut>
<dateDue>2011-02-14</dateDue>
</summary>
<details>
<books>
<name>Book1</name>
<name>Book2</name>
<name>Book4</name>
</books>
</details>
</loan>
</loans>

产生了想要的、正确的结果:

<html>
<body>
<h1>Loan Records Log</h1>
<table Border="1">
<tr>
<th>Loan ID</th>
<th>User</th>
<th>Date Out</th>
<th>Date Due</th>
</tr>
<tr>
<td>0001</td>
<td>AAA</td>
<td>2011-01-01</td>
<td>2011-01-14</td>
</tr>
<tr>
<td>0002</td>
<td>BBB</td>
<td>2011-01-10</td>
<td>2011-01-24</td>
</tr>
<tr>
<td>0003</td>
<td>CCC</td>
<td>2011-01-14</td>
<td>2011-01-28</td>
</tr>
<tr>
<td>0004</td>
<td>DDD</td>
<td>2011-02-01</td>
<td>2011-02-14</td>
</tr>
</table>
<table Border="1">
<tr>
<th>Book name</th>
<th>Count</th>
</tr>
<tr>
<td>Book7</td>
<td>2</td>
</tr>
<tr>
<td>Book4</td>
<td>4</td>
</tr>
<tr>
<td>Book1</td>
<td>3</td>
</tr>
<tr>
<td>Book2</td>
<td>2</td>
</tr>
<tr>
<td>Book4</td>
<td>4</td>
</tr>
<tr>
<td>Book6</td>
<td>1</td>
</tr>
<tr>
<td>Book1</td>
<td>3</td>
</tr>
<tr>
<td>Book3</td>
<td>1</td>
</tr>
<tr>
<td>Book4</td>
<td>4</td>
</tr>
<tr>
<td>Book7</td>
<td>2</td>
</tr>
<tr>
<td>Book8</td>
<td>1</td>
</tr>
<tr>
<td>Book1</td>
<td>3</td>
</tr>
<tr>
<td>Book2</td>
<td>2</td>
</tr>
<tr>
<td>Book4</td>
<td>4</td>
</tr>
</table>
</body>
</html>

关于xml - 计算 XML 文档中特定节点的出现频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5065142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com