问题
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) > 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) > 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