XSLT String Parsing

I'm new to XSLT for "PrimarySubject" below I need to replace the "&" characters to% 26 and the '' characters in% 20 that it contains.

<xsl:template name="BuildLink"> 
    <xsl:param name="PrimarySubject" /> 
    <xsl:text>?PrimarySubject=</xsl:text> 
    <xsl:value-of select="$PrimarySubject" /> 
</xsl:template> 

Is there a string replacement function that I can use in xslt version 1? Thank,

+3
source share
2 answers

This can be done most easily using the FXSL Library , or rather it . str-map

Here is a simple example .

This conversion is:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:testmap="testmap"
exclude-result-prefixes="xsl testmap"
>
   <xsl:import href="str-map.xsl"/>

   <!-- to be applied on any xml source -->

   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

   <xsl:template match="/">
     <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="'abc&amp;d f'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template match="testmap:*">
      <xsl:param name="arg1"/>

      <xsl:choose>
       <xsl:when test="$arg1 = '&amp;'">%26</xsl:when>
       <xsl:when test="$arg1 = ' '">%20</xsl:when>
       <xsl:otherwise><xsl:value-of select="$arg1"/></xsl:otherwise>
      </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

, if applied to any XML document (not used), creates the desired result:

abc%26d%20f

+3
source

XSLT has several substrings :

  • string substring-before (string, string)
  • string substring-after (, )
  • (, , ?)
0

Source: https://habr.com/ru/post/1736502/


All Articles