簡體   English   中英

在 XSLT 中處理多個同名元素

[英]Handling multiple elements with same name in XSLT

我有包含以下數據的 xml。

<Rows>
   <Header>
      <sourcetable>Table_1</sourcetable>
      <targettable>Table_2</targettable>
   </Header>
   <Table>
      <Source_Fieldname>DTIME_INSERTED</Source_Fieldname>
      <Source_Type>Timestamp</Source_Type>
      <Source_Fieldname>ID_JOB</Source_Fieldname>
      <Source_Type>String</Source_Type>
   </Table>

   <Header>
      <sourcetable>Table_3</sourcetable>
      <targettable>Table_4</targettable>
   </Header>
   <Table>
      <Source_Fieldname>DTIME_INSERTED</Source_Fieldname>
      <Source_Type>Timestamp</Source_Type>
      <Source_Fieldname>ID_JOB</Source_Fieldname>
      <Source_Type>String</Source_Type>
   </Table>   
</Rows>

我試圖將它輸出到每個“表格”元素的單獨表格中,就像這樣,但無法弄清楚,因為有多個具有相同名稱的元素。
桌子_

到目前為止,這就是我得到的。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />
    <xsl:template match="Row">        

        <table border="1px" cellpadding="3" cellspacing="1" style='font-family:"Calibri"; font-size:12; font-weight:"normal"' >
            <tr align="center">
            <xsl:for-each select="preceding-sibling::Header[1]">
               <th colspan="4"  bgcolor="#ccffff">
               <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="sourcetable" /> </font> 
               </th>
               <th colspan="4" bgcolor="#ccffff">
               <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="targettable" /> </font>
               </th>
            </xsl:for-each>
                <th bgcolor="#FFCCBC" rowspan="2">Flagfield</th>
            </tr>
            <tr  align="left">
                <td bgcolor="#C5E1A5">Source_Fieldname</td>
                <td bgcolor="#C5E1A5">Source_Type</td>
            </tr>

            <xsl:for-each select=".">

             <tr>
                <xsl:for-each select="Source_Fieldname">
                    <td> <xsl:value-of select="."/> </td>
                </xsl:for-each>

                <xsl:for-each select="Source_Type">
                        <td> <xsl:value-of select="."/> </td>
                </xsl:for-each>
            </tr>   

            </xsl:for-each>

            </xsl:for-each>
        </table>
        <br /><br/>
    </xsl:template>

</xsl:stylesheet>

任何有關如何實現所需輸出的建議表示贊賞。 謝謝!

你有一個良好的開端,但一些細節不起作用。 這是我將如何解決它。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />

    <xsl:template match="/">
        <html>
            <head/>
            <body>
                <xsl:apply-templates select="Rows/Table"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="Table">
        <table border="1px" cellpadding="3" cellspacing="1" style='font-family:"Calibri"; font-size:12; font-weight:"normal"' >
            <tr align="center">
                <th bgcolor="#ccffff">
                    <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="preceding-sibling::Header[1]/sourcetable" /> </font> 
                </th>
                <th bgcolor="#ccffff">
                   <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="preceding-sibling::Header[1]/targettable" /> </font>
                </th>
            </tr>
            <tr  align="left">
                <td bgcolor="#C5E1A5">Source_Fieldname</td>
                <td bgcolor="#C5E1A5">Source_Type</td>
            </tr>
            <xsl:apply-templates select="Source_Fieldname"/>
        </table>
        <br/><br/>
    </xsl:template>

    <xsl:template match="Source_Fieldname">
        <tr>
            <td> <xsl:value-of select="."/> </td>
            <td> <xsl:value-of select="following-sibling::Source_Type[1]"/> </td>
        </tr>   
    </xsl:template>

</xsl:stylesheet>

你可以在這里測試: https : //xsltfiddle.liberty-development.net/bFWR5Ej/1

到目前為止,我設法創建了所需的結果,但是,我會認真建議清理您的 XML 文件/輸入,因為它使事情復雜化。

但是對於您當前的文件,您可以使用此 XSLT-1.0 樣式表來獲得所需的輸出:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />

    <!-- A template handling the main HTML stuff -->
    <xsl:template match="/Rows">
        <html>
            <body>
                <font size="2" face="Calibri" >
                    <h1>The following Source - Target Tables have DDL Mismatch(es)</h1>
                    <br />
                    <xsl:apply-templates select="Table" />
                </font>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="Table">        
        <table border="1px" cellpadding="3" cellspacing="1" style='font-family:"Calibri"; font-size:12; font-weight:"normal"' >
            <tr align="center">
            <xsl:for-each select="preceding-sibling::Header[1]">
               <th colspan="1"  bgcolor="#ccffff">
               <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="sourcetable" /> </font> 
               </th>
               <th colspan="1" bgcolor="#ccffff">
               <font size="2" face="Calibri" style="text-transform:uppercase"> <xsl:value-of select="targettable" /> </font>
               </th>
            </xsl:for-each>
                <th bgcolor="#FFCCBC" rowspan="2">Flagfield</th>
            </tr>
            <tr  align="left">
                <td bgcolor="#C5E1A5">Source_Fieldname</td>
                <td bgcolor="#C5E1A5">Source_Type</td>
            </tr>
            <xsl:for-each select="*[local-name()='Source_Fieldname']">
                <tr>
                    <td><xsl:value-of select="."/></td>
                    <td><xsl:value-of select="following-sibling::Source_Type[1]"/></td>
                </tr>   
            </xsl:for-each>
        </table>
        <br /><br/>
    </xsl:template>

</xsl:stylesheet>

核心方面是xsl:for-each循環:

<xsl:for-each select="*[local-name()='Source_Fieldname']">
    <tr>
        <td><xsl:value-of select="."/></td>
        <td><xsl:value-of select="following-sibling::Source_Type[1]"/></td>
    </tr>   
</xsl:for-each>

輸出是:

在此處輸入圖片說明

它只是遍歷所有名為Source_Fieldname <Table>Source_Fieldname ,然后為這些元素第一個后續兄弟Source_Type元素創建一個表格單元格。 這確實適用於兩元素表,但將其擴展到多個子項的情況應該沒有問題。

暫無
暫無

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

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