2014-03-14 162 views
1

我有以下XML文件。將粗體標記中的文字用粗體表示

<xml> 
<chapter> 
    <p> 
    <L-1> 
     This is a sample text. I want to <E type='bold'>display this text in bold.<E> This is good. 
    </L-1> 
    </p> 
    <figure> 
    </figure> 
</chapter> 
</xml> 

現在我想寫一個xslt,它會使標籤內的文本變成粗體。

所需的輸出:

This is a sample text. I want to **display this text in bold.** This is good. 

我寫XSL-FO輸出。 XSLT

<xml> 
    <xsl:template match="node()" mode="chapter"> 
    <xsl:for-each select="node()"> 
      <xsl:if test="current()[name() = 'P']"> 
      <xsl:apply-templates select="current()[name() = 'P']" 
       mode="p" /> 
     </xsl:if> 
    </xsl:for-each> 
    </xsl:template> 
    <xsl:template match="node()[name() = 'P']" mode="p"> 
    <xsl:if test="current()/node()[name() = 'L-1']"> 
     <xsl:apply-templates select="current()/node()[name() = 'L-1']" 
      mode="l1" /> 
    </xsl:if> 
</xsl:template> 

    <xsl:template match="node()[name() = 'L-1']" mode="l1"> 
    <fo:block> 
     <xsl:value-of select="current()" /> 
    </fo:block> 
      <xsl:if test="current()/node()[name() = 'E']"> 
     <xsl:apply-templates select="current()/node()[name() = 'E']" 
      mode="e" /> 
    </xsl:template> 

<xsl:template match="node()[name() = 'E']" mode="e"> 
    <fo:block font-weight="bold"> 
     <xsl:value-of select="current()" /> 
    </fo:block> 
</xsl:template> 
</xml> 

闡釋: 我不得不實現遞歸方法到每個節點遍歷。這個遍歷必須是動態的。因爲在任何時候我都不知道我會得到什麼樣的xml。在高層次上,我知道節點結構。我有定義了所有節點的xsd和父節點中可以存在的子節點。因此,我的xslt在遞歸中運行,以檢查哪個是當前節點,並基於此我需要對其應用樣式。

現在有了上面的xslt,一旦遇到'E'標籤,E標籤後面的文本就會出現兩次。

電流輸出: 這是一個示例文本。我想以粗體顯示此文本。這很好。 以粗體顯示此文本。

請給我建議。

+0

您是否有XSLT來轉換您的XML?如果你這樣做,在這裏發佈。您只需添加一個簡單模板即可進行轉換。 – helderdarocha

回答

0

假設你已經有一個XSLT樣式表,它正在改變文本,並已處理該特定節點上,您可以添加該模板沒有模板(的E所有字符的字符串匹配包含bold一個type屬性)將取代比賽含fo:block一個font-weight="bold"屬性:

<xsl:template match="E[@type='bold']"> 
    <fo:block font-weight="bold"><xsl:value-of select="."/></fo:block> 
</xsl:template> 

如果沒有XSLT模板都和上面的文字是你想要的,那麼你可以使用這個樣式表,這將產生一個最小的XSL-FO文件,其中包含你的轉型想要:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/XMLFormat" 
    version="1.0"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 
    <xsl:template match="/"> 
     <fo:root> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="p1"> <fo:region-body/> 
       </fo:simple-page-master> 
      </fo:layout-master-set> 
      <fo:page-sequence master-name="p1"> 
       <fo:flow flow-name="xsl-region-body"> 
        <xsl:apply-templates/> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 

    <xsl:template match="E[@type='bold']"> 
     <fo:block font-weight="bold"><xsl:value-of select="."/></fo:block> 
    </xsl:template> 

</xsl:stylesheet> 
+0

嗨..我試了一下..這是越來越大膽。但E標籤之後的文本沒有顯示。請幫忙。我已經添加了我的xslt。 – Chai