2017-09-22 67 views
0

我試圖將XML轉換爲看起來像另一個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>

這是輸出應該如何將XML文件。要嵌套的標籤被刪除並按順序同步。

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

這是我的代碼。我不知道如何將標籤輸出

<?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"> 
 
\t <p>&lt;Code&gt;<xsl:value-of select="Code"/>&lt;/Code&gt;</br> 
 
\t &lt;CustomerID&gt;<xsl:value-of select="Customer"/>&lt;/CustomerID&gt;</br> 
 
\t <xsl:for-each select="Customer"> 
 
\t &lt;CustomerID&gt;<xsl:value-of select="Customer/@id"/>&lt;/CustomerID&gt;</br> 
 
\t &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> 
 
\t &lt;Address&gt; 
 
\t <xsl:for-each select="Address"> 
 
\t  <xsl:value-of select="HouseNumber"/> <xsl:value-of select="Street"/> <xsl:value-of select="Country"/> &lt;/Address&gt; </br> 
 
\t &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> 
 
\t &lt;PostalCode&gt;<xsl:value-of select="PostalCode"/>&lt;/PostalCode&gt;</br> 
 
\t </xsl:for-each> 
 
\t </xsl:for-each> 
 
\t &lt;ItemName&gt;<xsl:value-of select="Name"/>&lt;/ItemName&gt;</br> 
 
\t &lt;ItemType&gt;<xsl:value-of select="Type"/>&lt;/ItemType&gt;</br> 
 
\t &lt;Author&gt;<xsl:value-of select="Author"/>&lt;/Author&gt;</br> 
 
\t &lt;list-price&gt;<xsl:value-of select="ListPrice"/>&lt;/list-price&gt;</br> 
 
\t &lt;sell-price&gt;<xsl:value-of select="SellPrice"/>&lt;sell-price&gt;</br> 
 
\t &lt;cost-price&gt;<xsl:value-of select="Cost"/>&lt;/cost-price&gt;</br></p> 
 
\t </xsl:for-each> 
 
    </body> 
 
    </html> 
 
</xsl:stylesheet> 
 
\t  \t

+2

您已將所需輸出顯示爲XML,但您嘗試使用的XSLT包含'html'。你需要XML還是HTML作爲輸出? –

+0

@AniketV我希望XSLT的輸出看起來像xml。 –

+0

那麼您發佈的XSLT代碼究竟與您的問題有什麼關係?你只是發佈任何代碼? – Tomalak

回答

1

請嘗試以下XSLT以得到所需的輸出。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" /> 
    <xsl:strip-space elements="*"/> 
    <xsl:variable name="space" select="' '" /> 
    <xsl:variable name="comma" select="','" /> 

    <!-- identity transform --> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Item"> 
     <xsl:copy> 
      <Code><xsl:value-of select="Code" /></Code> 
      <CustomerID><xsl:value-of select="Customer/@id" /></CustomerID> 
      <FullName> 
       <xsl:value-of select="concat(Customer/Title, $space, Customer/FirstName, $space, Customer/MiddleName, $space, Customer/LastName)" /> 
      </FullName> 
      <Address> 
       <xsl:value-of select="concat(Customer/Address/HouseNumber, $comma, Customer/Address/Street, $comma, Customer/Address/Country)" /> 
      </Address> 
      <ContactNumbers> 
       <xsl:for-each select="Customer/Address/ContactNumber/*[contains(local-name(),'PhoneNumber')]"> 
        <xsl:value-of select="." /> 
        <xsl:if test="position() != last()"> 
         <xsl:value-of select="$comma" /> 
        </xsl:if> 
       </xsl:for-each> 
      </ContactNumbers> 
      <PostalCode><xsl:value-of select="Customer/Address/PostalCode" /></PostalCode> 
      <ItemName><xsl:value-of select="Name" /></ItemName> 
      <ItemType><xsl:value-of select="Type" /></ItemType> 
      <Author><xsl:value-of select="Author" /></Author> 
      <list-price><xsl:value-of select="ListPrice" /></list-price> 
      <sell-price><xsl:value-of select="SellPrice" /></sell-price> 
      <cost-price><xsl:value-of select="Cost" /></cost-price> 
      <profit><xsl:value-of select="format-number(SellPrice - Cost, '0.##')" /></profit> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

我也需要包含標籤。 –

+0

需要包含哪些標籤? XSLT的輸出與您共享的輸出相匹配。 –

+0

我編輯了我的問題。您可以在第二個代碼之前檢查編輯 –