首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于标记的XML文档相似度计算

基于标记的XML文档相似度计算
EN

Stack Overflow用户
提问于 2015-01-21 13:37:25
回答 1查看 114关注 0票数 0

作为计算XML文档之间相似度的一种方法(通常是几种,但在这种情况下是两种),基于标记的相似度计算有几种应用。现在,如何使用XSLT实现这样的方法。

我认为这是这样的:提取标记并为两个文档列出它们。接下来,检查两个列表之间的精确/部分匹配。

在这方面,XSLT是否为比较字符串(标记)提供了任何函数/操作。任何关于这一概念和执行的想法都是受欢迎的。

简单示例:

对于这些XML文档(当然是其中的一部分),

代码语言:javascript
复制
<book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>

而这个,

代码语言:javascript
复制
  <books>
      <authorname>Ralls, Kim</authorname>
      <booktitle>Midnight Rain</booktitle>
      <genre>Fantasy</genre>
      <cost>5.95</cost>
      <date>2000-12-16</date>
      <abstract>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</abstract>
   </books>

这两个文档都有六个元素(标签),其中两种类型都有,标题类似书名,作者有作者名,publish_date有日期。所以,这两者是相似的。(1次精确匹配,3次部分匹配)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-21 15:43:25

假设XSLT2.0,以下内容以第一个XML文档作为输入,第二个文档的URL作为参数,然后为第一个文档中的每个元素名称输出第二个文档中包含或包含名称的名称列表:

代码语言:javascript
复制
<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output method="text"/>  

<xsl:param name="doc2-url" as="xs:string" select="'test2015012102.xml'"/>
<xsl:variable name="doc2" as="document-node()" select="doc($doc2-url)"/>
<xsl:variable name="doc2-names" as="xs:string*" select="distinct-values($doc2//*/local-name())"/>

<xsl:template match="/">
  <xsl:value-of select="for $name in distinct-values(//*/local-name())
                        return concat($name, ': ', string-join($doc2-names[contains($name, .) or contains(., $name)], ', '))"
                separator="&#10;"/>
</xsl:template>

</xsl:stylesheet>

因此,对于您的示例,输出是

代码语言:javascript
复制
book: books, booktitle
author: authorname
title: booktitle
genre: genre
price:
publish_date: date
description: 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28068633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档