Remove value from string in XSLt - Version 1

I have an XML document with the following syntax as an example:

<EX1>
    <BUILDING>
        <ROOM> Room Name 1</ROOM>
    </BUILDING>
</EX1>

What I want to do is select the string ROOM but only return "Name 1" and remove the word "room" from the string.

How can this be done in XSL 1?

Thanks

Answers


One possible XSL 1.0 transormation, assuming that the target substring "Room" is always either at the beginning or not exists :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity template : copy element, unchanged -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- custom template to copy ROOM element and remove substring 'Room' from the inner text -->
<xsl:template match="ROOM[contains(.,'Room')]">
    <xsl:copy>
        <xsl:value-of select="normalize-space(substring-after(., 'Room'))"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

xsltranform demo


This can be done by using the following template:

     <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)" />
        <xsl:value-of select="$by" />
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text"
          select="substring-after($text,$replace)" />
          <xsl:with-param name="replace" select="$replace" />
          <xsl:with-param name="by" select="$by" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

In place of the normal select value of, use:

 <xsl:variable name="myVar">
                                            <xsl:call-template name="string-replace-all">
                                              <xsl:with-param name="text" select="ROOM" />
                                              <xsl:with-param name="replace" select="'Room'" />
                                              <xsl:with-param name="by" select="''" />
                                            </xsl:call-template>
                                          </xsl:variable>

                                          <xsl:value-of select="$myVar" />

Need Your Help

How to load JSON using ObjectMapper to map to a specific class structure?

java json jackson

Can I use Jackson ObjectMapper to load data from a JSON file directly into my own data structure? I'm trying to load a map of objects from a JSON file, but not sure how to do it.

Add network service

service wix install driver

I have a kernel service driver , i want to install it with an installer .