2014-07-09 31 views
0

我已經使用XSLT管理從XML轉換爲CSV。我只附加了XSLT的一部分,以使其更簡單。 我只是想添加功能,將取代「ConsentCode」的值如下:YES-> 1,NO-> 0,NOTSET->「」(空字符串)。 我認爲這可以通過使用「xsl:choose」功能來完成,但我沒有設法使其適應我的XSLT 您能否給我建議? 謝謝使用xsl替換XSLT中的值:選擇函數

這裏是我的XML

<SiebelMessage> 
    <ListOfSwiOrganizationIO> 
     <Account> 
      <Id>F-8LU</Id> 
      <PartyUId>A0A047</PartyUId> 
      <Email>[email protected]</Email> 
      <Name>DBEXT2</Name>   
      <ListOfIntegrityCode> 
       <IntegrityCode> 
        <IntegrityType>AllowSms</IntegrityType> 
        <ConsentCode>YES</ConsentCode> 
       </IntegrityCode> 
       <IntegrityCode> 
        <IntegrityType>AllowEmail</IntegrityType> 
        <ConsentCode>NO</ConsentCode> 
       </IntegrityCode> 
      </ListOfIntegrityCode>   
     </Account> 
    </ListOfSwiOrganizationIO> 
</SiebelMessage> 

這裏是我的XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:csv="csv:csv"> 
    <xsl:output method="text" encoding="utf-8"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:variable name="delimiter" select="';'"/> 

    <xsl:template match="/"> 
     <!-- Integrity Codes --> 
     <xsl:value-of select="concat(ListOfIntegrityCode/IntegrityCode[IntegrityType='AllowSms']/ConsentCode, $delimiter, ListOfIntegrityCode/IntegrityCode[IntegrityType='AllowEmail']/ConsentCode, $delimiter)"/> 
     <!-- end values --> 

     <xsl:if test="position()!=last()"> 
      <xsl:text>&#10;</xsl:text> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

在CSV所需的輸出: 1,0

+0

那麼是什麼'NOTSET->「」(空字符串)'意味着什麼?如果'ConsentCode'是空格,那麼你想要輸出一個空字符串,或者如果'ConsentCode'元素不存在,那麼你想輸出一個空字符串。通常我會定義一個映射和函數的變量,根據映射將每個輸入值映射到輸出值,然後您可以簡單地調用該函數。儘管存在'xsl:choose',XSLT/XPath 2.0也有'if(condition)then expression1 else expresssion2'條件表達式。 –

回答

0

好,如果你想與XSL做::選擇你可能會更好放置邏輯在一個名爲模板然後調用多次:

<xsl:template name="ConsentCode"> 
    <xsl:param name="code" /> 
    <xsl:choose> 
     <xsl:when test="$code = 'YES'">1</xsl:when> 
     <xsl:when test="$code = 'NO'">0</xsl:when> 
    </xsl:choose> 
</xsl:template> 

然後,將輸出的線路,像這樣

<xsl:call-template name="ConsentCode"> 
     <xsl:with-param name="code" select="ListOfIntegrityCode/IntegrityCode[IntegrityType='AllowSms']/ConsentCode" /> 
    </xsl:call-template> 
    <xsl:value-of select="$delimiter" /> 
    <xsl:call-template name="ConsentCode"> 
     <xsl:with-param name="code" select="ListOfIntegrityCode/IntegrityCode[IntegrityType='AllowEmail']/ConsentCode" /> 
    </xsl:call-template> 

在另一方面,如果你正在使用XSLT 2.0,您可以刪除的xsl:選擇並用這句話來代替

<xsl:value-of select="if ($code = 'YES') then '1' else (if ($code = 'NO') then '0' else '')" /> 

如果你模糊的編碼是你的事,你可以像這樣的代碼時,這將在兩個工作XSLT 1.0和XSLT 2.0

<xsl:value-of select="translate($code, 'YNESO', '10')" /> 
+0

謝謝你幫忙:) – user3804719