Eric C.
Eric C.

Reputation: 65

Retain unmatched nodes in apply templates

I have a requirement to sort records based on a field (account) only if the another field (depositvalue)is not blank. I am able to do this but my current xslt is removing unmatched nodes in apply templates. How can always leave everything else untouched in this case. The record to be sorted (DirectDeposit) may not always be there for some Employees.

I already made progress on the sorting.

XSLT:

   <xsl:stylesheet version = '1.0' 
   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Employee">
    <Employee>
        <xsl:apply-templates select="DirectDeposit[not(depositvalue = '')]" >
            <xsl:sort select="account"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="DirectDeposit[depositvalue = '']"/>
    </Employee>
</xsl:template>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

actual result: (employee header is gone)

<?xml version="1.0"?>
 <Hire>
 <Employee>
 <DirectDeposit>
  <action>DirectDeposit</action>
  <created_by/>
  <created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
  <date_of_birth>1985-04-09</date_of_birth>
  <account>2</account>
  <depositvalue>200</depositvalue>
 </DirectDeposit>
 <DirectDeposit>
  <action>DirectDeposit</action>
  <created_by/>
  <created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
  <date_of_birth>1985-04-09</date_of_birth>
  <account>3</account>
  <depositvalue>100</depositvalue>
 </DirectDeposit>
 <DirectDeposit>
  <action>DirectDeposit</action>
  <created_by/>
  <created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
  <date_of_birth>1985-04-09</date_of_birth>
  <account>1</account>
  <depositvalue/>
 </DirectDeposit>
 </Employee>
  <Employee/>
  </Hire>

Expected:

    <?xml version="1.0"?>
    <Hire>
  <Employee>
    <Header>
        <action/>
        <created_by/>
        <created_on_timestamp>2018-07- 
                 13T19:46:58.000Z</created_on_timestamp>
        <date_of_birth>1985-04-09</date_of_birth>
        <AdditionalElements/>
         </Header>
        <DirectDeposit>
        <action>DirectDeposit</action>
        <created_by/>
        <created_on_timestamp>2018-07- 
                   13T19:46:58.000Z</created_on_timestamp>
        <date_of_birth>1985-04-09</date_of_birth>
        <account>2</account>
        <depositvalue>200</depositvalue>
       </DirectDeposit>
         <DirectDeposit>
        <action>DirectDeposit</action>
        <created_by/>
        <created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
        <date_of_birth>1985-04-09</date_of_birth>
        <account>3</account>
        <depositvalue>100</depositvalue>
    </DirectDeposit>
    <DirectDeposit>
        <action>DirectDeposit</action>
        <created_by/>
        <created_on_timestamp>2018-07-13T19:46:58.000Z</created_on_timestamp>
        <date_of_birth>1985-04-09</date_of_birth>
        <account>1</account>
        <depositvalue/>
    </DirectDeposit>
</Employee>
<Employee/>

Upvotes: 0

Views: 131

Answers (1)

Michael Kay
Michael Kay

Reputation: 163360

Add

<xsl:apply-templates select="Header"/>

to your match="Employee" template. If you don't process the Header element, it won't be processed, and will therefore be absent from the output.

Upvotes: 1

Related Questions