2009-09-19 176 views
3

定期出現的標籤我有一個xml文檔,我正在使用xslt轉換爲xsl-fo文檔。我有這個棘手的問題我一直在試圖尋找一個解決方案,很長一段時間......XSLT轉換替換通過文檔

在我的源XML我有整個穿插了少量的標籤。我想將它們格式化爲生成文檔中的下劃線,但是我無法這樣做。

我用這樣的代碼嘗試:

<xsl:template match="//em"> 
    <fo:inline text-decoration="underline"> 
    <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
</xsl:template> 

完整的XSLT看起來是這樣的:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    version="1.0"> 



    <!-- match em tags --> 
    <xsl:template match="//em"> 
    <fo:inline text-decoration="underline"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 
    <xsl:template match="//u"> 
    <fo:inline text-decoration="underline"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 

    <!-- match b tags --> 
    <xsl:template match="//b"> 
    <fo:inline font-weight="bold"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 

    <xsl:template match="//br"> 
    <fo:block><xsl:text>&#xA;</xsl:text></fo:block> 
    </xsl:template> 

    <xsl:template match="briefs"> 
    <fo:root> 
     <fo:layout-master-set> 
     <fo:simple-page-master master-name="Evidence" page-width="8.5in" page-height="11in" margin="1in"> 
      <fo:region-body margin-bottom=".5in" margin-top=".5in" region-name="xsl-region-body" /> 
      <fo:region-before extent="1em" region-name="xsl-region-before" /> 
      <fo:region-after extent="1em" region-name="xsl-region-after" /> 
     </fo:simple-page-master> 
     </fo:layout-master-set> 
     <xsl:for-each select="brief"> 
     <fo:page-sequence master-reference="Evidence"> 
     <fo:static-content flow-name="xsl-region-before" font-family="Times"> 
      <fo:block font-size="10pt" text-align="center" color="#666666"> 
      <fo:inline font-style="italic"><xsl:value-of select="title"/></fo:inline> by <xsl:value-of select="author"/> 
      </fo:block> 
     </fo:static-content> 

     <fo:static-content flow-name="xsl-region-after" font-family="Times" font-size="10pt"> 
      <fo:table> 
      <fo:table-column /> 
      <fo:table-column column-width="1in" /> 
      <fo:table-body> 
       <fo:table-row> 
       <fo:table-cell> 
        <fo:block text-align="left" color="#666"><xsl:value-of select="copyright"/></fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block text-align="right" font-weight="bold"> 
        Page <fo:page-number/> 
        </fo:block> 
       </fo:table-cell> 
       </fo:table-row> 
      </fo:table-body> 
      </fo:table> 
     </fo:static-content> 

     <fo:flow flow-name="xsl-region-body" font-family="Times"> 
      <fo:block font-size="14pt" text-align="center" text-transform="uppercase" border-before-width="2pt" border-before-color="black" border-before-style="double" border-after-width="1pt" border-after-color="black" border-after-style="solid" background-color="#ccc"> 
      <xsl:value-of select="title"/> 
      </fo:block> 

      <xsl:for-each select="heading"> 
      <xsl:choose> 
       <xsl:when test="@level = 2"> 
       <fo:block font-size="11pt" font-weight="bold" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:when> 
       <xsl:when test="@level = 3"> 
       <fo:block font-size="10pt" font-weight="normal" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:when> 
       <xsl:otherwise> 
       <fo:block font-size="12pt" font-weight="bold" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:otherwise> 
      </xsl:choose> 

      <xsl:for-each select="content/item"> 
       <xsl:choose> 
       <xsl:when test="@type = 'card'"> 
        <!--Print the taglines--> 
        <fo:block font-size="10pt" font-weight="bold" padding-before="1em" keep-with-next="always"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="tagline"/> 
        </fo:block> 

        <!--Print the citation--> 
        <fo:block font-size="10pt" font-style="italic" keep-with-next="always" keep-together.within-page="always" margin-left=".25in" padding-before=".5em"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="citation" disable-output-escaping="yes" /> 
        </fo:block> 

        <!--Print the body--> 
        <fo:block font-size="10pt" keep-together.within-page="always" margin-left=".25in" padding-before=".5em"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="quote" disable-output-escaping="yes" /> 
        </fo:block> 

       </xsl:when> 
       <xsl:otherwise> 
        <fo:block font-size="10pt" padding-before=".5em"><xsl:value-of select="."/></fo:block> 
       </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each> 

      </xsl:for-each> 


     </fo:flow> 

     </fo:page-sequence> 
     </xsl:for-each> 
    </fo:root> 
    </xsl:template> 

</xsl:stylesheet> 

有沒有人有什麼想法? 非常感謝!!!!!

+0

你能發表你的意見樣本嗎? – lavinio 2009-09-29 01:56:38

回答

7

我不清楚你的代碼在哪裏實際上與文檔「瘋狂」 - 即運行匹配。如果<briefs>是根節點,那麼我希望在某個點看到一些<xsl:apply-templates select="*"/>或類似;否則你是只有去得到在根匹配中指定的輸出(並且它永遠不會應用你的<em>等比賽)。

如果你想定義只有在使用替代來處理整個文檔,那麼經典的比賽是一樣的東西:

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

因爲這所有的節點,和瀑布相匹配,它運行的每一個節點,直到更多找到特定的匹配項。所以用XSLT:

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

大多數節點將被重複「原樣」,但任何<bar>元件將與<BAR/>被替換(並且沒有任何屬性/內容)。

請注意,即使有這個;你需要需要顯式級聯,如果你期望子數據被匹配處理(如上面的<bar>示例所示,其中不是級聯)。在<briefs>比賽中我看不到任何<xsl:apply-templates>

最後; match="//em"是多餘的; match="em"應該就足夠了。

1

沒有看到整個樣式沒有幫助,但是匹配語法可能應該是match="em"match="//em"

+0

感謝您的回答!是的,我也嘗試過這種方式,並沒有幫助... – markwatson 2009-09-26 00:30:47

0

看起來好像你在做XSLT而不是聲明性的。 MG是正確的,我想你會有更多的運氣,如果你做少「for-each」-ing和更多「apply-templates」-ing。

看起來您並未匹配您的所有em節點,因爲您未在主根模板中應用模板。我很想說,每個你有一個for-each的地方,你應該使用apply-templates來代替,但這有點學術。