【问题标题】:How can I fix my XSLT so that it displays my XML as pictured?如何修复我的 XSLT 以便它显示我的 XML,如图所示?
【发布时间】:2018-05-13 11:50:36
【问题描述】:

我是 XML 新手,希望得到一点反馈。我正在尝试显示我的 XML,使其看起来像这样:

How I hope to get my XML to display

我似乎无法让元素显示在模板中。 (显然,我没有正确构建它们)。我如何让标题元素 (Koha) 像示例中那样显示为粗体和更大?我只是感觉有点卡住了,我的教授很忙,我希望有人能就下一步做什么提供建议?

我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<digitalLibrarySystem xmlns:xlink="http://www.w3.org/1999/xlink" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="http://www.pages.drexel.edu/~eom25/657/diglibschema/digitalLibrarySystem.xsd">
<systemMetadata>
<title>Koha</title>
<creator>by Katipo Communications</creator>
<subject>public libraries</subject>
<subject>bibliographic managemen</subject>
<subject>distributed library systems</subject>
<description>Koha was one of the the first open-source Integrated Library Systems. It is used and maintained by the worldwide library community.</description>
<date>2000</date>
<type>ILS</type>
<rights>Open-source</rights>
<identifier xlink:type="simple" xlink:href="http://http://www.koha.org/">http://www.http://www.koha.org/</identifier>
</systemMetadata>
<aboutRecord>
<recordCreator>Matthew Weidemann</recordCreator>
<creationDate>May 6, 2018</creationDate>
</aboutRecord>

到目前为止我的 XSLT:

 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
 <html>
 <body>
 <h1>"font-weight:bold"><xsl:value-of select="title"/></h1>
 <h2>by <xsl:value-of select="systemMetadata/creator"/></h2>
 <h3><xsl:value-of select="systemMetadata/subject"/></h3>
 <br/>
 <p>
 <h4><xsl:value-of select="systemMetadata/description"/></h4>
 <br/>
 </p>
  <h5><xsl:value-of select="systemMetadata/rights"/></h5>
  <br/>
  <h6><i>Record created by <xsl:value-of 
select="aboutRecord/recordCreator"/> on 
  <xsl:value-of select="aboutRecord/creationDate"/></i></h6>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

我对我认为的 XPath 感到困惑,任何建议都有帮助。

【问题讨论】:

    标签: html xml xslt-1.0


    【解决方案1】:

    我稍微重新组织了您的 XSLT 文件。
    如果您开始正确缩进文件,您会轻松很多。
    并查看这些差异以开始理解 XSLT 的工作原理。

    这里是您的(稍作修改的)XSLT-1.0 文件,它使您更接近您的目标:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <!-- Create HTML header and stuff -->
        <xsl:template match="/">
            <html>
                <body>
                    <xsl:apply-templates />       <!-- Apply the other templates -->
                </body>
            </html>
        </xsl:template>
    
        <!-- Process all systemMetadata sub-nodes -->
        <xsl:template match="systemMetadata">                
            <h1>
                <b><xsl:value-of select="title"/></b>
            </h1>
            <h2>
                <xsl:value-of select="creator"/>
            </h2>
            <h2>
                <xsl:value-of select="subject"/>
            </h2>
            <br/>
            <p>
                <h3>
                    <xsl:value-of select="description"/>
                </h3>
            </p>
            <h3>
                <xsl:value-of select="rights"/>
            </h3>
        </xsl:template>
    
        <!-- Process all aboutRecord sub-nodes -->
        <xsl:template match="aboutRecord">      
            <h3>
                <i>Record created by <xsl:value-of 
    select="recordCreator"/> on <xsl:value-of select="creationDate"/></i>
            </h3>
        </xsl:template>
    
    </xsl:stylesheet>
    

    现在您可以将其修改为漂亮地打印结果。
    如果要创建漂亮的边框,请将第一个模板更改为

    <!-- Create HTML header and stuff -->
    <xsl:template match="/">
        <html>
            <body>
                <table border="1">
                    <tr><td><xsl:apply-templates /></td></tr>
                </table>
            </body>
        </html>
    </xsl:template>
    

    【讨论】:

    • 谢谢。你不知道这有多大帮助。我仍在努力(这让我觉得自己很愚蠢),但现在我有了希望。
    • 那么,如果我有添加带边框的模板,我需要制作一个表格吗?
    • 您可以添加一行 &lt;tr&gt; 和一个单元格 &lt;td&gt; 并将所有内容放入其中,但它确实可以正常工作。顺便说一句,您不应该添加模板,而是替换另一个 &lt;xsl:template match="/"&gt;...
    • 我添加了两个标签以获得更清洁的解决方案。
    • 非常感谢。这实际上很有帮助,并且在我尝试完成最后两个任务时,看到解决方案(以及我遗漏的所有内容)将对我有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2020-08-10
    • 2014-09-16
    相关资源
    最近更新 更多