尝试将XML文件转换为其他xml文件

时间:2017-09-22 06:51:39

标签: xml xslt

我正在尝试将XML转换为看起来像另一个XML的输出。我不确定如何打印'正斜杠'虽然。 我还需要输出标签。 这是我必须转换的xml文件



    <ItemDetails>
    <Item>
        <Code>3390876231</Code>
        <Customer id='1055'>
            <FirstName>John</FirstName>
            <MiddleName>M</MiddleName>
            <LastName>Crusoe</LastName>
            <Title>Mr.</Title>
            <Address>
                <HouseNumber>House11</HouseNumber>
                <Street>St.Marks Street</Street>
                <Country>UK</Country>
                <ContactNumber>
                    <PhoneNumber1>0657385245</PhoneNumber1>
                    <PhoneNumber2>0657385211</PhoneNumber2>
                </ContactNumber>
                <PostalCode>3452DD</PostalCode>
            </Address>
        </Customer>
        <Name>Romeo and Juliet</Name>
        <Type>compact disc</Type>
        <Author>Rostropovich</Author>
        <ListPrice>18.98</ListPrice>
        <SellPrice>13.99</SellPrice>
        <Cost>9.92</Cost>
    </Item>
    <Item>
        <Code>3390007231</Code>
        <Customer id='1056'>
            <FirstName>Kayne</FirstName>
            <MiddleName/>
            <LastName>West</LastName>
            <Title>Mr.</Title>
            <Address>
                <HouseNumber>House78</HouseNumber>
                <Street>Pittsburg Street</Street>
                <Country>USA</Country>
                <ContactNumber>
                    <PhoneNumber1>0690885245</PhoneNumber1>
                    <PhoneNumber2>0690785211</PhoneNumber2>
                </ContactNumber>
                <PostalCode>3882PP</PostalCode>
            </Address>
        </Customer>
        <Name>XSLT</Name>
        <Type>book</Type>
        <Author>Tidwell</Author>
        <ListPrice>49.95</ListPrice>
        <SellPrice>34.99</SellPrice>
        <Cost>22.92</Cost>
    </Item>
    <Item>
        <Code>3390876001</Code>
        <Customer id='1057'>
            <FirstName>Martha</FirstName>
            <MiddleName>W</MiddleName>
            <LastName>Hamilton</LastName>
            <Title>Mrs.</Title>
            <Address>
                <HouseNumber>House456</HouseNumber>
                <Street>Alfred Street</Street>
                <Country>UK</Country>
                <ContactNumber>
                    <PhoneNumber1>0659985245</PhoneNumber1>
                    <PhoneNumber2>0657300011</PhoneNumber2>
                </ContactNumber>
                <PostalCode>3786CD</PostalCode>
            </Address>
        </Customer>
        <Name>The Little Schemer</Name>
        <Type>book</Type>
        <Author>Friedman</Author>
        <Author>Felleisen</Author>
        <ListPrice>29.95</ListPrice>
        <SellPrice>26.99</SellPrice>
        <Cost>17.92</Cost>
    </Item>
    <Item>
        <Code>3399996231</Code>
        <Customer id='1058'>
            <FirstName>Harold</FirstName>
            <MiddleName/>
            <LastName>Crossley</LastName>
            <Title>Mr.</Title>
            <Address>
                <HouseNumber>House909</HouseNumber>
                <Street>Beaumont Street</Street>
                <Country>UK</Country>
                <ContactNumber>
                    <PhoneNumber1>0656665245</PhoneNumber1>
                    <PhoneNumber2>0657385200</PhoneNumber2>
                </ContactNumber>
                <PostalCode>3772DI</PostalCode>
            </Address>
        </Customer>
        <Name>A Tribute to People’s Princess: Diana</Name>
        <Type>book</Type>
        <Author>Peter Donelli</Author>
        <ListPrice>65.98</ListPrice>
        <SellPrice>78.99</SellPrice>
        <Cost>60.92</Cost>
    </Item>
    <Item>
        <Code>33908755118</Code>
        <Customer id='1059'>
            <FirstName>Donna</FirstName>
            <MiddleName>H</MiddleName>
            <LastName>Miller</LastName>
            <Title>Miss</Title>
            <Address>
                <HouseNumber>House998</HouseNumber>
                <Street>Maple Street</Street>
                <Country>USA</Country>
                <ContactNumber>
                    <PhoneNumber1>8002255288</PhoneNumber1>
                    <PhoneNumber2>8011155288</PhoneNumber2>
                </ContactNumber>
                <PostalCode>3662DD</PostalCode>
            </Address>
        </Customer>
        <Name>KingMozart</Name>
        <Type>compact disc</Type>
        <Author>Donald Runnicles</Author>
        <ListPrice>20.98</ListPrice>
        <SellPrice>18.99</SellPrice>
        <Cost>22.92</Cost>
    </Item>
</ItemDetails>
&#13;
&#13;
&#13;

这是输出的方式。要删除并按顺序同步的嵌套标记。

&#13;
&#13;
<ItemDetails>
    <Item>
        <Code>3390876231</Code>
        <CustomerID>1055</CustomerID>
        <FullName>Mr.John M Crusoe</FullName>
        <Address>House11,St.Marks Street,UK</Address>
        <ContactNumbers>0657385245,0657385211</ContactNumbers>
        <PostalCode>3452DD</PostalCode>
        <ItemName>Romeo and Juliet</ItemName>
        <ItemType>compact disc</ItemType>
        <Author>Rostropovich</Author>
        <list-price>18.98</list-price>
        <sell-price>13.99</sell-price>
        <cost-price>9.92</cost-price>
        <profit>4.07</profit>
    </Item>
    <Item>
        <Code>3390007231</Code>
        <CustomerID>1056</CustomerID>
        <FullName>Mr.Kayne West</FullName>
        <Address>House78,Pittsburg Street,USA</Address>
        <ContactNumbers>0690885245</ContactNumbers>
        <PostalCode>3882PP</PostalCode>
        <ItemName>XSLT</ItemName>
        <ItemType>book</ItemType>
        <Author>Tidwell</Author>
        <list-price>49.95</list-price>
        <sell-price>34.99</sell-price>
        <cost-price>22.92</cost-price>
        <profit>12.07</profit>
    </Item>
</ItemDetails>
&#13;
&#13;
&#13;

这是我的代码。我不知道如何在输出中包含标签

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <html>
   <body>
    <xsl:for-each select="Item">
	 <p>&lt;Code&gt;<xsl:value-of select="Code"/>&lt;/Code&gt;</br>
	 &lt;CustomerID&gt;<xsl:value-of select="Customer"/>&lt;/CustomerID&gt;</br>
	 <xsl:for-each select="Customer">
	 &lt;CustomerID&gt;<xsl:value-of select="Customer/@id"/>&lt;/CustomerID&gt;</br>
	 &lt;FullName&gt;<xsl:value-of select="Title"/> <xsl:value-of select="FirstName"/> <xsl:value-of select="MiddleName"/> <xsl:value-of select="LastName"/>&lt;/FullName&gt; </br>
	 &lt;Address&gt;
	   <xsl:for-each select="Address">
	     <xsl:value-of select="HouseNumber"/> <xsl:value-of select="Street"/> <xsl:value-of select="Country"/> &lt;/Address&gt; </br>
	 &lt;ContactNumbers&gt;<xsl:for-each select="ContactNumber"> <xsl:value-of select="PhoneNumber1"/> , <xsl:value-of select="PhoneNumber2"/> </xsl:for-each>&lt;/ContactNumbers&gt; </br>
	 &lt;PostalCode&gt;<xsl:value-of select="PostalCode"/>&lt;/PostalCode&gt;</br>
	 </xsl:for-each>
	 </xsl:for-each>
	 &lt;ItemName&gt;<xsl:value-of select="Name"/>&lt;/ItemName&gt;</br>
	 &lt;ItemType&gt;<xsl:value-of select="Type"/>&lt;/ItemType&gt;</br>
	 &lt;Author&gt;<xsl:value-of select="Author"/>&lt;/Author&gt;</br>
	 &lt;list-price&gt;<xsl:value-of select="ListPrice"/>&lt;/list-price&gt;</br>
	 &lt;sell-price&gt;<xsl:value-of select="SellPrice"/>&lt;sell-price&gt;</br>
	 &lt;cost-price&gt;<xsl:value-of select="Cost"/>&lt;/cost-price&gt;</br></p>
	</xsl:for-each>
   </body>
  </html>
 </xsl:stylesheet>
	   	
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

请尝试以下XSLT以获得所需的输出。

absolute