0

1 つの XML ファイルで 2 つの XML ノードを比較したいのですが、違いを比較して要約を書きます。

ここに私のxmlデータがあります:

<AuditLog>
   <OldValue>
      <ProcessCategory>
         <CategoryId>3</CategoryId>
         <ChildCategories />
         <Created>2012-12-13T11:39:30.747</Created>
         <Name>New category name</Name>
         <ParentCategory />
      </ProcessCategory>
   </OldValue>
   <NewValue>
     <ProcessCategory>
        <CategoryId>3</CategoryId>
        <ChildCategories />
        <Created>2012-12-13T11:39:30.747</Created>
        <Name>Old Category name</Name>
        <ParentCategory />
     </ProcessCategory>
   </NewValue>
</AuditLog>

次のような結果が必要です。

プロパティカテゴリ名の違い、旧値:「旧カテゴリ名」、新値:「新カテゴリ名」

誰でも私を助けてもらえますか?

4

2 に答える 2

2

すべてのプロパティを繰り返し処理し、それらの値を比較できます。あなたの例では、オブジェクトの構造がネストされていない場合、これは機能するはずです:

    <xsl:template match="/">
      <xsl:for-each select="AuditLog">

        <xsl:call-template name="for">
          <xsl:with-param name="i">0</xsl:with-param>
          <xsl:with-param name="max" select="count(OldValue/*/*)" />
        </xsl:call-template>

      </xsl:for-each>
    </xsl:template>

  <xsl:template name="for">
    <xsl:param name="i" />
    <xsl:param name="max" />

    <xsl:variable name="oldValue" select="OldValue/*/*[$i]" />    
    <xsl:variable name="newValue" select="NewValue/*/*[$i]" />
    <xsl:variable name="prop" select="name(OldValue/*/*[$i])" />

    <xsl:if test="not($newValue=$oldValue)">
      <Changed Property="{$prop}" oldValue="{$oldValue}" newValue="{$newValue}" />
    </xsl:if>

    <xsl:if test="$i &lt; $max">
      <xsl:call-template name="for">
        <xsl:with-param name="i" select="$i+1" />
        <xsl:with-param name="max" select="$max" />
      </xsl:call-template>
    </xsl:if>

  </xsl:template>                
于 2012-12-17T10:18:36.660 に答える
0

XSLTを書いてからしばらく経っているので、XPATHがオフになっている可能性がありますが、これを試してください:

<xsl:if test="not(OldValue/ProcessCategory/Name=NewValue/ProcessCategory/Name">
    Old Name: <xsl:value-of select="OldValue/ProcessCategory/Name"/>
    New Name: <xsl:value-of select="NewValue/ProcessCategory/Name"/>
</xsl:if>
于 2012-12-17T09:08:00.370 に答える