2011-12-02 91 views
3

我對XSLT很陌生。XSLT用生成的標籤替換標籤

這是我想現在要解決好幾個小時的問題:

我自動生成的內容表的XML文檔偉大的工程至今。不過,我想用剛剛生成的toc代碼替換源xml中的佔位符標記。 因此,輸出應該包含帶有自動生成的toc xml替換的佔位符toc標籤的整個文檔。

這是我已經試過:

比方說,我有我的placeholderTag任何地方的文件中,並要替換該/那些。我以爲我可以通過節點通過所有節點環路()和檢查節點名稱等於我的佔位符標記:

<xsl:template match="node()"> 
    <xsl:choose> 
     <xsl:when test="divGen"> 
      <!-- apply other template to generate toc--> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy-of select="node()"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

然而if語句將不匹配這樣的。

編輯: 好吧,這裏的源文件(TEI編碼 - TEI命名空間中刪除):

<TEI> 
<teiHeader> 
    <fileDesc> 
     <titleStmt> 
      <title>Title</title> 
     </titleStmt> 
     <publicationStmt> 
      <p>Publication information</p> 
     </publicationStmt> 
     <sourceDesc> 
      <p>Information about the source</p> 
     </sourceDesc> 
    </fileDesc> 
</teiHeader> 
<text> 
    <front> 
     <titlePage> 
      <byline>title page details</byline> 
     </titlePage> 
    </front> 

    <body> 
     <divGen type="toc"/> 

     <div type="part"> 
      <div type="section"> 
       <head>heading1</head> 
      </div> 
      <div type="section"> 
       <head>heading2</head> 
      </div> 
     </div> 
     <div type="part"> 
      <div type="section"> 
       <head>heading3</head> 
      </div> 
      <div type="section"> 
       <head>heading4</head> 
      </div> 
      <div type="section"> 
       <head>heading5</head> 
      </div> 
     </div> 
    </body> 

    <back> </back> 
</text> 

我想自動生成從頭部值的TOC和更換divGen標籤由自動生成的toc代碼。但請注意,divGen標籤可以位於文檔中的任何位置,但不在身體之外。

任何想法?

克里斯

+0

您能否提供樣本輸入XML和期望的輸出? –

+0

增加了一個樣本輸入,輸出是不是因爲我覺得樣品重要,只是標題應該出現在任何方式 – Chris

回答

2

好問題,+1。

這是一個完整的轉換(與模擬TOC代由真正的替代),顯示如何做到這一點

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:variable name="vTOC"> 
    <xsl:apply-templates mode="TOC"/> 
    <mockTOC> 
    <xsl:comment>The real TOC generated here</xsl:comment> 
    </mockTOC> 
</xsl:variable> 

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

<xsl:template match="divGen[@type='toc']"> 
    <xsl:copy-of select="$vTOC"/> 
</xsl:template> 

<xsl:template match="text()" mode="TOC"/> 
</xsl:stylesheet> 

當這種轉變是在所提供的XML文檔應用:

<TEI> 
    <teiHeader> 
     <fileDesc> 
      <titleStmt> 
       <title>Title</title> 
      </titleStmt> 
      <publicationStmt> 
       <p>Publication information</p> 
      </publicationStmt> 
      <sourceDesc> 
       <p>Information about the source</p> 
      </sourceDesc> 
     </fileDesc> 
    </teiHeader> 
    <text> 
     <front> 
      <titlePage> 
       <byline>title page details</byline> 
      </titlePage> 
     </front> 
     <body> 
      <divGen type="toc"/> 
      <div type="part"> 
       <div type="section"> 
        <head>heading1</head> 
       </div> 
       <div type="section"> 
        <head>heading2</head> 
       </div> 
      </div> 
      <div type="part"> 
       <div type="section"> 
        <head>heading3</head> 
       </div> 
       <div type="section"> 
        <head>heading4</head> 
       </div> 
       <div type="section"> 
        <head>heading5</head> 
       </div> 
      </div> 
     </body> 
     <back> 
     </back> 
    </text> 
</TEI> 

正確,希望產生輸出,其中的任何<divGen type="toc"/> OCCURENCES被取代所產生的TOC

<TEI> 
    <teiHeader> 
     <fileDesc> 
     <titleStmt> 
      <title>Title</title> 
     </titleStmt> 
     <publicationStmt> 
      <p>Publication information</p> 
     </publicationStmt> 
     <sourceDesc> 
      <p>Information about the source</p> 
     </sourceDesc> 
     </fileDesc> 
    </teiHeader> 
    <text> 
     <front> 
     <titlePage> 
      <byline>title page details</byline> 
     </titlePage> 
     </front> 
     <body> 
     <mockTOC><!--The real TOC generated here--></mockTOC> 
     <div type="part"> 
      <div type="section"> 
       <head>heading1</head> 
      </div> 
      <div type="section"> 
       <head>heading2</head> 
      </div> 
     </div> 
     <div type="part"> 
      <div type="section"> 
       <head>heading3</head> 
      </div> 
      <div type="section"> 
       <head>heading4</head> 
      </div> 
      <div type="section"> 
       <head>heading5</head> 
      </div> 
     </div> 
     </body> 
     <back/> 
    </text> 
</TEI> 

說明:在一個變量的使用modes預先生成的TOC,然後重寫identity rule任何TOC佔位符。

+0

好回答,+1;) – Chris

+0

你太聰明的傢伙:) – Treemonkey

+0

@克里斯:不客氣。你不允許尚未給予好評,但可以接受的答案(BU點擊焯芬標誌旁邊)。 –

0
Im guessing somewhere u have a template like 

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

則u希望模板只匹配您的placeholderTag

那麼其他人將默認爲您的其他節點!

<xsl:template match="placeholderTag"> 
    <!-- applying generate toc thing--> 
</xsl:template> 

<xsl:template match="node()"> 
    <xsl:copy-of select="node()"/>  
</xsl:template>