Two/multi phases XSLT processingsteemCreated with Sketch.

in programming •  7 years ago 

Two/multi phases XSLT is very useful if you do have multiple steps to process your data. Here is an example.

I have a data dictionary to store authors:

<?xml version="1.0" encoding="UTF-8"?>
<authors>
  <author>Wyatt, T.</author>
  <author>Smith, A.</author>
  <author>Davids, D.</author>
</authors>

Everytime when I've got a new XML document, I need to check a particular element (fullName) to update data dictionary file. The XML document looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
  <creator>
    <fullName>Wyatt, A.</fullName>
  </creator>
  ...
</doc>

While updating data dictionary, I need to make sure there is no duplicate getting into data dictionary.

Multi-phase processing can be used to do this job. The key point for multiphase processing is to store the previous processing result into a variable and use that variable as the initial XML to process in the later phase. The example XSLT is shown below:

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

  <xsl:variable name="authorsDoc" select="document('authors.xml', /)"/>

  <xsl:template match="/">
    (html comment removed:  Phase 1 )
    <xsl:variable name="phase-1-output">
      <authors>
        (html comment removed:  If the newly entered author does not exist in current data dictionary,
               then add it to DD )
        <xsl:for-each select="/doc/creator/fullName[.!='']">
            <xsl:variable name="currentName" select=".[.=$authorsDoc//author]"/>
            <xsl:if test="not($currentName)">
              <author><xsl:value-of select="."/></author>
            </xsl:if>
        </xsl:for-each>

        (html comment removed:  Copy all existing DD authors, only once )
        <xsl:for-each select="$authorsDoc/authors/author[.!='']">
          <xsl:sort select="."/>
          <author><xsl:value-of select="."/></author>
        </xsl:for-each>
      </authors>
    </xsl:variable>

   (html comment removed:  Phase 2 )
    <authors>
      <xsl:for-each select="$phase-1-output/authors/author[.!='' and (not(.=preceding-sibling::author))]">     (html comment removed:  Remove duplicates )
        <xsl:sort select="."/>
        <author><xsl:value-of select="."/></author>
      </xsl:for-each>
    </authors>

  </xsl:template>

</xsl:stylesheet>
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Interesting

This post has received a 6.25 % upvote from @booster thanks to: @yuxid.

This post has been ranked within the top 80 most undervalued posts in the second half of Nov 09. We estimate that this post is undervalued by $11.99 as compared to a scenario in which every voter had an equal say.

See the full rankings and details in The Daily Tribune: Nov 09 - Part II. You can also read about some of our methodology, data analysis and technical details in our initial post.

If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.