Html 表循环xml xsl未显示

Html 表循环xml xsl未显示,html,xml,xslt,html-table,Html,Xml,Xslt,Html Table,为了在浏览器上显示内容,我需要使用下面的xml代码文件和xsl代码文件。出于某种原因,我什么都想不出来: 显示器应该如下所示,但我不知道我到底需要做什么: XML文件: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="invoices.xsl"?> <invoices> <invoice number="25" date="February 28, 2001"> <p

为了在浏览器上显示内容,我需要使用下面的xml代码文件和xsl代码文件。出于某种原因,我什么都想不出来:

显示器应该如下所示,但我不知道我到底需要做什么:

XML文件:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="invoices.xsl"?>
<invoices>
<invoice number="25" date="February 28, 2001">
<patient firstname="Jeff" familyname="Smith" SSN="123456789">
  <phone type="home" number="123-4567890"/>
  <phone number="321-76543321" type="work"/>
  <address type="home" line1="123 Street" city="City" state="US" zip="12345"/>
</patient>
<insurance name="Humongous First Medical Insurance" plannumber="12345" planname="The Client Company">
<phone number="098-76543321"/>
<address type="business" line1="321 Street" city="City" state="US" zip="54321"/>
</insurance>
<procedure code="123" name="Cleaning nose" cost="50.00" insurance_estimate="50.00"/>
<procedure code="124" name="Tarot reading of illnesses" cost="150.00" insurance_estimate="120.00"/>
<procedure code="125" name="Just for fun" cost="100.00" insurance_estimate="80.00"/>
</invoice>
<invoice number="27" date="February 28, 2001">
  <patient firstname="James" familyname="Smith" SSN="123456765">
     <phone type="home" number="123-4562245"/>
     <address type="home" line1="432 Street" city="City" state="US" zip="12343"/>
   </patient>
   <insurance name="Humongous Second Medical Insurance" plannumber="3455" planname="Another Client Company">
      <phone number="098-76543321"/>
      <address type="business" line1="344 Street" city="Some City" state="US" zip="54323"/>
   </insurance>
   <procedure code="123" name="Cleaning nose" cost="50.00" insurance_estimate="50.00"/>
   <procedure code="124" name="Tarot reading of illnesses" cost="150.00" insurance_estimate="120.00"/>
 </invoice>
 <invoice number="29" date="February 28, 2001">
<patient firstname="Neil" familyname="Smith" SSN="123456345">
  <phone type="home" number="125-4345890"/>
  <address type="home" line1="187 Street" city="Lost City" state="US" zip="42145"/>
</patient>
<insurance name="Humongous Third Medical Insurance" plannumber="12345" planname="The Lost City Client Company">
  <phone number="198-76345321"/>
  <address type="business" line1="342 Street" city="Completely Lost City" state="US" zip="111111-0000"/>
</insurance>
<procedure code="123" name="Cleaning nose" cost="50.00" insurance_estimate="50.00"/>
<procedure code="125" name="Maybe they wouldn't see this line..." cost="100.00" insurance_estimate="80.00"/>
</invoice>
</invoices>

XSL文件代码:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<BODY bgcolor="#FFFFE0">
<!--    -->
<TABLE border="0" width="100%">
<xsl:for-each select="/invoices/invoice">
<tr>
<td>
<H1>Invoice #
<xsl:value-of select="@number"/>,<BR/>
<xsl:value-of select="@date"/>
</H1>
<TD align="right"><img src="sax_extractData_logo.gif"/>
</td>
</td>
</TR>
</xsl:for-each>
<!--   -->
 </TABLE>  
<TABLE border="0" width="100%">
  <TR valign="top">
<TD>
  <xsl:for-each select="/invoice/patient">
        To: <xsl:value-of select="@firstname"/><xsl:text> </xsl:text>
            <xsl:value-of select="@familyname"/>
        <BR/>Account #<xsl:value-of select="@SSN"/>
        <BR/>
        <xsl:value-of select="address/@line1"/><BR/>
        <xsl:if test="address/@line2!=''">
            <xsl:value-of select="address/@line2"/><BR/>
        </xsl:if>
        <xsl:value-of select="address/@city"/>,
        <xsl:value-of select="address/@state"/>
        <xsl:value-of select="address/@zip"/><BR/>
  </xsl:for-each>
</TD>
<TD>
    <xsl:for-each select="/invoice/insurance">
        Insurance: <xsl:value-of select="@name"/><BR/>
        Plan name: <xsl:value-of select="@planname"/><BR/>
        Plan #<xsl:value-of select="@plannumber"/><BR/>
        <xsl:value-of select="address/@line1"/><BR/>
        <xsl:if test="address/@line2!=''">
            <xsl:value-of select="address/@line2"/><BR/>
        </xsl:if>
        <xsl:value-of select="address/@city"/>,
        <xsl:value-of select="address/@state"/>
        <xsl:value-of select="address/@zip"/><BR/>
        <xsl:value-of select="phone/@number"/><BR/>
  </xsl:for-each>
</TD>
  </TR>
</TABLE>
<P> </P>
<TABLE border="1" width="100%">
  <TR>
<TD width="20%">Code</TD>
<TD width="20%">Name</TD>
<TD width="20%">Cost</TD>
<TD width="20%">Insurance estimate</TD>
  </TR>
  <xsl:for-each select="/invoice/procedure">
  <TR>
<TD width="20%"><xsl:value-of select="@code"/></TD>
<TD width="20%"><xsl:value-of select="@name"/></TD>
<TD width="20%"><xsl:value-of select="@cost"/></TD>
<TD width="20%"><xsl:value-of select="@insurance_estimate"/></TD>
  </TR>
  </xsl:for-each>
</TABLE>
<P> </P>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>   

发票#
,
致:
帐户#


,
保险:
计划名称:
计划#


,

代码 名称 成本 保险估价


在这种情况下,与其写一大堆XSLT并想知道它为什么不起作用,不如退一步,从一些简单的可行的东西开始,并以此为基础。您已经从选择单个发票开始了,所以您可以首先让它输出发票号

<xsl:for-each select="/invoices/invoice">
   <H1>
       Invoice #<xsl:value-of select="@number"/>
   </H1>
<xsl:for-each>
请注意表达式现在是如何与当前发票项相对的。您可以对保险过程元素执行类似操作

实际上,最好在这里使用xsl:apply-templates,而不是xsl:for-each,就好像没有其他东西可以避免过度缩进一样。因此,您对每个语句的将变成以下内容

<xsl:apply-templates select="patient" />

然后,您将有一个单独的模板来输出详细信息

<xsl:template match="patient">
    To: <xsl:value-of select="@firstname"/>    <xsl:value-of select="@familyname"/>
    <BR/>
</xsl:template>

致:

尝试使用此XSLT作为启动程序。注意,我在这里没有输出许多HTML表或所有字段,但它应该会给您一些想法,因此您可以在此基础上进行构建:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html"/>
   <xsl:template match="/">
      <HTML>
         <BODY bgcolor="#FFFFE0">
            <xsl:apply-templates select="invoices/invoice" />
         </BODY>
      </HTML>
   </xsl:template>

    <xsl:template match="invoice">
       <H1>
          Invoice #<xsl:value-of select="@number"/>
       </H1>
       <img src="sax_extractData_logo.gif"/>
       <br/>

       <xsl:apply-templates select="patient" />
       <xsl:apply-templates select="insurance" />
       <table>
          <xsl:apply-templates select="procedure" />
       </table>
    </xsl:template>

   <xsl:template match="patient">
       To: <xsl:value-of select="@firstname"/>    <xsl:value-of select="@familyname"/>
       <BR/>
   </xsl:template>

    <xsl:template match="insurance"> 
       Insurance:  <xsl:value-of select="@name"/>
       <BR/>
    </xsl:template>

     <xsl:template match="procedure"> 
        <tr>
           <td><xsl:value-of select="@code"/></td>
           <td><xsl:value-of select="@name"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

发票#

致:
保险:

由于某些原因,我无法再次使用相同的模板。例如,它只是替换第一个名称。另外,如何在同一行中使用另一个模板?我不确定“再次使用同一个模板”是什么意思,因为上面XSLT中的所有模板都不同。看一看,看看它在起作用。
<xsl:template match="patient">
    To: <xsl:value-of select="@firstname"/>    <xsl:value-of select="@familyname"/>
    <BR/>
</xsl:template>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="html"/>
   <xsl:template match="/">
      <HTML>
         <BODY bgcolor="#FFFFE0">
            <xsl:apply-templates select="invoices/invoice" />
         </BODY>
      </HTML>
   </xsl:template>

    <xsl:template match="invoice">
       <H1>
          Invoice #<xsl:value-of select="@number"/>
       </H1>
       <img src="sax_extractData_logo.gif"/>
       <br/>

       <xsl:apply-templates select="patient" />
       <xsl:apply-templates select="insurance" />
       <table>
          <xsl:apply-templates select="procedure" />
       </table>
    </xsl:template>

   <xsl:template match="patient">
       To: <xsl:value-of select="@firstname"/>    <xsl:value-of select="@familyname"/>
       <BR/>
   </xsl:template>

    <xsl:template match="insurance"> 
       Insurance:  <xsl:value-of select="@name"/>
       <BR/>
    </xsl:template>

     <xsl:template match="procedure"> 
        <tr>
           <td><xsl:value-of select="@code"/></td>
           <td><xsl:value-of select="@name"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>