How do I number the nodes, if they are sometimes different elements



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?


No comments:

Post a Comment