2017-09-13 23 views
1

我需要找到一種方法來應對以下XML的空格和換行:在XSLT使用空格和換行處理1

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<Message.body> 
<Text>This is 
    an attachment text 
    with whitespaces and linebreaks 
    File name: https://somerandomurl.com/123.txt 
    File Type: txt 
</Text> 
</Message.body> 

而這裏的XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:template match="/"> 
<root> 
    <xsl:choose> 
      <xsl:when test="(starts-with(Message.body/Text, 'This is an attachment link:'))" /> 
     <xsl:otherwise> 
      xxx 
     </xsl:otherwise> 
    </xsl:choose> 
    <Name> 
     <xsl:value-of select="substring-before(substring-after(Message.body/Text, 'File Name: '), 'File Type: ')"/> 
    </Name> 
    <Type> 
     <xsl:value-of select="substring-after(Message.body/Text, 'File Type: ')"/> 
    </Type> 
</root> 

這部分:

<xsl:when test="(starts-with(Message.body/Text, 'This is an attachment link:'))" /> 

當然不會工作,因爲中間有空格的換行符。此外,下面的線不與期望的結果,由於提到的問題:

實際結果:

<Name>https://somerandomurl.com/123.txt 
    </Name> 
<Type>txt 
    </Type> 

期望的結果:

<Name>https://somerandomurl.com/123.txt</Name> 
<Type>txt</Type> 

我應該嘗試用空格替換所有換行符,或者XSLT 1有沒有更簡單的方法?

在此先感謝!

回答

1

你應該能夠使用normalize-space()解決這兩個問題...

<xsl:template match="/"> 
    <root> 
    <xsl:choose> 
     <xsl:when test="starts-with(normalize-space(Message.body/Text), 'This is an attachment link:')" /> 
     <xsl:otherwise> 
     xxx 
     </xsl:otherwise> 
    </xsl:choose> 
    <Name> 
     <xsl:value-of select="normalize-space(substring-before(substring-after(normalize-space(Message.body/Text), 'File name: '), 'File Type: '))"/> 
    </Name> 
    <Type> 
     <xsl:value-of select="normalize-space(substring-after(Message.body/Text, 'File Type: '))"/> 
    </Type> 
    </root> 
</xsl:template> 

注意,在子,後子,前字符串比較是區分大小寫的,所以我不得不改變File Name:File name:到匹配你的輸入。

+0

非常感謝! :) – CoffeeCups