从 XSLt 中的字符串中删除值 - 版本 1

Remove value from string in XSLt - Version 1

我有一个 XML 文档,其语法如下:

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

我想要做的是 select 字符串 ROOM 但只有 return "Name 1" 并从字符串中删除单词 "room"。

如何在 XSL 1 中完成此操作?

谢谢

一种可能的 XSL 1.0 转换,假设目标子字符串 "Room" 总是在开头或不存在:

<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

这可以使用以下模板完成:

     <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>

代替正常的select值,使用:

 <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" />