I want to convert XPath value like Z.12.s from Z12s using xslt

馋奶兔 提交于 2021-02-05 12:16:35

问题


Please any one help me on converting XPath value to dot (full stop) separated.

E.g: Z12s to Z. 12.after 1st char need put dot and after 2 char dot then every 2 chars dot but not last value.


回答1:


If you don't know the length of the input string, you will need to use a recursive named template for this, such as:

<xsl:template name="split-string">
    <xsl:param name="string"/>
    <xsl:param name="length" select="1"/>
    <xsl:value-of select="substring($string, 1, $length)"/>
    <xsl:if test="string-length($string) > $length">
        <xsl:text>.</xsl:text>
        <!-- recursive call -->
        <xsl:call-template name="split-string">
            <xsl:with-param name="string" select="substring($string, $length + 1)"/>
            <xsl:with-param name="length" select="2"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

Demo: https://xsltfiddle.liberty-development.net/pNmC4HF




回答2:


If your address can be over 90 characters, add more if's to the AddressDetail template according the pattern that is in the template.

    <xsl:template match="AddressDetails">
      <xsl:copy>
        <xsl:apply-templates select="node() | @*">
          <xsl:with-param name="address" select="substring(Address, 1, 30)"/>
        </xsl:apply-templates>
      </xsl:copy>
      <xsl:if test="string-length(Address) &gt; 30">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*">
            <xsl:with-param name="address" select="substring(Address, 31, 30)"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:if>
      <xsl:if test="string-length(Address) &gt; 60">
        <xsl:copy>
          <xsl:apply-templates select="node() | @*">
            <xsl:with-param name="address" select="substring(Address, 61, 30)"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:if>
      <!-- etc. -->
    </xsl:template>

    <xsl:template match="Address">
      <xsl:param name="address"/>
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:value-of select="$address"/>
      </xsl:copy>
    </xsl:template>

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


来源:https://stackoverflow.com/questions/60520724/i-want-to-convert-xpath-value-like-z-12-s-from-z12s-using-xslt

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