2011-02-15 34 views
0

如果這篇文章寫得不好,但是這是我第一次接觸XSLT。XSL轉換 - 排除某些兒童

以下是xml結構示例。

<ProcessedOrder> 
    <ProcessDetail> 
    <Detail1>Some Sample Data</Detail1> 
    </ProcessDetail> 
    <Management> 
    <Employee Id="EM156896"> 
     <Name>James Davies</Name> 
    </Employee> 
    </Management> 
    <Order Id="IR1245486"> 
    <Details> 
     <Date>01-02-2011</Date> 
     <Name>XSLT : The Complete Beginners Guide</Name> 
     <Description>The Complete Beginners Guide to XSL Transformations</Description> 
    </Details> 
    <Delivery> 
     <Customer Id="CN005687"> 
     <Name>John Henry</Name> 
    <ContactInformation> 
     <AddressLine1>55 John Street</AddressLine1> 
     <Country _Id="GBR">United Kingdom</Country> 
     <Phone>1234-123456</Phone> 
     <Fax>1234-5544-2250</Fax> 
     <EmailAddress>[email protected]</EmailAddress> 
     <City>London</City> 
     <PostalCode>AW7T 3XS</PostalCode> 
     </ContactInformation> 
     </Customer> 
    </Delivery> 
    </Order> 
    <Billing> 
    <Customer Id="CN005858"> 
     <Name>Thomas Henry</Name> 
     <ContactInformation> 
     <AddressLine1>66 Thomas Street</AddressLine1> 
     <Country _Id="GBR">United Kingdom</Country> 
     <Phone>1234-545464</Phone> 
     <Fax>2233-8989-1234</Fax> 
     <EmailAddress>[email protected]</EmailAddress> 
     <City>Bristol</City> 
     <PostalCode>BS4Y 2WT</PostalCode> 
     </ContactInformation> 
    </Customer> 
    </Billing> 
</ProcessedOrder> 

//所需的輸出

<Order Id="IR1245486"> 
    <Details> 
    <Date>01-02-2011</Date> 
    <Name>XSLT : The Complete Beginners Guide</Name> 
    <Description>The Complete Beginners Guide to XSL Transformations</Description>  
    </Details> 
    <Delivery> 
    <Customer Id="CN005858"> 
     <Name>Thomas Henry</Name> 
     <ContactInformation> 
     <AddressLine1>66 Thomas Street</AddressLine1> 
     <Country _Id="GBR">United Kingdom</Country> 
     <Phone>1234-545464</Phone> 
     <Fax>2233-8989-1234</Fax> 
     <EmailAddress>[email protected]</EmailAddress> 
     <City>Bristol</City> 
     <PostalCode>BS4Y 2WT</PostalCode> 
     </ContactInformation> 
    </Customer> 
    </Delivery> 
</Order> 

我試圖轉型過程中做了一些事情。

  1. 從XML
  2. 提取<Order><Billing>其中<Delivery><Customer>是排除來自<Delivery>
  3. 插入<Customer><Customer>

我假設我可以使用下面的提取訂單..

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
    <xsl:apply-templates select="//Order"/> 
    </xsl:template> 

    <xsl:template match="Order"> 
    <xsl:copy-of select="."/>  
    </xsl:template> 
</xsl:stylesheet> 

一旦我開始試圖排除客戶我完全迷路..

任何幫助將是不勝感激。

非常感謝,

M.

回答

0

這裏是一個樣本樣式應該通過添加模板做的工作,允許進一步定製:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

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

    <xsl:template match="/"> 
    <xsl:apply-templates select="ProcessedOrder/Order"/> 
    </xsl:template> 

    <xsl:template match="Order/Delivery/Customer"> 
    <xsl:apply-templates select="/ProcessedOrder/Billing/Customer"/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

馬丁,萬分感謝了回來我在這。我將貫穿您提出的解決方案。乍一看,它似乎比我試圖實施自己的解決方案的一些更合乎邏輯。 – user617850 2011-02-15 16:15:23