简体   繁体   English

在 XSLT 中处理多个同名元素

[英]Handling multiple elements with same name in XSLT

I have xml with the following data.我有包含以下数据的 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>

I am trying to output this into separate tables per "Table" element just like this but can't figure it out since there are multiple elements with the same name.我试图将它输出到每个“表格”元素的单独表格中,就像这样,但无法弄清楚,因为有多个具有相同名称的元素。
桌子_

So far this is what I got.到目前为止,这就是我得到的。

<?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>

Any suggestion how to achieve desired output is appreciated.任何有关如何实现所需输出的建议表示赞赏。 Thanks!谢谢!

You had a good start but a few details were not working.你有一个良好的开端,但一些细节不起作用。 Here's how I would solve it.这是我将如何解决它。

<?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>

You can test it here : https://xsltfiddle.liberty-development.net/bFWR5Ej/1你可以在这里测试: https : //xsltfiddle.liberty-development.net/bFWR5Ej/1

So far I managed to create the desired outcome, but however, I would seriously suggest to sanitize your XML file/input, because it's complicating things.到目前为止,我设法创建了所需的结果,但是,我会认真建议清理您的 XML 文件/输入,因为它使事情复杂化。

But with your current file, you can use this XSLT-1.0 stylesheet to get the desired output:但是对于您当前的文件,您可以使用此 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>

The central aspect is the xsl:for-each loop:核心方面是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>

The output is:输出是:

在此处输入图片说明

It simply iterates over all <Table> children which are named Source_Fieldname and then creates a table cell for these and for the first following-sibling Source_Type element.它只是遍历所有名为Source_Fieldname <Table>Source_Fieldname ,然后为这些元素第一个后续兄弟Source_Type元素创建一个表格单元格。 This does work for a two element table, but it should be no problem to extend it to a several children situation.这确实适用于两元素表,但将其扩展到多个子项的情况应该没有问题。

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

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