0

我有一个 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>

本质上,转换文件检查从左到右然后从右到左的相等性。唯一的问题是我的 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 按从上到下的顺序为我提供了匹配元素的差异,但不保证它匹配正确的元素。它是<SubTopic Guid="462AF46304694D4785EF3B7E642AD8A2">从左侧比较 xml 到<SubTopic Guid="462AF46304694D4785EF3B7E642AD8A2">比较右侧 xml 进行比较。

线

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

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

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

但是那个过程好像'[@Guid=$guid]' 不存在。

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

I know my xslt skills are low but I am sure that I am missing something obvious.

4

1 回答 1

1

I see that $guid variable is built using the ../@Guid (thus the parent axes) and then the XPath expression is looking for an equality of names and guids but expect both to match on the same level. maybe it should be converted to [name() = name(current())][../@Guid = $guid]? if this is not the case then maybe you can post a simplified version of your input and the expected result? this would help us help you debug the transform. also as a note, the . = comparison will test for the equality of the string values of the two nodes. If you expect it to test for the existence of a node with the same name and parent Guid then you would need to shorten it to not($pDoc2/*/*[name()=name(current())][../@Guid=$guid]

于 2012-05-11T19:01:42.730 回答