user2610593
user2610593

Reputation:

xslt object tree create structure

papa francesco rossi; figlio giuseppe rossi; nipote bartolomeo rossi

I would like to split this record by ";" and count ";" character

Can you give some instructions about this?

I remember that in visual basic exist SPLIT functions .... sound familar ?? thanks.

<familiari>papa francesco rossi; figlio giuseppe rossi; nipote bartolomeo rossi</familiari>

in output i would like:

; ; nipote bartolomeo rossi
this is my xml code:

<p>
   <FAMILIARI>papa francesco rossi; figlio giuseppe rossi; nipote bartolomeo rossi</FAMILIARI>
   <FAMILIARI>papa francesco rossi; figlio giuseppe rossi; nipote giuseppe contarino</FAMILIARI>
   <FAMILIARI>papa francesco rossi; figlio giuseppe rossi; nipote antonio mazzarino</FAMILIARI>
</p>

I would like this structure in output :

-papafrancesco rossi
--figlio giuseppe rossi
---nipote bartolomeo rossi
---nipote giuseppe contarino
---nipote antonio mazzarino

Upvotes: 0

Views: 70

Answers (1)

Troy Kroening
Troy Kroening

Reputation: 64

Building off of what @freefaller did for you with your previous question. Here is what I came up with:

<xsl:call-template name="getFinalText">
  <xsl:with-param name="text" select="text()"/>
  <xsl:with-param name="prev_text" select="preceding-sibling::familiari[1]"/>
  <xsl:with-param name="iteration" select="1"/>
</xsl:call-template>

Here is the template it calls.

<xsl:template name="getFinalText">
  <xsl:param name="text"/>
  <xsl:param name="prev_text"/>
  <xsl:param name="iteration"/>
  <xsl:choose>
    <xsl:when test="contains($text,';')">
      <xsl:if test="not(substring-before($text,';')=substring-before($prev_text,';'))">
        <xsl:call-template name="semicolon">
          <xsl:with-param name="count" select="0"/>
          <xsl:with-param name="iteration" select="$iteration"/>
        </xsl:call-template>
        <xsl:value-of select="substring-before($text,';')"/>
        <xsl:text xml:space="preserve">&#10;</xsl:text>
      </xsl:if>
      <xsl:call-template name="getFinalText">
        <xsl:with-param name="text" select="substring-after($text,';')"/>
        <xsl:with-param name="prev_text" select="substring-after($prev_text,';')"/>
        <xsl:with-param name="iteration" select="$iteration + 1"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="semicolon">
        <xsl:with-param name="count" select="0"/>
        <xsl:with-param name="iteration" select="$iteration"/>
      </xsl:call-template>
      <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

And here is the semicolon template.

<xsl:template name="semicolon">
  <xsl:param name="count"/>
  <xsl:param name="iteration"/>
  <xsl:if test="$count &lt; $iteration">
    <xsl:text>;</xsl:text>
    <xsl:call-template name="semicolon">
      <xsl:with-param name="count" select="$count + 1"/>
      <xsl:with-param name="iteration" select="$iteration"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

This worked for me on XML Playground. By the way, thank you @freefaller for mentioning it in the previous question. It was new to me and oh so useful.

Upvotes: 1

Related Questions