2016-12-15 202 views
1

我的要求是如下 1)更換空間%20 2)更換/用%2F 3)空元素具有值作爲虛設XSLT替換空的節點與虛擬

樣品輸入:

<?xml version="1.0" encoding="UTF-8" ?> 
    <process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1"> 
    <Sender>Sender 1</Sender> 
    <TransactionId>TransactionId/2</TransactionId> 
    <TransactionType>TransactionType5</TransactionType> 
    <Status>Status6</Status> 
    <Limit>70.73</Limit> 
    <Remarks>Remarks8</Remarks> 
    <Result>GlobalResult9</Result> 
    <Type>DecisionType10</Type> 
    <DecidedBy>DecidedBy11</DecidedBy> 
     <AddRequest1/> 
    <AddRequest2>RAMA</AddRequest2> 
    </process> 

所需的輸出:

<process xmlns="http://xmlns.oracle.com/CCS/Project5/BPELProcess1"> 
<Sender>Sender%201</Sender> 
<TransactionId>TransactionId%2F2</TransactionId> 
<TransactionType>TransactionType5</TransactionType> 
<Status>Status6</Status> 
<Limit>70.73</Limit> 
<Remarks>Remarks8</Remarks> 
<Result>GlobalResult9</Result> 
<Type>DecisionType10</Type> 
<DecidedBy>DecidedBy11</DecidedBy> 
<AddRequest1>DUMMY</AddRequest1> 
<AddRequest2>RAMA</AddRequest2> 
</process> 

我曾嘗試以下XSLT,但一直沒找到笏更換空節點虛擬 如

<AddRequest1></AddRequest1> is not coverted to   <AddRequest1>DUMMY</AddRequest1> 

XSLT試圖爲如下

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="@*|node()"> 
<xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:copy> 
    </xsl:template> 
    <xsl:template match="//text()"> 
    <xsl:call-template name="rep_SPLChar"> 
    <xsl:with-param name="text" select="."/> 
    </xsl:call-template> 
     </xsl:template> 

    <xsl:template name="rep_SPLChar"> 
    <xsl:param name="text"/> 
    <xsl:variable name="temp_space" select="'%20'"/> 
    <xsl:variable name="temp_backslash" select="'%2F'"/> 
    <xsl:if test="normalize-space($text) != ''"> 
     <xsl:choose> 
     <xsl:when test="contains($text,' ')"> 
      <xsl:call-template name="rep_SPLChar"> 
      <xsl:with-param name="text" 
      select="concat((concat(substring-before($text,'   '),$temp_space)),substring-after($text,' '))"/> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:choose> 
     <xsl:when test="contains($text,'/')"> 
      <xsl:call-template name="rep_SPLChar"> 
      <xsl:with-param name="text" 
          select="concat((concat(substring- before($text,'/'),$temp_backslash)),substring-after($text,'/'))"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
     </xsl:choose> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:if> 
    </xsl:template> 
    </xsl:stylesheet> 

回答

2

嘗試添加該模板應用到XSLT來匹配 「空」 的元素

<xsl:template match="*[not(*)][not(normalize-space())]"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*"/> 
    <xsl:text>DUMMY</xsl:text> 
    </xsl:copy> 
</xsl:template> 
+0

非常感謝蒂姆C中,它解決了我的問題 –