Concat Strings to numbers using Recursive

萝らか妹 提交于 2019-12-06 07:21:54
Dimitre Novatchev

Here is a simpler, non-recursive solution, using the Piez method:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="mphantom">
     <math>
     <xsl:variable name="vCur" select="."/>
      <xsl:for-each select=
       "(document('')//node()|document('')//@*|document('')//namespace::*)
          [not(position() > string-length($vCur))]
       ">
        <xsl:value-of select="concat('|', substring($vCur, position(), 1),'|')"/>
      </xsl:for-each>
     </math>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<math>
    <mn>
        <mphantom>12</mphantom>
    </mn>
</math>

the wanted, correct result is produced:

<math>|1||2|</math>

II. For completeness, here is an XSLT 2.0 solution:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="mphantom">
     <math>
       <xsl:value-of separator="" select=
        "for $i in 1 to string-length()
          return
             concat('|', substring(., $i, 1), '|')
        "/>
     </math>
 </xsl:template>
</xsl:stylesheet>

Try this..

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="mn[mphantom]" name="phant">
  <xsl:param name="chars" select="mphantom" />
  <xsl:value-of select="concat('|phantom',substring($chars,1,1),'|')" />
  <xsl:if test="substring($chars,2)">
    <xsl:call-template name="phant">
      <xsl:with-param name="chars" select="substring($chars,2)" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!