xslt对象树创建结构

时间:2013-11-14 15:24:45

标签: xslt xslt-1.0

papa francesco rossi; figlio giuseppe rossi; nipote bartolomeo rossi

我想将此记录拆分为“;”并计算“;”字符

你能就此提供一些指示吗?

我记得在Visual Basic中存在SPLIT功能....声音熟悉?感谢。

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

在输出中我想:

; nipote bartolomeo rossi
    这是我的xml代码:

<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>

我想在输出中使用这个结构:

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

1 个答案:

答案 0 :(得分:1)

利用previous question建立@freefaller为您做的事情。以下是我提出的建议:

<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>

这是它调用的模板。

<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>

这是分号模板。

<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>

这适用于XML Playground。顺便说一句,谢谢@freefaller在前一个问题中提到它。这对我来说是新的,非常有用。

相关问题