Using XSLT to compare two nodes and emit XML containing attribute indicating whether value was updated, added, or deleted

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

  •  23-09-2019
  •  | 
  •  

문제

Been programming front-end languages for a long time and I rarely encounter XSLT on a project. Well, here it is... We currently have some functions in our XSLT file that are comparing nodes and emitting XML that contains something like previousValue="Old value". This feature helps our users understand what changed when viewing the form.

Looking at the XML (below), I need to compare <ns1:OtherEducationTypeDesc> and emit XML correctly that states what the old value was.

I need it to look something like:

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

I tried to give as much information as possible but if you need anything else, let me know! Any assistance is appreciated!! Thanks!!


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>
도움이 되었습니까?

해결책

가능성 :

  1. 리디렉션 할 페이지의 콘텐츠 편집기 웹 파트에 자바 스크립트 리디렉션을 추가합니다.

  2. 현재 사용중인 마스터 페이지를 편집하고 현재 URL에 따라 사용자를 리디렉션하는 JavaScript 함수를 만듭니다.

    그러나 ISA 또는 TMG가있는 경우 리디렉션을 처리하기 위해 규칙을 다소 구현하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top