XSL转换 - 排除某些孩子

时间:2011-02-15 15:15:43

标签: xslt

如果写得不好而道歉,但这是我第一次接触XSLT。

以下是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>john.henry@sampledata.com</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>thomas.henry@sampledata.com</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>thomas.henry@sampledata.com</EmailAddress>
        <City>Bristol</City>
        <PostalCode>BS4Y 2WT</PostalCode>
      </ContactInformation>
    </Customer>
  </Delivery>
</Order>

我正在尝试在转型期间做很多事情。

  1. 从XML
  2. 中提取<Order>
  3. <Customer>
  4. 中排除<Delivery>
  5. <Customer>所在的<Billing>插入<Delivery><Customer>
  6. 我假设我可以使用以下内容提取订单..

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

    一旦我开始尝试排除客户,我就完全迷失了......

    非常感谢任何帮助。

    非常感谢,

    微米。

1 个答案:

答案 0 :(得分: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>