1

长期从事前端语言编程,XSLT在项目中很少遇到。好吧,就是这样……我们目前在 XSLT 文件中有一些函数,它们用于比较节点并发出包含类似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>
4

1 回答 1

1

我会推荐 XMLUnit。XMLUnit 旨在比较 xmls - http://xmlunit.sourceforge.net/。我个人使用 XMLUNit 来比较大型(~10-20MB)xml,而不是使用 XSLT。

我在 XMLUNit 周围放置了一个 ANT 包装器,以便可以比较 xmls 的目录并创建 CSV 输出 - https://github.com/parj/AddOnJavaAntTasks/tree/master/org.pm.xml.AntXMLUnit

jar 文件 - https://github.com/parj/AddOnJavaAntTasks/downloads

于 2011-12-23T05:00:18.013 回答