【问题标题】:using generate-id() for xsl:result-document filename and linking to HTML对 xsl:result-document 文件名使用 generate-id() 并链接到 HTML
【发布时间】:2015-12-05 19:44:53
【问题描述】:

我正在使用 xslt 将一个大的 xml 转换为更小的相互链接的 html 文件。我在使用 generate-id() 函数时遇到了麻烦,因为生成的 id 与 html href="" 和文件名中的 id 不同

我通过 xsl:result-document 创建以下文件:

index.html | d1e83523.html | d1e83524.html | d1e83525.html | ...

index.html 应该包含一个链接到其他 *.html 文件的列表


index.html 我想要,但我得到的只是不同的 id:

      <ul>
         <li><a href="d1e83523.html">Sample 1</a></li>
         <li><a href="d1e83524.html">Sample 2</a></li>
         <li><a href="d1e83525.html">Sample 3</a></li>
      </ul>

xsl 创建 index.html

<xsl:template match="lab/*">
    <xsl:result-document encoding="utf-8" method="html" href="HTML_out/index.html" >
                <html>
                    <head></head>
                    <body>
                        <ul>
                            <xsl:for-each select="chapter/heading">
                                <li>
                                    <a href="{generate-id()}.html">
                                        <xsl:value-of select="foo"/>
                                    </a>
                                </li>
                            </xsl:for-each>
                        </ul>
                    </body>
                </html>
            </xsl:result-document>
 </xsl:template>

xsl 创建其他 *.html

<xsl:template match="chapter/*[not(self::heading)]">
        <xsl:for-each select=".">
            <xsl:result-document encoding="utf-8" method="html" href="HTML_out/{concat(generate-id(), '.html')}" >
                <html>
                    <head></head>
                    <body>
                        <xsl:apply-templates/>
                    </body>
                </html>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>

xml-sample(注意:后面有多个描述类结构元素)

<lab>
    <description>
        <chapter>
            <heading>Example</heading
            <operation>other elements</operation>
            <operation>other elements</operation>
            ...
        </chapter>
        ...
    </description>
</lab>

感谢您的每一次帮助!

编辑:我正在使用 generate-id() 为这些文件获取唯一的文件名

【问题讨论】:

    标签: html xml xslt xslt-1.0 saxon


    【解决方案1】:

    如果您将&lt;xsl:for-each select="chapter/heading"&gt; 更改为&lt;xsl:for-each select="chapter/*[not(self::heading)]"&gt;,那么您的索引生成将处理您为其生成结果文档的相同元素,并且生成的ID 应该匹配。但是,您需要在同一个转换中运行两个 XSLT sn-ps 以确保获得相同的 id,如果您有单独的样式表,则 generate-id 不能保证给出相同的结果。

    【讨论】:

    • 非常感谢!不幸的是,我仍然不太明白 generate-id() 函数是如何工作的……我尝试过differend 来获取函数括号内的相同节点集,但对我没有任何作用。然而,这个解决方案成功了!
    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 2021-12-08
    • 1970-01-01
    • 2022-01-06
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多