簡體   English   中英

如何比較兩個xml對象與xslt?

[英]How to compare two xml objects with xslt?

我想比較一個XML文件中的兩個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>

我需要結果如下:

屬性類別的區別名稱,舊值:“舊類別名稱”,新值:“新類別名稱”

有人可以幫幫我嗎?

您可以遍歷所有屬性並比較它們的值。 如果對象的結構沒有更多嵌套,那么在您的示例中,這應該工作:

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

自從我編寫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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM