基于标签计算XML文档相似度

时间:2015-01-21 13:37:25

标签: xml xslt pattern-matching string-matching similarity

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

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

在这方面,XSLT是否提供用于比较字符串(标签)的任何功能/操作。 对概念和实施的任何想法都受到欢迎。

简单示例:

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

<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>

和这一个,

  <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>

两个文档都有六个元素(标签),其中类型出现在两者中,标题类似于booktitle,作者使用authorname,publish_date使用日期。所以,这两者是相似的。 (1个完全匹配,3个部分匹配)

1 个答案:

答案 0 :(得分:1)

假设XSLT 2.0以下将第一个XML文档作为输入,第二个文档的URL作为参数,然后在第一个文档中为每个元素名称输出一个包含名称的列表或在第二个文档中包含名称:

<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>

因此,对于您的样本,输出是

book: books, booktitle
author: authorname
title: booktitle
genre: genre
price:
publish_date: date
description: