Counting nodes with the BizTalk mapper and custom XSLT

By patrickwellink
March 27, 2006
Comments Off on Counting nodes with the BizTalk mapper and custom XSLT

Ok here is something quite simple, but when I Googled for it i could not find anu solution to this problem. Here is the case. I have a docuemnt that looks like this….

<SomeRoot>
  <Header>
    <NumberOfChildren>??</NumberOfChildren>
    <Child>
      <SomeData>Data A</SomeData>
    </Child>
    <Child>
      <SomeData>Data B</SomeData>
    </Child>
  </Header>
  <Header>
    <NumberOfChildren>??</NumberOfChildren>
    <Child>
      <SomeData>Data B</SomeData>
    </Child>
  </Header>
</SomeRoot>

If I would use the BizTalk COUNT functoid it would give me :

<SomeRoot>
  <Header>
    <NumberOfChildren>3</NumberOfChildren>
    <Child>
      <SomeData>Data A</SomeData>
    </Child>
    <Child>
      <SomeData>Data B</SomeData>
    </Child>
  </Header>
  <Header>
    <NumberOfChildren>3</NumberOfChildren>
    <Child>
      <SomeData>Data B</SomeData>
    </Child>
  </Header>
</SomeRoot>

i.e.  BizTalk is always counting the nodes from the root of the document. I needed to know the number childeren within each recurring node. It turned out that i had to use some small XSLT.
I used the iteration functoid to give an indication of the current node I was in. I needed to capture this as a variable in my XSLT. Here is what the map looks like.

 

The Scripting functoid looks like this (I use an Inline XSLT template)

The complete XSLT looks like this (there is one line missing in this screenshot)

<xsl:template name="MyXsltCountTemplate">
<xsl:param name="Iteration" />

<xsl:for-each select="/SomeRoot/Header">
   <xsl:if test="position()=$Iteration">
      <xsl:element name="NumberOfChildren">
      <xsl:value-of select="count(./Child)" />
     </xsl:element>
   </xsl:if> 
</xsl:for-each>
</xsl:template>

And with this little script I get what I want the result of a map would give me the desired results.

<SomeRoot>
  
<Header>
      <
NumberOfChildren>2</NumberOfChildren>
      <
Child>
         
<SomeData>Data A</SomeData>
      </Child>
     
<Child>
        
<SomeData>Data B</SomeData>
     
</Child>
   </
Header>
   <
Header>
      <
NumberOfChildren>1</NumberOfChildren>
      <
Child>
        
<SomeData>Data B</SomeData>
     
</Child>
   </
Header>
</
SomeRoot>

 

 

 

 

Comments: 0

Comments are closed.

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta