Joachim Hollekim
Joachim Hollekim

Reputation: 39

XSL template match everything except something

I have a xsl with a snippet that looks like this:

<xsl:template match="group">
<xsl:element name="group">
<xsl:copy-of select="*"/>
</xsl:element> 
</xsl:template>

However, I need it to copy all groups except those with the typevalue "RECORD". How do I do this? I can use both XSL 1.0 and 2.0, with 1.0 preferred. The source file contains several thousand groups, with 4-5 different typevalues. I want the code to select only those groups without the typevalue RECORD.

Here's a example group that should be filtered away:

<group recstatus="1">
    <sourcedid>
      <source>system_owner_28f57240-5e2b-44af-8e62-fbf9aa6b6165</source>
      <id>basic_groups_69782a81-042d-4717-b9e4-18abacb306b7</id>
    </sourcedid>
    <grouptype>
      <scheme>Unique</scheme>
      <typevalue level="0">RECORD</typevalue>
    </grouptype>
    <description>
      <short>Ipsum dipsum</short>
    </description>
    <relationship>
      <sourcedid>
        <source>system_owner_28f57240-5e2b-44af-8e62-fbf9aa6b6165</source>
        <id>69782a81-042d-4717-b9e4-18abacb306b7</id>
      </sourcedid>
      <label>Ipsum dipsum</label>
    </relationship>
</group>

Many thanks for all and any help!

Upvotes: 0

Views: 897

Answers (2)

Michael Kay
Michael Kay

Reputation: 163360

Taking your question literally, the most direct XSLT representation of the requirement is to have two template rules:

<xsl:template match="*[everything()]" priority="10">
  ..
</xsl:template>

<xsl:template match="*[something()]" priority="20"/>

that is, a rule that matches "something" and does nothing, and a lower-priority rule that matches "everything" and does something.

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167601

If you use

<xsl:template match="/*">
  <xsl:copy>
    <xsl:copy-of select="//group[not(grouptype/typevalue[level = 0] = 'RECORD')]"/>
  </xsl:copy>
</xsl:template>

you copy those group elements not having that descendant typevalue being RECORD.

Or, if you want to use the identity transformation template to copy everything then reverse the condition to avoid copying the elements you want to eliminate with an empty template

<xsl:template match="group[grouptype/typevalue[level = 0] = 'RECORD']"/>

Upvotes: 1

Related Questions