2

I need to number the nodes that contain data (xml) using xsl transformation. Nodes that are numbered, may have elements with different tag names. I was looking for a solution to this, but I could not find a proper way of numbering.

Here is a sample input file:

       <Contract>
        <Parametr1>
            <Interposes>
                   <Accomplice>
                    <Person>
                        <Name>John</Name>
                           <Surname>Person1</Surname>
                    </Person>
                   </Accomplice>
                <Representative>
                    <Name>George</Name>
                    <Surname>Person2</Surname>
                   </Representative>
            </Interposes>
            <Interposes>
                <Accomplice>
                    <Person>
                        <Name>Andy</Name>
                        <Surname>Person3</Surname>
                    </Person>
                </Accomplice>
            </Interposes>
            <Interposes>
                <Accomplice>
                    <Firm>
                        <FirmName>VeloDrom</FirmName>
                        <Description>Description</Description>
                    </Firm>
                </Accomplice>
                <Representative>
                    <Name>Agnes</Name>
                    <Surname>Person4</Surname>
                </Representative>
                <Representative>
                    <Name>Michael</Name>
                    <Surname>Person5</Surname>
                </Representative>
            </Interposes>
        </Parametr1>
       </Contract>

The correct result should look like this:

  1. George Person2 representing a person: John Person1
  2. Andy Person3
  3. Agnes Person4 representing a firm: VeloDrom Description
  4. Michael Person5 representing a firm: VeloDrom Description

My attempt to solve the (unsuccessful):

      <xsl:template match="Contract">
        <xsl:apply-templates select="Parametr1"/>
      </xsl:template>


      <xsl:template match="Parametr1">

          <xsl:apply-templates select="Interposes/Accomplice/Person"/>

          <xsl:apply-templates select="Interposes/Representative"/>

      </xsl:template>


      <xsl:template match="Interposes/Accomplice/Person | Interposes/Representative">
          <xsl:number format="1. " count="Interposes/Accomplice/Person | Interposes/Representative"/>
        <xsl:choose>
          <xsl:when test="count(./Interposes/Representative)=0">
            <xsl:apply-templates select="Interposes/Accomplice/Person"/>
          </xsl:when>
          <xsl:when test="count(./Interposes/Representative) &gt; 0">
            <xsl:text> </xsl:text>
              <xsl:value-of select="./Name"/><xsl:text> </xsl:text>
            <xsl:value-of select="./Surname"/>
            <xsl:if test="count(../Representative) &gt; 0">
              <xsl:if test="../Accomplice/Person">
                <xsl:text>, representing a person: </xsl:text>
                <xsl:value-of select="../Accomplice/Person/Name"/><xsl:text> </xsl:text>
                   <xsl:value-of select="../Accomplice/Person/Surname"/>
                 </xsl:if>
                 <xsl:if test="../Accomplice/Firm">
                   <xsl:text>, representing a firm: </xsl:text>
                   <xsl:value-of select="../Accomplice/Firm/FirmName"/><xsl:text> </xsl:text>
                   <xsl:value-of select="../Accomplice/Firm/Description"/>
                 </xsl:if>
               </xsl:if>
             </xsl:when>
           </xsl:choose>
       </xsl:template>

How to do it correctly?

1 Answer 1

1

Firstly, in the template that matches Parametr1 you should combine the two xsl:apply-templates into one.

<xsl:apply-templates select="Interposes[not(Representative)]/Accomplice
                             |Interposes/Representative"/>

In this case though, I have made a slight amendment to select the Accomplice element, and also only those which don't have a Representative

Doing this, then allows you to use the position() function to get the number. This will return the position of the various nodes (in document order) you have just selected.

To keep things tidy, I would have separate templates for Accomplice and Representative though....

<xsl:template match="Accomplice">
   <xsl:value-of select="position()"/>
   <xsl:text>. </xsl:text>
   <!-- Select either Person or Firm -->

<xsl:template match="Representative">
   <xsl:value-of select="position()"/>
   <xsl:text>. </xsl:text>
   <xsl:value-of select="concat(Name, ' ', Surname)"/>

Note you don't need full path to the element names in the match here. Also note you don't need the condition on the Accomplice in this case, as it will be selecting those elements where the condition is true.

It should now become a bit more straight-forward, with the only extra work being in the Representative template, to choose whether they are representing a firm or a person

  <xsl:choose>
     <xsl:when test="../Accomplice/Person">
        <xsl:text>, representing a person: </xsl:text>
     </xsl:when>
     <xsl:otherwise>
        <xsl:text>, representing a firm: </xsl:text>
     </xsl:otherwise>
  </xsl:choose>

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text"/>

   <xsl:template match="Parametr1">
      <xsl:apply-templates select="Interposes[not(Representative)]/Accomplice|Interposes/Representative"/>
   </xsl:template>

   <xsl:template match="Accomplice">
      <xsl:value-of select="position()"/>
      <xsl:text>. </xsl:text>
      <!-- Select either Person or Firm -->
      <xsl:apply-templates select="*"/>
   </xsl:template>

   <xsl:template match="Representative">
      <xsl:value-of select="position()"/>
      <xsl:text>. </xsl:text>
      <xsl:value-of select="concat(Name, ' ', Surname)"/>
      <xsl:choose>
         <xsl:when test="../Accomplice/Person">
            <xsl:text>, representing a person: </xsl:text>
         </xsl:when>
         <xsl:otherwise>
            <xsl:text>, representing a firm: </xsl:text>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="../Accomplice/*"/>
   </xsl:template>

   <xsl:template match="Person">
      <xsl:value-of select="concat(Name, ' ', Surname)"/>
      <xsl:value-of select="'&#10;'"/>
   </xsl:template>

   <xsl:template match="Firm">
      <xsl:value-of select="concat(FirmName, ' ', Description)"/>
      <xsl:value-of select="'&#10;'"/>
   </xsl:template>
</xsl:stylesheet>

Notice the use of templates matching Person and Firm to allow them to be used in both the Accomplice and Representative templates.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.