Xslt通过Xslt按属性匹配元素

Xslt通过Xslt按属性匹配元素,xslt,Xslt,我有一个XSLT文件 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="vrtfDoc2"> </xsl:param> <

我有一个XSLT文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="vrtfDoc2">
  </xsl:param>

  <xsl:variable name="vDoc2" select="$vrtfDoc2/*"/>

  File 1 TO File 2 MATCH

  <xsl:template match="node()|@*" name="identity">
    <xsl:param name="pDoc2"/>
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:with-param name="pDoc2" select="$pDoc2"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="Topic/SubTopic/*">
      <xsl:with-param name="pDoc2" select="$vDoc2"/>
    </xsl:apply-templates>

    -----------------------

    File 2 TO File 1 MATCH

    <xsl:apply-templates select="$vDoc2">
      <xsl:with-param name="pDoc2" select="/*"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Topic/SubTopic/*">
    <xsl:param name="pDoc2"/>
    <xsl:variable name="guid" select="../@Guid" />
    <table border="1" style="width:100%">
      <thead>
        <td>Current Element</td>
        <td>Left Guid</td>
        <td>Left</td>
        <td>Right</td>
        <td>Right Guid</td>
        <td>Diff Type</td>
      </thead>

      <xsl:if test="not(. = $pDoc2/*/*[name()=name(current())])">
      <tr>
        <td>
          <xsl:value-of select="name()"/>
        </td>
        <td>
          <xsl:value-of select="$guid"/>
        </td>
        <td>
          <xsl:value-of select="."/>
        </td>
        <td>
          <xsl:value-of select="$pDoc2/*/*[name()=name(current())]"/>
        </td>
        <td>

        </td>
        <td>Diff</td>
      </tr> 
    </xsl:if>
    <xsl:if test=". = $pDoc2/*/*[name()=name(current())]">
      <tr>
        <td>
          <xsl:value-of select="name()" />
        </td>
        <td>
          <xsl:value-of select="../@Guid"/>
        </td>
        <td>
          <xsl:value-of select="."/>
        </td>
        <td>
          <xsl:value-of select="$pDoc2/*/*[name()=name(current())]"/>
        </td>
        <td>
          <xsl:value-of select="$pDoc2/*/*[name()=name(current())]"/>
        </td>
        <td>Same</td>
      </tr>
    </xsl:if>
    </table>
  </xsl:template>
</xsl:stylesheet>

文件1到文件2匹配
-----------------------
文件2与文件1匹配
当前元素
左Guid
左边
赖特
右Guid
差分型
差异
同样的
本质上,转换文件从左到右再从右到左检查相等性。唯一的问题是我的xml文件有类似的代码片段

  <SubTopic Guid="462AF46304694D4785EF3B7E642AD8A2">
    <Description xmlns="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Modification History</Description>
    <Text xmlns="http://www.authorit.com/xml/authorit" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <p id="4">Not Applicable</p>
    </Text>
  </SubTopic>

修改历史

不适用

xslt按从上到下的顺序提供了匹配元素的差异,但不能保证它匹配正确的元素。它正在将左侧比较xml中的
与右侧比较xml中的
进行比较

线路

<xsl:variable name="guid" select="../@Guid" />

从左侧xml文件中为我提供父guid,这是正确的,但我尝试了以下变体

<xsl:if test="not(. = $pDoc2/*/*[name()=name(current())][@Guid=$guid])">

但是这个过程就像“[@Guid=$Guid]”不存在一样

简而言之,xslt必须比较正确的元素,并且必须具有


我知道我的xslt技能很低,但我确信我缺少了一些明显的东西。

我看到
$guid
变量是使用
。/@guid
(因此
父轴
轴)构建的,然后XPath表达式正在寻找名称和guid的相等,但希望两者在同一级别上匹配。也许应该将其转换为
[name()=name(current())][../@Guid=$Guid]
?如果不是这样的话,也许你可以发布你的输入和预期结果的简化版本?这将帮助我们调试转换。另请注意,
=比较将测试两个节点的字符串值是否相等。如果希望它测试是否存在具有相同名称和父Guid的节点,则需要将其缩短为
not($pDoc2/*/*[name()=name(current())][../@Guid=$Guid]