XSLT用生成的标签替换标签

问题描述:

我对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

好问题,+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点击焯芬标志旁边)。 –

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>