gpt4 book ai didi

xml - 如何计算 XSLT 中的引用总数?

转载 作者:行者123 更新时间:2023-12-03 17:20:23 24 4
gpt4 key购买 nike

我的问题是:使用XSLT,如何统计QUOTE标签的总数(请看下面的示例代码)
结果需要在 HTLM 中导出,它会显示如下:共有 6 个引号

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="text.xsl" ?>

<quotes>
<quote>Quote 1 </quote>
<quote>Quote 2</quote>
<quote>Quote 3</quote>
<quote>Quote 4</quote>
<quote>Quote 5</quote>
<quote>Quote 6</quote>
</quotes>

我已经尝试过这个 XSLT 代码,但它不起作用:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:value-of select="count(//quote)"></xsl:value-of>
</xsl:template>

</xsl:stylesheet>

你能帮我解决这个问题吗?谢谢

最佳答案

您的 XPath 表达式 ,虽然效率不高,产生正确的结果 .

当使用 Saxon 9.1.0.5J 运行转换时,结果为:

<?xml version="1.0" encoding="UTF-8"?>6

问题似乎是这是一个 XSLT 2.0 转换 (它不需要!),并且您似乎正在尝试在浏览器中运行它。 不幸的是,今天的浏览器不支持(还)XSLT 2.0 .

解决方案是简单地将版本更改为 1.0 .

此转换也不需要 XML Schema namespace 。

最后,如果提供的 XML 文档的结构不会改变,一个更有效的 XPath 表达式(因为使用 // 缩写会导致从顶部元素节点开始扫描整个(子)树),将是下列的:
count(/*/quote)

将所有这些放在一起,我们得到以下转换 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html"/>

<xsl:template match="/">
<xsl:value-of select="count(/*/quote)"/>
</xsl:template>
</xsl:stylesheet>

它会产生想要的结果 .

关于xml - 如何计算 XSLT 中的引用总数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/736702/

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