使用XSLT进行XML到XML转换

时间:2017-01-09 06:01:44

标签: xml xslt

我必须将XML转换为另一种XML结构 源XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <urn:LookupRecords xmlns:urn="urn:RedIron.RetailRepository.Services.SearchService" xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
      <urn:query>
        <a:Headers>
          <a:SearchHeader>
            <a:Name>MAX_RESULTS</a:Name>
            <a:ProviderGuid>00000000-0000-0000-0000-000000000000</a:ProviderGuid>
            <a:Value>10</a:Value>
          </a:SearchHeader>
          <a:SearchHeader>
            <a:Name>CASHIER</a:Name>
            <a:Value>1706</a:Value>
          </a:SearchHeader>
          <a:SearchHeader>
            <a:Name>RECEIPT_LOOKUP</a:Name>
            <a:Value>CustomerReceipt</a:Value>
          </a:SearchHeader>
        </a:Headers>
        <a:Params>
          <arr:KeyValueOfguidArrayOfQueryParametertmL6yAXy>
            <arr:Key>{4de38819-43bf-4333-a17b-049ec32703c3}</arr:Key>
            <arr:Value>
              <a:QueryParameter>
                <a:Name>TRAINING_MODE</a:Name>
                <a:Value>FALSE</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>STORE</a:Name>
                <a:Value>121</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>REGISTER</a:Name>
                <a:Value>2</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>TRANSACTION_NUMBER</a:Name>
                <a:Value>1843</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>TRANSACTION_DATE_YEAR</a:Name>
                <a:Value>2016</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>TRANSACTION_DATE_MONTH</a:Name>
                <a:Value>12</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>TRANSACTION_DATE_DAY</a:Name>
                <a:Value>20</a:Value>
              </a:QueryParameter>
              <a:QueryParameter>
                <a:Name>CONCEPT</a:Name>
                <a:Value>WS</a:Value>
              </a:QueryParameter>
            </arr:Value>
          </arr:KeyValueOfguidArrayOfQueryParametertmL6yAXy>
        </a:Params>
      </urn:query>
    </urn:LookupRecords>
  </soapenv:Body>
</soapenv:Envelope>

输出XML应为:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService">
      <GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Exception i:nil="true"/>
        <a:ResponseHeaders>
          <a:Header>
            <a:Name>RECEIPT_LOOKUP</a:Name>
            <a:Value/>
          </a:Header>
          <a:Header>
            <a:Name>CASHIER</a:Name>
            <a:Value>1111</a:Value>
          </a:Header>
          <a:Header>
            <a:Name>STORE</a:Name>
            <a:Value>281</a:Value>
          </a:Header>
        </a:ResponseHeaders>
        <a:SearchResults>
          <a:Result>
            <a:RecordData>
              <Transaction xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <TransactionKey>
                  <TransactionNumber>8407</TransactionNumber>
                  <StoreNumber>281</StoreNumber>
                  <RegisterNumber>9</RegisterNumber>
                  <TransactionStatus>0</TransactionStatus>
                  <POSMode>0</POSMode>
                  <BusinessDate>
                    <Year>2016</Year>
                    <Month>12</Month>
                    <Day>12</Day>
                    <Hour>0</Hour>
                    <Minute>0</Minute>
                    <Second>0</Second>
                  </BusinessDate>
                </TransactionKey>
                <TransactionData>
                  <CashierId>1111</CashierId>
                  <SalesPersonId>1111</SalesPersonId>
                  <ReferencedTransactionNumber>0</ReferencedTransactionNumber>
                  <TransactionTimeStamp>
                    <Year>2016</Year>
                    <Month>12</Month>
                    <Day>12</Day>
                    <Hour>19</Hour>
                    <Minute>43</Minute>
                    <Second>57</Second>
                  </TransactionTimeStamp>
                </TransactionData>
                <MerchandiseItem>
                  <Header>
                    <RecordSequenceNumber>1</RecordSequenceNumber>
                  </Header>
                  <Data>
                    <Sku>100214</Sku>
                    <Department>911</Department>
                    <Quantity>1.0000</Quantity>
                    <ExtendedSellingPrice>10.0000</ExtendedSellingPrice>
                    <ExtendedOriginalPrice>10.0000</ExtendedOriginalPrice>
                    <Description>Test Item 9</Description>
                    <ItemVoidFlag>false</ItemVoidFlag>
                    <ItemReturnFlag>false</ItemReturnFlag>
                    <PriceOverride>false</PriceOverride>
                    <EmployeeSaleFlag>false</EmployeeSaleFlag>
                    <AdditionalAmount>0.00</AdditionalAmount>
                    <PLUFlag>true</PLUFlag>
                    <DiscountFlag>false</DiscountFlag>
                    <ProfilePromptFlag>false</ProfilePromptFlag>
                    <PriceLevel>0</PriceLevel>
                    <QuantityCluster>0</QuantityCluster>
                    <LinkedItem>false</LinkedItem>
                    <ScannedItem>false</ScannedItem>
                    <PriceOverrideReason>0</PriceOverrideReason>
                    <PromotionNumber>0</PromotionNumber>
                    <MixMatchNumber>0</MixMatchNumber>
                    <ExtendedOrigPriceWithPriceOverride>10.00</ExtendedOrigPriceWithPriceOverride>
                    <PriceLevel2>0.00</PriceLevel2>
                    <TransactionTaxAllocation>
                      <TaxProrations>
                        <TaxProration>
                          <index>1</index>
                          <amount>0.8500</amount>
                          <jurisdiction xsi:nil="true"/>
                        </TaxProration>
                      </TaxProrations>
                    </TransactionTaxAllocation>
                  </Data>
                  <Annotations>
                    <Annotation Source="OrderEntry">
                      <IsOrderEntryItem>False</IsOrderEntryItem>
                    </Annotation>
                  </Annotations>
                  <ResultAnnotations>
                    <SearchResultAnnotation>
                      <ProviderGuid>7283d946-62bf-46cf-8e1d-47599e895894</ProviderGuid>
                      <Annotations>
                        <XElement>
                          <ReturnMerchItem>
                            <ReturnableQuantity>1.0000</ReturnableQuantity>
                            <ReturnableValue>10.0000</ReturnableValue>
                          </ReturnMerchItem>
                        </XElement>
                      </Annotations>
                    </SearchResultAnnotation>
                  </ResultAnnotations>
                </MerchandiseItem>
                <Tender>
                  <Header>
                    <RecordSequenceNumber>9</RecordSequenceNumber>
                  </Header>
                  <Data>
                    <TenderId>5</TenderId>
                    <TenderAmount>10.8500</TenderAmount>
                    <ChangeDue>0.0000</ChangeDue>
                    <ExchangeAmount>0.0000</ExchangeAmount>
                    <MaskedCardNumber>523345******0102</MaskedCardNumber>
                    <PaymentToken>hNL4MB7lFPF176spN8tJ</PaymentToken>
                    <MatchingToken>4137452508934584316</MatchingToken>
                    <CardholderName>ND 2/MASTERCARD TRACK 1 A</CardholderName>
                  </Data>
                </Tender>
                <Receipts>
                  <Receipt>
                    <Header>
                      <RecordType>CustomerReceipt</RecordType>
                    </Header>
                    <Data>
                      <Type>CustomerReceipt</Type>                                         
                    </Data>
                  </Receipt>
                </Receipts>
              </Transaction>
            </a:RecordData>
            <a:ResponseHeaders i:nil="true"/>
            <a:TransactionKey>
              <a:BusinessDate>2016-12-12T00:00:00</a:BusinessDate>
              <a:POSMode>0</a:POSMode>
              <a:RegisterNumber>9</a:RegisterNumber>
              <a:StoreNumber>281</a:StoreNumber>
              <a:TransactionNumber>8407</a:TransactionNumber>
              <a:TransactionStatus>0</a:TransactionStatus>
            </a:TransactionKey>
          </a:Result>
        </a:SearchResults>
        <a:StatusCode>Success</a:StatusCode>
        <a:StatusCodeReason i:nil="true"/>
      </GetRecordResult>
    </GetRecordResponse>
  </s:Body>
</s:Envelope>

现在我正在尝试创建一个XSLT,如:

<xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
     xmlns:a="urn:RedIron.RetailRepository.Core"
      version="2.0">
<xsl:output indent="yes"/>
 <xsl:template match = "a:Headers" > 
     <s:Envelope> 
       <s:Body>
        <xsl:element name="GetRecordResponse" namespace="urn:RedIron.RetailRepository.Services.SearchService"> 
            <GetRecordResponse xmlns="urn:RedIron.RetailRepository.Services.SearchService" > 
             <GetRecordResult xmlns:a="urn:RedIron.RetailRepository.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
              <a:Exception i:nil="true">
                <a:ResponseHeaders>

                 <xsl:copy-of select="a:SearchHeader"/>

                </a:ResponseHeaders>              
             </a:Exception>
             </GetRecordResult>
            </GetRecordResponse> 
        </xsl:element>
        </s:Body>
      </s:Envelope>             
   </xsl:template> 
</xsl:stylesheet>

现在我无法更改

<a:SearchHeader><a:Header>

请告诉我,我是否遵循正确的方法,或者有更简单的方法来做同样的事情。

1 个答案:

答案 0 :(得分:0)

假设您想保留相同的内容并只更改标记名称,您可以遵循此方法(简化输入)

<root>
 <car>
    <color>red</color>
    <year>2000</year>
 </car>
 <car>
    <color>blue</color>
    <year>2010</year>
 </car>
</root>

使用此xsl

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output indent="yes"/>

<xsl:template match="/">
    <root>
        <xsl:apply-templates select="//car"/>
    </root>
</xsl:template>
<xsl:template match="car">
    <newTagForCar>
        <xsl:copy-of select="./*"/>
    </newTagForCar>
</xsl:template>

</xsl:stylesheet>

给你这个结果

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <newTagForCar>
  <color>red</color>
  <year>2000</year>
 </newTagForCar>
 <newTagForCar>
  <color>blue</color>
  <year>2010</year>
 </newTagForCar>
</root>