【问题标题】:XML and XSLT creating a menu of links <a href="URL ">Text Description</a> with different url and textXML 和 XSLT 创建具有不同 url 和文本的链接 <a href="URL ">Text Description</a> 菜单
【发布时间】:2017-07-19 02:46:57
【问题描述】:

这就是我所追求的。在标准 HTML 中,我可以做一个这样的链接列表:

<p>
    <a href="/name-of-volume/name-of-book/00-Preamble/Preamble.xml">Preamble</a>
    <a href="/name-of-volume/name-of-book/01-Chapter-01/chapter_1.xml">Chapter 1</a> 
    <a href="/name-of-volume/name-of-book/02-Chapter-02/chapter_2.xml">Chapter 2</a> 
    <a href="/name-of-volume/name-of-book/03-Chapter-03/chapter_3.xml">Chapter 3</a> 
    <a href="/name-of-volume/name-of-book/04-Chapter-04/chapter_4.xml">Chpater 4</a> 
    <a href="/name-of-volume/name-of-book/05-Chapter-05/chapter_5.xml">Chapter 5</a> 
    <a href="/name-of-volume/name-of-book/06-Chapter-06/chapter_6.xml">Chapter 6</a> 
    <a href="/name-of-volume/name-of-book/07-Chapter-07/chapter_7.xml">Chapter 7</a>
    <a href="/name-of-volume/name-of-book/08-Chapter-08/chapter_8.xml">Chapter 8</a> 
    <a href="/name-of-volume/name-of-book/09-Chapter-09/chapter_9.xml">Chapter 9</a> 
    <a href="/name-of-volume/name-of-book/10-Chapter-10/chapter_10.xml">Chapter 10</a> 
    <a href="/name-of-volume/name-of-book/11-Appendix/Appendix.xml">Appendix</a> 
</p>

当浏览器显示此内容时,开始和结束标记之间的文本作为链接出现在屏幕上,我们看不到实际的 URL。当我们单击链接文本时,我们会跳转到 URL。

使用下面的 XML 示例(意识到可能有更好的设置),我如何设置它(包括 XSLT),以便在浏览器屏幕上获得相同的效果。也就是说,我想查看链接文本,但不想显示实际 URL。

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/style-sheets/master-rulebook.xsl"?>

<ROOT>

<side_menu_div>
    <link-text>Preamble</link-text>
        <chapter-link>/name-of-volume/name-of-book/00-Preamble/Preamble.xml</chapter-link>
    <link-text>Chapter 1</link-text>
        <chapter-link>/name-of-volume/name-of-book/01-Chapter-01/chapter_1.xml"</chapter-link>
    <link-text>Chapter 2</link-text> 
        <chapter-link>/name-of-volume/name-of-book/02-Chapter-02/chapter_2.xml"></chapter-link>
    <link-text>Chapter 3</link-text> 
        <chapter-link>/name-of-volume/name-of-book/03-Chapter-03/chapter_3.xml"></chapter-link>
    <link-text>Chapter 4</link-text> 
        <chapter-link>/name-of-volume/name-of-book/04-Chapter-04/chapter_4.xml"</chapter-link>
    <link-text>Chapter 5</link-text> 
        <chapter-link>/name-of-volume/name-of-book/05-Chapter-05/chapter_5.xml"</chapter-link>
    <link-text>Chapter 6</link-text> 
        <chapter-link>/name-of-volume/name-of-book/06-Chapter-06/chapter_6.xml"</chapter-link>
    <link-text>Chapter 7</link-text> 
        <chapter-link>/name-of-volume/name-of-book/07-Chapter-07/chapter_7.xml"</chapter-link>
    <link-text>Chapter 8</link-text>
        <chapter-link>/name-of-volume/name-of-book/08-Chapter-08/chapter_8.xml"</chapter-link>
    <link-text>Chapter 9</link-text> 
        <chapter-link>/name-of-volume/name-of-book/09-Chapter-09/chapter_9.xml"</chapter-link>
    <link-text></link-text> 
        <chapter-link>/name-of-volume/name-of-book/10-Chapter-10/chapter_10.xml"Chapter 10</chapter-link>
    <link-text></link-text> 
        <chapter-link>/name-of-volume/name-of-book/11-Appendix/Appendix.xml"Appendix</chapter-link> 
</side_menu_div>

</ROOT>

我相信上面的 XML 可以正常工作,但我不知道如何设置 XSLT。

在下面海报的帮助下,我的 XSLT 现在看起来像这样,并且运行良好

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html" encoding="utf-8" />
 <xsl:strip-space elements="*" />


<xsl:template match="/">
    <html>
        <body style="background-color:#fffc99;  font-size:16pt;">
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

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


<xsl:template match="side_menu_div">
    <div style="width: 10%; height: 100%; position: fixed; padding-right: 1em; background: #578be0; " >

        <xsl:for-each select="/ROOT/side_menu_div/link-text">
            <xsl:variable name="index" select="position()" />
                <a href="{/ROOT/side_menu_div/chapter-link[$index]}">
                    <xsl:value-of select="." />
                </a>
                <br/>
        </xsl:for-each>

    </div>
</xsl:template>

<xsl:template match="chapter">
        <div style="width: 90%; height: auto; position: absolute; margin-left: 10%; padding-left: 1em; background: #fffc99; " >
            <xsl:apply-templates/>
        </div>
</xsl:template>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    输入 XML 共享需要一些调整才能获得所需的输出,因为很少有 &lt;link-text&gt; 节点没有值。 double-quotes 中的 &lt;chapter-link&gt; 也不是必需的。请在下方找到调整后的输入 XML。

    输入 XML

    <?xml version="1.0" encoding="UTF-8"?>
    <ROOT>
        <side_menu_div>
            <link-text>Preamble</link-text>
            <chapter-link>/name-of-volume/name-of-book/00-Preamble/Preamble.xml</chapter-link>
            <link-text>Chapter 1</link-text>
            <chapter-link>/name-of-volume/name-of-book/01-Chapter-01/chapter_1.xml</chapter-link>
            <link-text>Chapter 2</link-text>
            <chapter-link>/name-of-volume/name-of-book/02-Chapter-02/chapter_2.xml</chapter-link>
            <link-text>Chapter 3</link-text>
            <chapter-link>/name-of-volume/name-of-book/03-Chapter-03/chapter_3.xml</chapter-link>
            <link-text>Chapter 4</link-text>
            <chapter-link>/name-of-volume/name-of-book/04-Chapter-04/chapter_4.xml</chapter-link>
            <link-text>Chapter 5</link-text>
            <chapter-link>/name-of-volume/name-of-book/05-Chapter-05/chapter_5.xml</chapter-link>
            <link-text>Chapter 6</link-text>
            <chapter-link>/name-of-volume/name-of-book/06-Chapter-06/chapter_6.xml</chapter-link>
            <link-text>Chapter 7</link-text>
            <chapter-link>/name-of-volume/name-of-book/07-Chapter-07/chapter_7.xml</chapter-link>
            <link-text>Chapter 8</link-text>
            <chapter-link>/name-of-volume/name-of-book/08-Chapter-08/chapter_8.xml</chapter-link>
            <link-text>Chapter 9</link-text>
            <chapter-link>/name-of-volume/name-of-book/09-Chapter-09/chapter_9.xml</chapter-link>
            <link-text>Chapter  10</link-text>
            <chapter-link>/name-of-volume/name-of-book/10-Chapter-10/chapter_10.xml</chapter-link>
            <link-text>Appendix</link-text>
            <chapter-link>/name-of-volume/name-of-book/11-Appendix/Appendix.xml</chapter-link>
        </side_menu_div>
    </ROOT>
    

    以下是有助于转换的 XSL

    XSL

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output method="html" encoding="utf-8" />
        <xsl:strip-space elements="*" />
        <xsl:template match="/">
            <p>
                <xsl:for-each select="/ROOT/side_menu_div/link-text">
                    <xsl:variable name="index" select="position()" />
                    <a href="{/ROOT/side_menu_div/chapter-link[$index]}">
                        <xsl:value-of select="." />
                    </a>
                </xsl:for-each>
            </p>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 这几乎可以工作。我得到菜单和链接工作,但是.........链接文本重叠(溢出)到下一个链接,每个链接文本实际上是打开它下面的链接。例如第 1 章的链接实际上是打开第 2 章,第 2 章的链接打开第 3 章等等。我将尝试发布屏幕截图,以便输出清晰。
    • 这确实有效。我必须清理我的 XML(就像你上面所说的那样)。我在真实章节的实际代码中仍然有一些垃圾,并且不得不编辑我的 XSLT 以添加
      标签以及一些填充和边距。现在看起来不错并且可以工作。
    猜你喜欢
    • 2015-09-17
    • 2017-02-21
    • 2021-03-19
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 2021-12-14
    • 2013-01-12
    • 1970-01-01
    相关资源
    最近更新 更多