簡體   English   中英

XML樣式表XSL,同一項目的多種結構

[英]XML Stylesheet XSL, multiple structure for same item

這是我的xml部分:

<record>
   <BillNum>999</BillNum>
   <Item>Glasses</Item>
   <Price>100</Price>
</record>
<record>
   <BillNum>999</BillNum>
   <Item>Book</Item>
   <Price>50</Price>
</record>
<record>
   <BillNum>999</BillNum>
   <Item>Shoes</Item>
   <Price>500</Price>
</record>

現在,我想為此XML編寫xsl,它可以像下面這樣設置樣式:

<table>
        <tr>
            <td>Item</td>
            <td>Price</td>
        </tr>
        <tr>
            <td>Glasses</td>
            <td>100</td>
        </tr>
        <tr>
            <td>Book</td>
            <td>50</td>
        </tr>
        <tr>
            <td>Shoes</td>
            <td>500</td>
        </tr>
</table>

每個BillNum。

請幫助我,我該如何編寫所需的xsl?

謝謝

這是一個分組問題,在XSLT 1.0中使用Muenchian分組http://www.jenitennison.com/xslt/grouping/muenchian.xml並使用密鑰來解決

<xsl:key name="bill" match="record" use="BillNum"/>

<xsl:template match="NameOfParentOfRecordHere">
  <xsl:apply-templates select="record[generate-id() = generate-id(key('bill', BillNum)[1])]" mode="table"/>
</xsl:template>

<xsl:template match="record" mode="table">
<table>
        <tr>
            <td>Item</td>
            <td>Price</td>
        </tr>
        <xsl:apply-templates select="key('bill', BillNum)"/>
</table>
</xsl:template>

<xsl:template match="record">
  <tr>
    <xsl:apply-templates select="Item | Price"/>
  </tr>
</xsl:template>

<xsl:template match="Item | Price">
  <td><xsl:value-of select="."/></td>
</xsl:template>

然后使用類似的模板設置HTML文檔結構

<xsl:template match="/">
  <html>
    <head>
      <title>Example</title>
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM