开发者

using xslt to remove term if a property of it is a duplicate of another term

开发者 https://www.devze.com 2023-03-18 03:36 出处:网络
I looked at How to remove duplicate XML nodes using XSLT and other related questions, but they all seem about removing duplicates if the whole node is a duplicate.What I want to do is remove a node on

I looked at How to remove duplicate XML nodes using XSLT and other related questions, but they all seem about removing duplicates if the whole node is a duplicate. What I want to do is remove a node only if one properties within it matches a property within another node.

In my xml, I have 2 termTypes; Nd and Pt. Pts should be used. Where the system finds an Nd, the termName of it should not be used, instead, the name of the Pt referenced in relation termName should be used instead.

But something has gone wrong and some Nds have the same name as the name of the Pts they should be referencing. These terms are irrelevant and I need to delete them

I have:

<term>
<termId>1</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Nd</termType>
<relation>
  <relationType>USE</relationType>
  <termId>2</termId>
  <termName>A</termName>
</relation>
</term>

<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Pt</termType>
</term>

<term>
<termId>3</termId>
<termUpdate>Add</termUpdate>
<termName>C</termName>
<termType>Nd</termType>
<relation>
  <relationType>USE</relationType>
  <termId>4</termId>
  <termName>D</termName>
</relation>
</term>

<term>
<termId>4</termId>
<termUpdate>Add</termUpdate>
<termName>D</termName>
<termType>Pt</termType>
</term>

Is it possible to use xslt (or some other method) to go through and see that, if the <termName> of an Nd <term> matches the <termName> of a Pt term in its <relation>, delete the whole term? Terms referenc开发者_JAVA技巧ed in <relation> are always Pt terms.

Output:

<term>
<termId>2</termId>
<termUpdate>Add</termUpdate>
<termName>A</termName>
<termType>Pt</termType>
</term>

<term>
<termId>3</termId>
<termUpdate>Add</termUpdate>
<termName>C</termName>
<termType>Nd</termType>
<relation>
  <relationType>USE</relationType>
  <termId>4</termId>
  <termName>D</termName>
</relation>
</term>

<term>
<termId>4</termId>
<termUpdate>Add</termUpdate>
<termName>D</termName>
<termType>Pt</termType>
</term>

Thanks!


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kPtByName" match="term[termType='Pt']"
  use="termName"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="term[termType='Nd']">
  <xsl:if test="not(key('kPtByName', termName))">
   <xsl:call-template name="identity"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (wrapped into a top element to make it well-formed):

<terms>
    <term>
    <termId>1</termId>
    <termUpdate>Add</termUpdate>
    <termName>A</termName>
    <termType>Nd</termType>
    <relation>
      <relationType>USE</relationType>
      <termId>2</termId>
      <termName>A</termName>
    </relation>
    </term>

    <term>
    <termId>2</termId>
    <termUpdate>Add</termUpdate>
    <termName>A</termName>
    <termType>Pt</termType>
    </term>

    <term>
    <termId>3</termId>
    <termUpdate>Add</termUpdate>
    <termName>C</termName>
    <termType>Nd</termType>
    <relation>
      <relationType>USE</relationType>
      <termId>4</termId>
      <termName>D</termName>
    </relation>
    </term>

    <term>
    <termId>4</termId>
    <termUpdate>Add</termUpdate>
    <termName>D</termName>
    <termType>Pt</termType>
    </term>
</terms>

produces the wanted, correct output:

<terms>
   <term>
      <termId>2</termId>
      <termUpdate>Add</termUpdate>
      <termName>A</termName>
      <termType>Pt</termType>
   </term>
   <term>
      <termId>3</termId>
      <termUpdate>Add</termUpdate>
      <termName>C</termName>
      <termType>Nd</termType>
      <relation>
         <relationType>USE</relationType>
         <termId>4</termId>
         <termName>D</termName>
      </relation>
   </term>
   <term>
      <termId>4</termId>
      <termUpdate>Add</termUpdate>
      <termName>D</termName>
      <termType>Pt</termType>
   </term>
</terms>

Explanation:

  1. The identity rule copies every node "as is".

  2. There is just one template that overrides the identity template. It matches any term whose termType child has a value of Nd. This template only calls the identity template if there is no term with the same value of its tername child and whose termType child has a value of Pt. This test is performed using a conveniently defined xsl:key


This problem can also be solved by considering that we need to copy the nodes of type Nd only when the given condition is satisfied. We can then use xsl:copy-of to copy the wanted nodes and make use of the XSLT function current() to build a bit complex XPath.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:include href="identity.xsl"/>

 <xsl:template match="term[termType='Nd']" name="excludeNd">
     <xsl:copy-of select="self::node()
         [ not( ../term
                [ termId = current()/relation/termId ]
                    /termName = current()/termName )
         ]"/>
 </xsl:template>

</xsl:stylesheet>

Where the template exceludeNd will include a term of type Nd in the output, if and only if its relation term has not the same name.

The included file identity.xsl is just an external stylesheet containing the well known Identity Transform.

0

精彩评论

暂无评论...
验证码 换一张
取 消