表循环xml xsl未显示

时间:2014-03-16 22:29:31

标签: html xml xslt html-table

我需要使用下面的xml代码文件和xsl代码文件才能在浏览器上显示内容。出于某种原因,我无法弄明白:

显示应如下所示,但我无法弄清楚我需要做什么: enter image description here

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>   

1 个答案:

答案 0 :(得分:1)

在这样的情况下,不是编写整个XSLT负载并且想知道它为什么不起作用,而是退后一步,从简单的工作开始,并在此基础上构建。您已经开始选择单独的发票了,所以您最初可以将其输出到发票号

<xsl:for-each select="/invoices/invoice">
   <H1>
       Invoice #<xsl:value-of select="@number"/>
   </H1>
<xsl:for-each>

但您的问题在于如何选择患者元素。你这样做....

<xsl:for-each select="/invoice/patient">

但有两个问题。首先,这是一个绝对表达式,而不是相对于您当前所在的节点。第一个/代表顶级文档节点,因此它在XML中查找 invoice 的根节点,该节点不存在。

其次,无论如何,循环都在错误的位置。它需要进入 xsl:for-each 循环才能选择 invoice 元素,而不是之后。然后,你可以写这个....

<xsl:for-each select="/invoices/invoice">

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

请注意表达式现在患者以及当前发票项目的相对位置。您可以对保险过程元素执行类似操作。

实际上,最好在这里使用 xsl:apply-templates ,而不是 xsl:for-each ,就好像没有其他任何东西可以避免多余的缩进。因此,您的 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>