从XML生成XSLT

时间:2017-04-28 22:55:20

标签: xml xslt

我正在从XML创建XLST文件。我已经回顾了W3schools和许多其他帖子,但我找到的所有内容似乎都指向基本的XLST语法。我认为我正在做的事情很简单。以下XML是我需要帮助创建XLST文件的项目示例:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
    <Item Version="2">
        <GeneralInfo>
            <Name>TW</Name>
            <DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:06:36</DateTime>       
        </GeneralInfo>
        <AllInfo>
            <Quantity>166800</Quantity>
            <Cost>1668</Cost>
            <ID type="width">20</ID>
            <ID type="length">30</ID>               
            <Info>
                <Id type="ClientCode">Ac</Id>
                <Name>Arthur</Name>
            </Info>
        </AllInfo>
    </Item>
    <Item Version="2">
        <GeneralInfo>
            <Name>TW</Name>
            <DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:07:36</DateTime>       
        </GeneralInfo>
        <AllInfo>
            <Quantity>166</Quantity>
            <Cost>15</Cost>
            <ID type="width">20</ID>            
            <Info>
                <Id type="ClientCode">Ad</Id>
                <Name>Lynx</Name>
            </Info>
        </AllInfo>
        <SupplementalData>
            <Items>
                <Color>Red</Color>
            </Items>
        </SupplementalData>
    </Item> 
</Order>    

我希望XLST的输出如下表所示: enter image description here

1 个答案:

答案 0 :(得分:1)

对于XML

<Order>
<Item Version="2">
<GeneralInfo>
<Name>TW</Name>
<DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:06:36</DateTime>
</GeneralInfo>
<AllInfo>
<Quantity>166800</Quantity>
<Cost>1668</Cost>
<ID type="width">20</ID>
<ID type="length">30</ID>
<Info>
<Id type="ClientCode">Ac</Id>
<Name>Arthur</Name>
</Info>
</AllInfo>
</Item>
<Item Version="2">
<GeneralInfo>
<Name>TW</Name>
<DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:07:36</DateTime>
</GeneralInfo>
<AllInfo>
<Quantity>166</Quantity>
<Cost>15</Cost>
<ID type="width">20</ID>
<Info>
<Id type="ClientCode">Ad</Id>
<Name>Lynx</Name>
</Info>
</AllInfo>
<SupplementalData>
<Items>
<Color>Red</Color>
</Items>
</SupplementalData>
</Item> 
</Order>

和XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Date Time</th>
<th>Quantity</th>
<th>Cost</th>
<th>width</th>
<th>length</th>
<th>Client code</th>
<th>Name</th>
<th>Color</th>
</tr>
<xsl:for-each select="Order/Item">
<tr>
<td><xsl:value-of select="GeneralInfo/Name"/></td>
<td><xsl:value-of select="GeneralInfo/DateTime"/></td>
<td align="right"><xsl:value-of select="AllInfo/Quantity"/></td>
<td align="right"><xsl:value-of select="AllInfo/Cost"/></td>
<td align="right"><xsl:value-of select="AllInfo/ID[@type='width']"/></td>
<td align="right"><xsl:value-of select="AllInfo/ID[@type='length']"/></td>
<td><xsl:value-of select="AllInfo/Info/Id[@type='ClientCode']"/></td>
<td><xsl:value-of select="AllInfo/Info/Name"/></td>
<td><xsl:value-of select="SupplementalData/Items/Color"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template> 
</xsl:stylesheet>