XSLT を使用して 2 つのノードを比較し、値が更新されたか、追加されたか、削除されたかを示す属性を含む XML を出力する

StackOverflow https://stackoverflow.com/questions/2143398

  •  23-09-2019
  •  | 
  •  

質問

長い間フロントエンド言語をプログラミングしてきましたが、めったに遭遇することはありません XSLT プロジェクトで。さて、ここで...現在、XSLT ファイルには、ノードを比較し、次のような内容を含む XML を出力する関数がいくつかあります。 previousValue="Old value". 。この機能は、ユーザーがフォームを表示したときに何が変更されたかを理解するのに役立ちます。

XML (以下) を見て、比較する必要があります。 <ns1:OtherEducationTypeDesc> 古い値が何であったかを示す XML を正しく出力します。

次のようにする必要があります。

<EducationTypes>
    <EducationType Code="11">Engineering</EducationType>
    <EducationType Code="12" Value="New Value" PrevValue="Old Value">Other</EducationType>
</EducationTypes>

できるだけ多くの情報を提供しようとしましたが、他に何か必要な場合はお知らせください。ご協力をお願いいたします。ありがとう!!


XSLT

<EducationTypes xmlns="omitted">
  <xsl:choose>
    <xsl:when test="$has-updates">
      <!--Get unchanged nodes-->
      <xsl:variable name="unchanged-nodes">
        <xsl:call-template name="intersection">
          <xsl:with-param name="nodes1" select="$educationType-nodes[1]/ns1:Code"/>
          <xsl:with-param name="nodes2" select="$educationType-nodes[last()]/ns1:Code"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="education-codes">
        <xsl:with-param name="node-set" select="msxsl:node-set($unchanged-nodes)/ns1:Code"/>
        <xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
      </xsl:call-template>

      <!--Get added nodes-->
      <xsl:variable name="added-nodes">
        <xsl:call-template name="difference">
          <xsl:with-param name="nodes1" select="$educationType-nodes[last()]/ns1:Code"/>
          <xsl:with-param name="nodes2" select="$educationType-nodes[1]/ns1:Code"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="education-codes">
        <xsl:with-param name="node-set" select="msxsl:node-set($added-nodes)/ns1:Code"/>
        <xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
        <xsl:with-param name="status" select="'added'"/>
      </xsl:call-template>

      <!--Get deleted nodes-->
      <xsl:variable name="deleted-nodes">
        <xsl:call-template name="difference">
          <xsl:with-param name="nodes1" select="$educationType-nodes[1]/ns1:Code"/>
          <xsl:with-param name="nodes2" select="$educationType-nodes[last()]/ns1:Code"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="education-codes">
        <xsl:with-param name="node-set" select="msxsl:node-set($deleted-nodes)/ns1:Code"/>
        <xsl:with-param name="otherText" select="$educationType-nodes[last()]/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
        <xsl:with-param name="status" select="'deleted'"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:call-template name="education-codes">
        <xsl:with-param name="node-set" select="$educationType-nodes/ns1:Code" />
        <xsl:with-param name="otherText" select="$educationType-nodes/ancestor::ns1:ProgramInfo/ns1:OtherEducationTypeDesc"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</EducationTypes>


XML

<?xml version="1.0" encoding="ISO-8859-1"?>
<ns1:ProgramInfo>
  <ns1:RecognizedDegrees>false</ns1:RecognizedDegrees>
    <ns1:EducationCodes>
      <ns1:Code>01</ns1:Code>
      <ns1:Code>02</ns1:Code>
      <ns1:Code>09</ns1:Code>
      <ns1:Code>10</ns1:Code>
      <ns1:Code>12</ns1:Code>
    </ns1:EducationCodes>
    <ns1:OtherEducationTypeDesc>Old Description</ns1:OtherEducationTypeDesc>
    <ns1:DegreeCodes>
      <ns1:Code>03</ns1:Code>
      <ns1:Code>06</ns1:Code>
      <ns1:Code>07</ns1:Code>
    </ns1:DegreeCodes>
    <ns1:OtherDegreeDesc></ns1:OtherDegreeDesc>
    <ns1:EducationLevels>
      <ns1:Code>08</ns1:Code>
    </ns1:EducationLevels>
    <ns1:OtherEducationLevelDesc></ns1:OtherEducationLevelDesc>
</ns1:ProgramInfo>

<ns1:ProgramInfo>
    <ns1:RecognizedDegrees>false</ns1:RecognizedDegrees>
    <ns1:EducationCodes>
      <ns1:Code>01</ns1:Code>
      <ns1:Code>02</ns1:Code>
      <ns1:Code>09</ns1:Code>
      <ns1:Code>10</ns1:Code>
      <ns1:Code>12</ns1:Code>
    </ns1:EducationCodes>
    <ns1:OtherEducationTypeDesc>New Description</ns1:OtherEducationTypeDesc>
    <ns1:DegreeCodes>
      <ns1:Code>03</ns1:Code>
      <ns1:Code>06</ns1:Code>
      <ns1:Code>07</ns1:Code>
    </ns1:DegreeCodes>
    <ns1:OtherDegreeDesc></ns1:OtherDegreeDesc>
    <ns1:EducationLevels>
      <ns1:Code>08</ns1:Code>
    </ns1:EducationLevels>
    <ns1:OtherEducationLevelDesc></ns1:OtherEducationLevelDesc>
  </ns1:ProgramInfo>
役に立ちましたか?

解決

代わりに XMLUnit をお勧めします。XMLUnit は XML を比較するように設計されています - http://xmlunit.sourceforge.net/. 。私は個人的に、XSLT を使用するのではなく、大きな (約 10 ~ 20MB) XML を比較するために XMLUNit を使用しました。

XMLUNit の周りに ANT ラッパーをまとめて、XML のディレクトリを比較して CSV 出力を作成できるようにしました。 https://github.com/parj/AddOnJavaAntTasks/tree/master/org.pm.xml.AntXMLUnit

jar ファイル - https://github.com/parj/AddOnJavaAntTasks/downloads

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top