根据标签计算 XML 文档相似度

Computing XML document similarity based on tags

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

我是这样想的: 提取标签并为两个文档列出它们。接下来,检查两个列表之间的 exact/partial 匹配。

在这方面,XSLT 是否提供任何 function/operation 用于比较字符串(标记)。 欢迎任何关于概念和实施的想法。

简单示例:

对于这些 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>

两个文档都有六个元素(标签),其中genre出现在两者中,title类似于booktitle,author有authorname,publish_date有date。所以,这两个是相似的。 (1 个完全匹配,3 个部分匹配)

假设以下 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: