在XML转换到文本

时间:2018-03-21 07:25:18

标签: xml templates xslt transformation

我正在尝试将以下XML文档转换为一组简单的文本记录。此XML文档是比较报告,包含来自源(<row host="src">)和目标(<row host="targ">)的记录。我希望源和目标的记录显示在一行上。

<col></col>属性是动态生成的,并且会因每条记录而异。

有人可以帮我为这个XML文档创建一个XSLT转换模板。

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8" ?>
<rows>
    <row-diff op="update" row-stat="oos">
        <row host="src">
            <col name="ID" val="165218" diff="n"/>
            <col name="GGROWHASH" hex="y" val="510616A185137C0B" diff="y"/>
            <col name="APPLICATIONID" val="2631718" diff="n"/>
            <col name="APPLICATIONALERTTYPEID" val="2" diff="n"/>
            <col name="ALERTTYPE" val="Business Verification - Underwriting" diff="n"/>
            <col name="MESSAGEERROR" val="Business Verification - Underwriting" diff="n"/>
            <col name="ISPASS" val="0" diff="n"/>
            <col name="ISACTIVE" val="0" diff="n"/>
            <col name="ISVALIDATIONREQUIRED" val="1" diff="n"/>
            <col name="ISHIDELINK" val="0" diff="n"/>
            <col name="CREATEDAT" val="2016-11-04:19:25:17.000000" diff="n"/>
            <col name="CLEAREDAT" val="2016-11-04:19:26:34.000000" diff="y"/>
            <col name="ISSUPPRESSED" val="0" diff="n"/>
            <col name="DESCRIPTION" val=" " diff="n"/>
        </row>
        <row host="targ">
            <col name="ID" val="165218" diff="n"/>
            <col name="GGROWHASH" hex="y" val="CFF873308AFA3EDE" diff="y"/>
            <col name="APPLICATIONID" val="2631718" diff="n"/>
            <col name="APPLICATIONALERTTYPEID" val="2" diff="n"/>
            <col name="ALERTTYPE" val="Business Verification - Underwriting" diff="n"/>
            <col name="MESSAGEERROR" val="Business Verification - Underwriting" diff="n"/>
            <col name="ISPASS" val="0" diff="n"/>
            <col name="ISACTIVE" val="0" diff="n"/>
            <col name="ISVALIDATIONREQUIRED" val="1" diff="n"/>
            <col name="ISHIDELINK" val="0" diff="n"/>
            <col name="CREATEDAT" val="2016-11-04:19:25:17.000000" diff="n"/>
            <col name="CLEAREDAT" val="2016-11-04:19:28:40.000000" diff="y"/>
            <col name="ISSUPPRESSED" val="0" diff="n"/>
            <col name="DESCRIPTION" val=" " diff="n"/>
        </row>
    </row-diff>
    <row-diff op="update" row-stat="oos">
        <row host="src">
            <col name="ID" val="167450" diff="n"/>
            <col name="GGROWHASH" hex="y" val="B3FA385F42FC2B83" diff="y"/>
            <col name="APPLICATIONID" val="2684158" diff="n"/>
            <col name="APPLICATIONALERTTYPEID" val="1" diff="n"/>
            <col name="ALERTTYPE" val="Application Alert" diff="n"/>
            <col name="MESSAGEERROR" val="Application Alert" diff="n"/>
            <col name="ISPASS" val="0" diff="n"/>
            <col name="ISACTIVE" val="0" diff="n"/>
            <col name="ISVALIDATIONREQUIRED" val="1" diff="n"/>
            <col name="ISHIDELINK" val="0" diff="n"/>
            <col name="CREATEDAT" val="2016-11-06:17:09:12.000000" diff="n"/>
            <col name="CLEAREDAT" val="2016-11-06:17:18:15.000000" diff="y"/>
            <col name="ISSUPPRESSED" val="0" diff="n"/>
            <col name="DESCRIPTION" val=" " diff="n"/>
        </row>
        <row host="targ">
            <col name="ID" val="167450" diff="n"/>
            <col name="GGROWHASH" hex="y" val="A507083F74BF5677" diff="y"/>
            <col name="APPLICATIONID" val="2684158" diff="n"/>
            <col name="APPLICATIONALERTTYPEID" val="1" diff="n"/>
            <col name="ALERTTYPE" val="Application Alert" diff="n"/>
            <col name="MESSAGEERROR" val="Application Alert" diff="n"/>
            <col name="ISPASS" val="0" diff="n"/>
            <col name="ISACTIVE" val="0" diff="n"/>
            <col name="ISVALIDATIONREQUIRED" val="1" diff="n"/>
            <col name="ISHIDELINK" val="0" diff="n"/>
            <col name="CREATEDAT" val="2016-11-06:17:09:12.000000" diff="n"/>
            <col name="CLEAREDAT" val="2016-11-06:17:37:29.000000" diff="y"/>
            <col name="ISSUPPRESSED" val="0" diff="n"/>
            <col name="DESCRIPTION" val=" " diff="n"/>
        </row>
</rows>
&#13;
&#13;
&#13;

以下是我正在使用的XSLT模板。此模板将源和目标中的行放在同一行上。

&#13;
&#13;
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
   <xsl:template match = "/">
      <html>
         <body>
            <h2>Out Of Sync report</h2>
            <table border = "1">	
               <xsl:for-each select = "rows/row-diff">
                  <tr>
				  <td><xsl:value-of select = "@op"/></td>
					<xsl:for-each select = "row/col">	
                     <td><xsl:value-of select = "@val"/></td>
					</xsl:for-each>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>
&#13;
&#13;
&#13;

我希望来自&#34; name&#34;要被视为标题的属性。而且,如果我能以某种方式将来自源和目标的记录一个接一个地放入,那就太棒了。如下所示

&#13;
&#13;
<style type='text/css'>
html, body, div, span, h1, h2, h3, h4, h5, h6, p, pre, a, table, tr, th, td {
border: 0; font: 10.5pt calibri; font-style: inherit;font-weight: inherit;color: #777; margin: 0; outline: 0;padding: 0; padding-left:5px; padding-right:5px; vertical-align: baseline;}
html {-webkit-text-size-adjust: none;}
table,tr,td {color:#585858; padding:1px 1px 1px 1px; margin:0px 0px 0px 0px; text-align:left;}
table { border: 1px solid #777; border-collapse: collapse; padding-top:10px;}
td { padding:2px 2px 2px 2px; border: 1px solid #777;}
th {font:bold 10.5pt calibri; color:#FFFFFF; background:#DBA901; padding:3px 3px 3px 3px; border: 1px solid #777; text-transform: uppercase; text-align:left;}
th {border-bottom-color: #777; background-color: #DBA901; border-top-color: #777; border-right-color: #777; border-left-color: #777; border-bottom-style: hidden;}
h1 { font-size: 20px; line-height: 35px; color: #777; padding: 20px, 20px,20px,5px;border-bottom:1px solid #777; width:800px;}
.overview_table {width: 250px;}
.red td {color: red;}
.err {color: red;}
</style>

<body>
<table>
<tr>
<th>Origin</th>
<th>Operation</th>
<th>ID</th>
<th>GGROWHASH</th>
<th>APPLICATIONID</th>
<th>APPLICATIONALERTTYPEID</th>
<th>ALERTTYPE</th>
<th>MESSAGEERROR</th>
<th>ISPASS</th>
<th>ISACTIVE</th>
<th>ISVALIDATIONREQUIRED</th>
<th>ISHIDELINK</th>
<th>CREATEDAT</th>
<th>CLEAREDAT</th>
<th>ISSUPPRESSED</th>
<th>DESCRIPTION</th>
</tr>
<tr>
<td>src</td>
<td>update</td>
<td>165218</td>
<td>510616A185137C0B</td>
<td>2631718</td>
<td>2</td>
<td>Business Verification - Underwriting</td>
<td>Business Verification - Underwriting</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>2016-11-04:19:25:17.000000</td>
<td>2016-11-04:19:26:34.000000</td>
<td>0</td>
<td> </td>
</tr>
<tr>
<td>targ</td>
<td>update</td>
<td>165218</td>
<td>CFF873308AFA3EDE</td>
<td>2631718</td>
<td>2</td>
<td>Business Verification - Underwriting</td>
<td>Business Verification - Underwriting</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>2016-11-04:19:25:17.000000</td>
<td>2016-11-04:19:28:40.000000</td>
<td>0</td>
<td> </td>
</tr>
<tr>
</table>
</body>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

可以通过修改XSLT以多次执行循环来实现所需的输出。

为了打印标题行,您可以在row-diff[1]/row[1]/col上运行循环并对前2列的值进行硬编码,因为它们的值在输入XML中不可用。

<tr>
    <th>ORIGIN</th>
    <th>OPERATION</th>
    <xsl:for-each select="row-diff[1]/row[1]/col">
        <th><xsl:value-of select="@name" /></th>
    </xsl:for-each>
</tr>

要打印值,请运行嵌套循环,如下所示。在这里,您需要注意为外循环中的前2列和内循环中的其余列准备<td>

<xsl:for-each select="row-diff/row">
    <tr>
        <td><xsl:value-of select="@host" /></td>
        <td><xsl:value-of select="../@op" /></td>
        <xsl:for-each select="col">
            <td><xsl:value-of select="@val" /></td>
        </xsl:for-each>
    </tr>
</xsl:for-each>

以下是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" />
    <xsl:strip-space elements="*" />

    <xsl:template match="rows">
        <html>
            <body>
                <h2>Out Of Sync report</h2>
                <table border="1">
                    <tr>
                        <th>ORIGIN</th>
                        <th>OPERATION</th>
                        <xsl:for-each select="row-diff[1]/row[1]/col">
                            <th><xsl:value-of select="@name" /></th>
                        </xsl:for-each>
                    </tr>
                    <xsl:for-each select="row-diff/row">
                        <tr>
                            <td><xsl:value-of select="@host" /></td>
                            <td><xsl:value-of select="../@op" /></td>
                            <xsl:for-each select="col">
                                <td><xsl:value-of select="@val" /></td>
                            </xsl:for-each>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输出(转化后将<style>添加到html

<html>
<style type='text/css'>
html, body, div, span, h1, h2, h3, h4, h5, h6, p, pre, a, table, tr, th, td {
border: 0; font: 10.5pt calibri; font-style: inherit;font-weight: inherit;color: #777; margin: 0; outline: 0;padding: 0; padding-left:5px; padding-right:5px; vertical-align: baseline;}
html {-webkit-text-size-adjust: none;}
table,tr,td {color:#585858; padding:1px 1px 1px 1px; margin:0px 0px 0px 0px; text-align:left;}
table { border: 1px solid #777; border-collapse: collapse; padding-top:10px;}
td { padding:2px 2px 2px 2px; border: 1px solid #777;}
th {font:bold 10.5pt calibri; color:#FFFFFF; background:#DBA901; padding:3px 3px 3px 3px; border: 1px solid #777; text-transform: uppercase; text-align:left;}
th {border-bottom-color: #777; background-color: #DBA901; border-top-color: #777; border-right-color: #777; border-left-color: #777; border-bottom-style: hidden;}
h1 { font-size: 20px; line-height: 35px; color: #777; padding: 20px, 20px,20px,5px;border-bottom:1px solid #777; width:800px;}
.overview_table {width: 250px;}
.red td {color: red;}
.err {color: red;}
</style>
    <body>
        <h2>Out Of Sync report</h2>
        <table border="1">
            <tr>
                <th>ORIGIN</th>
                <th>OPERATION</th>
                <th>ID</th>
                <th>GGROWHASH</th>
                <th>APPLICATIONID</th>
                <th>APPLICATIONALERTTYPEID</th>
                <th>ALERTTYPE</th>
                <th>MESSAGEERROR</th>
                <th>ISPASS</th>
                <th>ISACTIVE</th>
                <th>ISVALIDATIONREQUIRED</th>
                <th>ISHIDELINK</th>
                <th>CREATEDAT</th>
                <th>CLEAREDAT</th>
                <th>ISSUPPRESSED</th>
                <th>DESCRIPTION</th>
            </tr>
            <tr>
                <td>src</td>
                <td>update</td>
                <td>165218</td>
                <td>510616A185137C0B</td>
                <td>2631718</td>
                <td>2</td>
                <td>Business Verification - Underwriting</td>
                <td>Business Verification - Underwriting</td>
                <td>0</td>
                <td>0</td>
                <td>1</td>
                <td>0</td>
                <td>2016-11-04:19:25:17.000000</td>
                <td>2016-11-04:19:26:34.000000</td>
                <td>0</td>
                <td>
                </td>
            </tr>
            <tr>
                <td>targ</td>
                <td>update</td>
                <td>165218</td>
                <td>CFF873308AFA3EDE</td>
                <td>2631718</td>
                <td>2</td>
                <td>Business Verification - Underwriting</td>
                <td>Business Verification - Underwriting</td>
                <td>0</td>
                <td>0</td>
                <td>1</td>
                <td>0</td>
                <td>2016-11-04:19:25:17.000000</td>
                <td>2016-11-04:19:28:40.000000</td>
                <td>0</td>
                <td>
                </td>
            </tr>
            <tr>
                <td>src</td>
                <td>update</td>
                <td>167450</td>
                <td>B3FA385F42FC2B83</td>
                <td>2684158</td>
                <td>1</td>
                <td>Application Alert</td>
                <td>Application Alert</td>
                <td>0</td>
                <td>0</td>
                <td>1</td>
                <td>0</td>
                <td>2016-11-06:17:09:12.000000</td>
                <td>2016-11-06:17:18:15.000000</td>
                <td>0</td>
                <td>
                </td>
            </tr>
            <tr>
                <td>targ</td>
                <td>update</td>
                <td>167450</td>
                <td>A507083F74BF5677</td>
                <td>2684158</td>
                <td>1</td>
                <td>Application Alert</td>
                <td>Application Alert</td>
                <td>0</td>
                <td>0</td>
                <td>1</td>
                <td>0</td>
                <td>2016-11-06:17:09:12.000000</td>
                <td>2016-11-06:17:37:29.000000</td>
                <td>0</td>
                <td>
                </td>
            </tr>
        </table>
    </body>
</html>