简体   繁体   English

在HTML文件中链接XML和XSLT文件

[英]Link XML and XSLT files in an HTML file

I am still learning how to use XML and XSL to create an HTML output file and have improved a lot thanks to stackoverflow. 我仍在学习如何使用XML和XSL创建HTML输出文件,并且由于stackoverflow有了很多改进。

I have an XML file that is auto generated but I cannot tell the XML file where the XSLT file is every time a new XML is created. 我有一个自动生成的XML文件,但是每次创建新XML时都无法告诉XML文件XSLT文件在哪里。
Is there a way to create an HTML file that when a user opens it, the HTML will "href" to the XML and XSLT and output it as the intended HTML result, in the same way a CSS or Javascript is included in an HTML. 有没有一种创建HTML文件的方法,当用户打开它时,HTML将“ href”到XML和XSLT并将其输出为预期的HTML结果,就像HTML中包含CSS或Javascript一样。

Thank you, Robin 谢谢你,罗宾

This is the XML file when it is generated from a program we use... 这是从我们使用的程序生成的XML文件...

<Root>
<Ticket>
<TicketStart Date="2012-10-22" />
<Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="0" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="0" Date="2012-10-22" />
<ClubcardPoints Opcode="96" Function="28" SchemeNo="40" PromNo="0" CardNo="1042540000026" QualSpend="30005" PointSpend="0" Points="6" BonusPoints="0" PromCount="0" Date="2012-10-22" />
<Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="1" SchemeNo="40" CardNo="1042540000026" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="30005" Date="2012-10-22" />
<TicketEnd Date="2012-10-22" />
</Ticket>
<Ticket>
<TicketStart Date="2012-10-22" />
<TicketEnd Date="2012-10-22" />
</Ticket>
<Events>
</Events>
<Ticket>
<TicketStart Date="2012-10-22" />
<Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="0" SchemeNo="40" CardNo="1042540000057" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="0" Date="2012-10-22" />
<ClubcardPoints Opcode="96" Function="28" SchemeNo="40" PromNo="0" CardNo="1042540000057" QualSpend="18025" PointSpend="0" Points="3" BonusPoints="0" PromCount="0" Date="2012-10-22" />
<Clubcard Opcode="96" Function="27" CardAcc="1" QualSpendInfo="1" SchemeNo="40" CardNo="1042540000057" PointsToDate="0" RedemptionValue="0" CustomerUpdateDate="000000" QualSpent="18025" Date="2012-10-22" />
<TicketEnd Date="2012-10-22" />
</Ticket>
<Events>
</Events>
</Root>

` `

and currently I have to put this at the start of the XML file 目前,我必须将其放在XML文件的开头

<?xml version="1.0" encoding="utf-8"?>
 <?xml-stylesheet type="text/xsl" href="XSLnice.xsl"?>

The XSL file looks like this... XSL文件看起来像这样...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
    <title>StoreLine Loyalty Sales</title>
      </head>
    <body>
      <br/>
      <br/>
      <br/>
       <b>Loyal Customers</b>
      <br/>
      <br/>
    <table width="100%" border="3">
      <THEAD>
      <TR bgcolor="RGB(0, 204, 51)">
      <TD width="25%">
        <font color="white"><B>Account Number</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Points</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Date</B></font>
      </TD>
      <TD width="25%">
        <font color="white"><B>Qualified Spent</B></font>
      </TD>
      </TR>
      </THEAD>
    <TBODY>
    <xsl:for-each select="Root/Ticket/ClubcardPoints">
      <TR>
      <TD width="25%"><xsl:value-of select="@CardNo" /></TD>
      <TD width="25%"><xsl:value-of select="@Points" /></TD>
      <TD width="25%"><xsl:value-of select="@Date" /></TD>
      <TD width="25%"><xsl:value-of select="format-number(@QualSpend div 100,'&#x52;#.##')" /></TD>
    </TR>
    </xsl:for-each>
    </TBODY>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

` `

With these files in the same folder, I double click the XML file and it presents me with a friendly report of the values I need in the XML. 将这些文件放在同一文件夹中,然后双击XML文件,它为我提供了有关XML中所需值的友好报告。 So rather if I could just double-click one HTML file and everytime a new XML is placed in the same folder, the HTML will find the XML and use the XSL as the style-sheet to present itself. 因此,如果我可以双击一个HTML文件,并且每次将新XML放在同一文件夹中,则HTML都会找到该XML,并使用XSL作为样式表来展示自身。 Instead of having to manually update the XML everytime and tell it to use the XSL. 不必每次都手动更新XML并告诉它使用XSL。

Also I have tried but unsuccessfully group the 'CardNo' and report how many times it was used, how much in total 'Points' were earned and how much was the 'qualified amount'. 我也尝试过但未成功地对“ CardNo”进行分组,并报告了使用了多少次,获得了多少“积分”以及有多少“合格金额”。 Instead of this long list of entries, like a consolidated report. 而不是这么长的条目列表,例如合并报告。

Thank you so much... 非常感谢...

Here is the preferred output file... 这是首选的输出文件...

<THEAD>
                <TR bgcolor="RGB(0, 204, 51)">
                    <TD width="25%"><font color="white"><B>Account Number</B></font></TD>
                    <TD width="25%"><font color="white"><B>Total Points</B></font></TD>
                    <TD width="25%"><font color="white"><B>Date</B></font></TD>
                    <TD width="25%"><font color="white"><B>Total Qualified Spent</B></font></TD>
                </TR>
            </THEAD>
            <TBODY>
                <TR>
                    <TD width="25%">1042540000002</TD>
                    <TD width="25%">100</TD>
                    <TD width="25%">2012-10-22</TD>
                    <TD width="25%">R750.32</TD>
                </TR>

If I understand correctly, you're using XSLT to create HTML from your XML file. 如果我理解正确,那么您正在使用XSLT从XML文件创建HTML。 The regular way of doing this is to run an XSLT processor, giving it the XSLT and XML files. 这样做的常规方法是运行XSLT处理器,并为其提供XSLT和XML文件。 There is no need to modify the XML file in order to do this. 无需为此修改XML文件。 You feed the XSLT and XML files to the processor, and it gives you the transformed output file, which will be HTML in your case. 您将XSLT和XML文件提供给处理器,它会为您提供转换后的输出文件,在您的情况下为HTML。

I believe both Saxon and Altova have free versions of theit XSLT engines (alongside the commercial ones), so try downloading one of them and give it a try. 我相信Saxon和Altova都具有免费版本的theit XSLT引擎(以及商业版本),因此请尝试下载其中一个并尝试一下。 You just need to call the XSLT engine from the command line. 您只需要从命令行调用XSLT引擎。 You can probably use Microsoft's engine, too, as long as you don't need XSLT 2. 只要不需要XSLT 2,您也可以使用Microsoft的引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM