2015-12-02 38 views
0

我有以下語法作爲示例XML文檔:刪除值 - 第1版

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

我想要做的就是選擇串房間,但只返回「名稱1」和從字符串中刪除單詞「房間」。

這怎麼能在XSL 1中完成?

由於

回答

0

這可以是通過使用以下模板完成:

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

到位,正常使用選擇值:

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

一種可能的XSL 1.0 transormation,假設目標串"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> 

​​

+0

好一點,但爲我用它不能在一開始保證這仍是供將來參考有用的反應:) – seanbulley