2012-04-25 38 views
1

我正在使用XSLT。如何在不移除xslt中的內容的情況下刪除某些標記並將整個內容XML作爲輸入傳遞給另一個模板

源XML:

  <?xml version="1.0" encoding="iso-8859-1"?> 
      <Content> 
       <alertHeader> 


       <ol xmlns="http://www.w3.org/1999/xhtml"> 
        <li> 
        <strong>Review</strong> 
        your current available balance. It can be obtained 24 hours a day, 7 days a week through 
        <a href="/ALL_UNDER_123">Account Activity</a> 
        any 123 ATM or by calling 
        <a id="dynamicvariable" href="#" name="Customercare">[Customercare]</a> 
        at 
        <a id="dynamicvariable" href="#" name="contactNo">[contactNo]</a> 
        . 
        </li> 
        <li> 
        <strong>Take into consideration</strong> 


        <ul> 
         <li>Please get in touch with us</li> 
         <li>Please consider this as important info</li> 
        </ul> 


        </li> 
        <li> 
        <strong>Current</strong> 
        Statementt doesnot match the requirement 
        <a id="dynamicvariable" href="#" name="Actual acccount">[Actual acccount]</a> 
        ,plus 
        <a id="dynamicvariable" href="#" name="totalcharges">[totalcharges]</a> 
        Make u r response as positive. 
        </li> 
       </ol> 
       <p xmlns="http://www.w3.org/1999/xhtml"></p> 
       <div xmlns="http://www.w3.org/1999/xshtml"></div> 
       <span xmlns="http://www.w3.org/1999/xshtml"></span> 



       </alertHeader> 
      </Content> 

我想寫一個XSLT在標籤alertHeader傳遞全部內容作爲值到另一個模板。

我想修改這段代碼如下。

需要
    1.Remove the tags <p></p>, and <div></div>,<span></span> and <a></a>. I want to remove only tags but not the value of the tags. It should be there as it is. 
        2.Pass the content including tags to "Process" template. 

輸出:

  <aaa> 
      <text> 

      <ol xmlns="http://www.w3.org/1999/xhtml"> 
       <li> 
        <strong>Review</strong> 
        your current available balance. It can be obtained 24 hours a day, 7 days a week through 
       <dynamicvariable name="Customercare"/> 

       at 
       <dynamicvariable name="contactNo"/> 

       . 
       </li> 
       <li> 
        <strong>Take into consideration</strong> 


        <ul> 
         <li>Please get in touch with us</li> 
         <li>Please consider this as important info</li> 
        </ul> 


        </li> 
       <li> 
       <strong>Current</strong> 
        Statementt doesnot match the requirement 
       </li> 
      </ol> 
      </text> 
      </aaa> 

當前XSLT:

   <?xml version="1.0" encoding="utf-8"?> 
       <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
       <xsl:output method="xml" indent="yes"/> 


       <xsl:template match="alertHeader"> 
        <xsl:call-template name="process"> 

        <xsl:with-param name="text" select="." /> 


        </xsl:call-template> 

       </xsl:template> 
       <xsl:template name="process"> 
        <xsl:param name="text" /> 

        <xsl:variable name="head" select="substring-before($text, '[')" /> 
        <xsl:variable name="tag" select="substring-before(substring-after($text, '['), ']')" /> 
        <xsl:variable name="tail" select="substring-after($text, ']')" /> 

        <xsl:choose> 
        <xsl:when test="$head != '' and $tag != ''"> 
         <xsl:value-of select="$head" /> 
         <dynamicVariable name="{$tag}" /> 
         <!-- recursive step: process the remainder of the string --> 
         <xsl:call-template name="process"> 
         <xsl:with-param name="text" select="$tail" /> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:value-of select="$text" /> 
        </xsl:otherwise> 
        </xsl:choose> 
       </xsl:template> 

       </xsl:stylesheet> 

任何一個可以說什麼需要我的代碼的所有變化。

謝謝。

回答

0

xslt-1.0中的問題是您無法訪問結果節點或節點集,因爲在沒有XPath或函數或任何引用模板轉換結果的地方。

是不是很重要,您現在致電process?我會去了解這個問題的方法是使process該轉換在一個節點模板,然後從每個節點調用它下面alertHeader,如:

<xsl:template match="alertHeader"> 
    <!-- switch modes so we know we need to call process --> 
    <xsl:apply-templates mode="callproc"/> 
</xsl:template> 

<xsl:template match="*" mode="callproc"> 
    <!-- call process on THIS node --> 
    <xsl:call-template name="process"> 
    <xsl:with-param name="text" select="."/> 
    </xsl:call-template> 
    <!-- descend --> 
    <xsl:apply-templates mode="callproc"/> 
</xsl:template> 

<!-- all the stuff that needs stripping --> 
<xsl:template match="p|div|span|a" mode="callproc"> 
    <!-- call process on the text() portion --> 
    <xsl:call-template name="process"> 
    <xsl:with-param name="text" select="text()"/> 
    </xsl:call-template> 
    <!-- no descent here --> 
</xsl:template> 

更新: 沒有process模板

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xh="http://www.w3.org/1999/xhtml" 
    xmlns:xsh="http://www.w3.org/1999/xshtml"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="Content"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="alertHeader"> 
    <xsl:element name="aaa"> 
     <xsl:element name="text"> 
     <!-- switch modes so we know we need to call process --> 
     <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:copy> 
     <!-- descend --> 
     <xsl:apply-templates/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- all the stuff that needs stripping --> 
    <xsl:template match="xh:p|xh:div|xh:span|xsh:div|xsh:span"> 
    <xsl:apply-templates/> 
    </xsl:template> 

    <!-- was process --> 
    <xsl:template match="xh:a[@name]"> 
    <xsl:element name="dynamicvariable" namespace="http://www.w3.org/1999/xhtml"> 
     <xsl:attribute name="name"> 
     <xsl:value-of select="@name"/> 
     </xsl:attribute> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="xh:a"/> 

    <xsl:template match="text()"> 
    <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 

輸出:

   <ol xmlns="http://www.w3.org/1999/xhtml"> 
      <li> 
       <strong>Review</strong> 
       your current available balance. It can be obtained 24 hours a day, 7 days a week through 

       any Wells Fargo ATM or by calling 
       <dynamicvariable name="Call_Center_Name"/> 
       at 
       <dynamicvariable name="Phone_Number"/> 
       . 
      </li> 
      <li> 
       <strong>Take into account</strong> 


       <ul> 
        <li>Your pending transactions and any additional transactions that have not yet been deducted from your available balance, such as checks you have written or upcoming scheduled automatic payments.</li> 
        <li>Any transactions that have been returned because you did not have enough money in your account at that time; they may be resubmitted for payment by the person or party who received the payment from you</li> 
       </ul> 


      </li> 
      <li> 
       <strong>Deposit</strong> 
       enough money to establish and maintain a positive account balance. A deposit of at least 
       <dynamicvariable name="Absolute_Available_Balance"/> 
       ,plus 
       <dynamicvariable name="Total_Fee"/> 
       in fees, would have been required to make your account balance positive at the time we sent this notice. 
      </li> 
       </ol> 






    </text> 
</aaa> 
+0

我沒有得到的願望輸出在此。 – Patan 2012-04-25 07:54:24

+0

不錯,你必須相應地重寫'process' – hroptatyr 2012-04-25 07:59:22

+0

我想在輸出中獲取標籤。 – Patan 2012-04-25 08:01:56

2

無需對XML文檔進行任何初始處理,然後將其傳遞給流程模板。從查看你的問題,看起來你有一個要求(你沒有明確提到)將文本中的'標籤'轉換爲dynamicVariable元素,如表單'[Total_Fee]'。

所以,你需要做的是首先有一個模板來忽略你選擇的節點,但是繼續匹配它們中的元素和文本。

<xsl:template match="Content|alertHeader|xhtml:p|xshtml:div|xshtml:span|xhtml:a"> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:template> 

請注意這裏的複雜之處在於,你已經爲你的一些節點的定義不同的命名空間(而這些將有XSLT文檔中聲明)。如果您有多個名稱空間,則還可以執行以下操作。

<xsl:template match="Content|alertHeader|*[local-name() = 'p']|*[local-name() = 'span']|*[local-name() = 'div']|*[local-name() = 'a']"> 

沒有命名空間,你可以做以下

<xsl:template match="Content|alertHeader|p|div|span|a"> 

接下來,你叫過程模板可以被組合,以匹配文本()元素

<xsl:template match="text()" name="process"> 

這使得它匹配文本元素,遞歸調用自己在文本內查找標籤。

以下是完整的XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xhtml="http://www.w3.org/1999/xhtml" 
    xmlns:xshtml="http://www.w3.org/1999/xshtml" 
    exclude-result-prefixes="xhtml xshtml"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="Content|alertHeader|xhtml:p|xshtml:div|xshtml:span|xhtml:a"> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:template> 

    <xsl:template match="text()" name="process"> 
     <xsl:param name="text" select="." /> 

     <xsl:choose> 
     <xsl:when test="contains($text, ']') and contains($text, '[')"> 
      <xsl:value-of select="substring-before($text, '[')"/> 
      <dynamicVariable name="{substring-before(substring-after($text, '['), ']')}"/> 
      <xsl:call-template name="process"> 
       <xsl:with-param name="text" select="substring-after($text, ']')"/> 
      </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$text"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="@*|*"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

當適用於您的XML,下面是輸出

<ol xmlns="http://www.w3.org/1999/xhtml"> 
<li> 
<strong>Review</strong>      your current available balance. It can be obtained 24 hours a day, 7 days a week through      Account Activity      any Wells Fargo ATM or by calling      <dynamicVariable name="Call_Center_Name" xmlns="" />      at      <dynamicVariable name="Phone_Number" xmlns="" />      .     </li> 
<li> 
<strong>Take into account</strong> 
<ul> 
<li>Your pending transactions and any additional transactions that have not yet been deducted from your available balance, such as checks you have written or upcoming scheduled automatic payments.</li> 
<li>Any transactions that have been returned because you did not have enough money in your account at that time; they may be resubmitted for payment by the person or party who received the payment from you</li> 
</ul> 
</li> 
<li> 
<strong>Deposit</strong>      enough money to establish and maintain a positive account balance. A deposit of at least      <dynamicVariable name="Absolute_Available_Balance" xmlns="" />      ,plus      <dynamicVariable name="Total_Fee" xmlns="" />      in fees, would have been required to make your account balance positive at the time we sent this notice.     </li> 
</ol> 
+0

其實我想刪除這些標籤,例如 Patan 2012-04-25 08:47:39

+0

要刪除的任何標籤,只需將其添加到排除列表以及p,span,div和已列出的標籤即可。 – 2012-04-25 08:56:56

+0

我已更新我的XSLT以排除內容和alertHeader – 2012-04-25 11:03:56

相關問題