【问题标题】:XSLT replace tag by generated oneXSLT 用生成的标签替换标签
【发布时间】:2011-12-02 12:28:26
【问题描述】:

我对 XSLT 还是很陌生。

这是我试图解决几个小时的问题:

我为 xml 文档自动生成了一个目录,到目前为止效果很好。但是,我想用刚刚生成的 toc 代码替换我的源 xml 中的占位符标记。 所以输出应该包含整个文档,用自动生成的 toc xml 替换 placeholder-toc-tag。

这是我尝试过的:

假设我在文档中的任何位置都有我的 placeholderTag,并且想要替换这个/那些。我认为我可以通过 node() 遍历所有节点并检查节点名称是否等于我的占位符标记:

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

我想从头值自动生成目录,并用自动生成的目录代码替换 divGen 标记。但是请注意,divGen 标签可以在文档中的任何位置,但不能在正文之外。

有什么想法吗?

克里斯

【问题讨论】:

  • 您能否提供示例输入 XML 和所需的输出?
  • 添加了一个示例输入,输出对于我认为的示例并不重要,只是标题应该以任何方式出现

标签: xslt


【解决方案1】:

好问题,+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>

生成了正确的、想要的输出,其中&lt;divGen type="toc"/&gt; 的任何出现都被生成的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,然后为任何 TOC 覆盖 identity rule占位符。

【讨论】:

  • @Chris:不客气。您还不能投票,但您可以接受答案(请点击旁边的 chack-mark)。
  • @Treemonkey:这几乎是标准技术。阅读我的博客,了解真正有趣的事情:)
【解决方案2】:
Im guessing somewhere u have a template like

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

那么你希望模板只匹配你的 placeholderTag

那么其他节点将默认为您的其他节点!

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

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

【讨论】:

    猜你喜欢
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 2022-12-08
    • 2016-03-22
    相关资源
    最近更新 更多