2014-05-13 97 views
0

我必須在下面實現。基於其他元素的值移除XML元素

if if <m_control>/<initiator_id> is Dummy then xml element <note>/<reason> should be removed.Below is the hirarchy of note element。

<o:m_content/o:application/o:product/o:client_specific_illustration/o:note> 

下面是示例XML:

<?xml version="1.0"?> 
<message xmlns="http://www.origoservices.com" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#"> 

<m_control> 
    <expected_response_type>synchronous</expected_response_type> 
    <initiator_id>Dummy</initiator_id> 
    <user_id>Dummy</user_id> 
    <responder_id>Responder</responder_id> 
</m_control> 
<m_content> 
    <b_control> 
     <message_version_number>3.7</message_version_number> 
     <submission_date>2014-04-14</submission_date> 
    </b_control> 
<intermediary type="Test"> 
    <rdr_basis_of_sale> 
     <advised_category>Independent</advised_category> 
    </rdr_basis_of_sale> 
</intermediary> 
<application> 
    <address id="ADPC2"> 
     <postcode>AB24 3DB</postcode> 
    </address> 
    <address id="ADPC1"> 
     <postcode>B14 7JG</postcode> 
    </address> 
    <personal_client id="PC1"> 
     <title>Mr</title> 
     <forenames>Test</forenames> 
     <surname>FLtwelve</surname> 
     <sex>Male</sex> 
     <marital_status>Married</marital_status> 
     <date_of_birth>1950-10-16</date_of_birth> 
     <employment_contract> 
      <occupation code="AAB00021">Actuary</occupation> 
      <full_time_ind>No</full_time_ind> 
     </employment_contract> 
     <smoker_ind>No</smoker_ind> 
     <residential_status>In Own Home - With Someone Else</residential_status> 
     <home_address address_id="ADPC1"/> 
     <enhanced_underwriting> 
      <medical_conditions/> 
     </enhanced_underwriting> 
     <tpsdata> 
      <postcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test</postcode> 
     </tpsdata> 
    </personal_client> 
    <personal_client id="PC2"> 
     <employment_contract> 
      <occupation code="WAB02558">Wig Maker</occupation> 
      <full_time_ind>Yes</full_time_ind> 
     </employment_contract> 
     <smoker_ind>Yes</smoker_ind> 
     <residential_status>In Own Home - Alone</residential_status> 
     <home_address address_id="ADPC2"/> 
     <enhanced_underwriting> 
      <medical_conditions/> 
      <lifestyle> 
       <height units="Centimetre">180</height> 
       <weight units="Kilogram">70</weight> 
       <waist units="Centimetre">81</waist> 
       <units_of_alcohol_per_week>1</units_of_alcohol_per_week> 
       <smoking_details> 
        <regular_smoker_ind>Yes</regular_smoker_ind> 
        <current_smoking> 
         <number_of_cigarettes_per_day>4</number_of_cigarettes_per_day> 
         <number_of_cigars_per_day>0</number_of_cigars_per_day> 
         <rolling_tobacco_per_week units="Gram">0</rolling_tobacco_per_week> 
         <pipe_tobacco_per_week units="Gram">0</pipe_tobacco_per_week> 
         <start_date>1985-09</start_date> 
        </current_smoking> 
       </smoking_details> 
      </lifestyle> 
     </enhanced_underwriting> 
     <tpsdata> 
      <postcode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">AB24 3DB</postcode> 
     </tpsdata> 
    </personal_client> 
    <product type="Compulsory Purchase Annuity" product_code="CPA"> 
     <open_market_option_ind>Yes</open_market_option_ind> 
     <annuity type="Non Protected Rights"> 
      <annuitant sequence_number="1" personal_client_id="PC1"/> 
      <with_profit_ind>No</with_profit_ind> 
      <contribution legislation_applicable="Post 1997"> 
       <amount currency="GBP">391586</amount> 
       <source_details> 
        <product_type>Occupational Scheme - Defined Contribution</product_type> 
        <product_provider_name>Other</product_provider_name> 
        <transfer_ind>No</transfer_ind> 
       </source_details> 
       <adviser_charges_applicable> 
        <adviser_charge_applicable adviser_charge_id="ac1"/> 
       </adviser_charges_applicable> 
      </contribution> 
      <payment_frequency>Annually</payment_frequency> 
      <payment_timing_code>In Advance</payment_timing_code> 
      <escalation> 
       <change_index>Level</change_index> 
       <lpi_lag_basis>Statutory</lpi_lag_basis> 
       <proportionate_escalation_ind>No</proportionate_escalation_ind> 
      </escalation> 
      <payment_period> 
       <start_basis>Specified Date</start_basis> 
       <start_date>2014-04-14</start_date> 
      </payment_period> 
      <guaranteed_period> 
       <years>5</years> 
      </guaranteed_period> 
      <commuted_ind>No</commuted_ind> 
      <with_proportion_ind>No</with_proportion_ind> 
      <reversionary_annuity type="Spouse" legislation_applicable="Post 1997"> 
       <annuitant personal_client_id="PC2"/> 
       <number_of_dependants>1</number_of_dependants> 
       <fraction_of_original_payment> 
        <numerator>10</numerator> 
        <denominator>10</denominator> 
       </fraction_of_original_payment> 
       <payment_period> 
        <start_basis>Next Due Date</start_basis> 
       </payment_period> 
       <overlap_ind>No</overlap_ind> 
       <spouse_remarriage_cease_ind>Yes</spouse_remarriage_cease_ind> 
      </reversionary_annuity> 
     </annuity> 
     <adviser_charges> 
      <adviser_charge id="ac1"> 
       <type>Adviser</type> 
       <amount currency="GBP">7831.72</amount> 
       <facilitated_from>Annuity In Payment</facilitated_from> 
       <facilitated_before_product_investment_ind>Yes</facilitated_before_product_investment_ind> 
       <payment_frequency>Single</payment_frequency> 
       <reason>Initial</reason> 
      </adviser_charge> 
     </adviser_charges> 
     <illustration_basis> 
      <annuity_calculation_required>Payment</annuity_calculation_required> 
     </illustration_basis> 
     <client_specific_illustration> 
      <expiry_date>2014-04-28</expiry_date> 
      <note> 
       <reason>reason for failure is specified over here</reason> 
      </note> 
      <pension_annuity type="Non Protected Rights"> 
       <total_amount currency="GBP">18539.33</total_amount> 
       <reversionary_annuity> 
        <total_amount currency="GBP">18539.33</total_amount> 
       </reversionary_annuity> 
      </pension_annuity> 
      <adviser_charges> 
       <adviser_charge> 
        <adviser_charge_requested adviser_charge_id="ac1"/> 
        <type>Adviser</type> 
        <amount currency="GBP">7831.72</amount> 
        <facilitated_from>Annuity In Payment</facilitated_from> 
        <facilitated_before_product_investment_ind>Yes</facilitated_before_product_investment_ind> 
        <payment_frequency>Single</payment_frequency> 
        <reason>Initial</reason> 
       </adviser_charge> 
      </adviser_charges> 
      <tpsdata> 
       <guaranteed_quote>Yes</guaranteed_quote> 
      </tpsdata> 
     </client_specific_illustration> 
    </product> 
    <document_out type="Client Specific Illustration"> 
     <print_requirements> 
      <distribution_method>Web Hosted</distribution_method> 
      <web_host_format>PDF</web_host_format> 
     </print_requirements> 
    </document_out> 
</application> 
</m_content> 
</message> 

以下是我已經嘗試了XSLT,但我沒有得到預期的輸出,當音符/元素的原因是沒有得到清除。

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:o="http://www.origoservices.com" xmlns:dp="http://www.datapower.com/extensions" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:date="http://exslt.org/dates-and-times" version="1.0" extension-element-prefixes="dp" exclude-result-prefixes="fn date"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="o:m_content/o:application/o:product/o:client_specific_illustration/o:note[../../../../../o:m_control/o:initiator_id='TEX']"> 

    </xsl:template> 
</xsl:stylesheet> 

有沒有人請讓我知道,我在哪裏comiting錯誤?

問候。

回答

1

只需要一個變量來存儲initiator_id。如下圖所示:

<xsl:variable name="test" select="o:message/o:m_control/o:initiator_id"/> 

然後,測試note節點

<xsl:template match="o:note[parent::o:client_specific_illustration]"> 
    <xsl:choose> 
     <xsl:when test="$test='Dummy'"></xsl:when> 
     <xsl:otherwise> 
      <xsl:copy> 
       <xsl:apply-templates/> 
      </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

因此,完整的樣式表是:

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:o="http://www.origoservices.com" xmlns:dp="http://www.datapower.com/extensions" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:date="http://exslt.org/dates-and-times" 
    version="1.0" extension-element-prefixes="dp" exclude-result-prefixes="fn date"> 

    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:variable name="test" select="o:message/o:m_control/o:initiator_id"/> 

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

    <xsl:template match="o:note[parent::o:client_specific_illustration]"> 
     <xsl:choose> 
      <xsl:when test="$test='Dummy'"></xsl:when> 
      <xsl:otherwise> 
       <xsl:copy> 
        <xsl:apply-templates/> 
       </xsl:copy> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

作爲替代,你可以使用

<xsl:template match="o:m_content[preceding-sibling::o:m_control/o:initiator_id='Dummy']/o:application/o:product/o:client_specific_illustration/o:note"/> 
+0

謝謝Joel。我有兩個問題1)這個xslt將從源xml中刪除所有其他元素,我需要刪除只有特定的note元素 2)我開發的xslt有什麼問題? 謝謝 – user2607367

+0

對於第(1)項,我編輯了我的答案。對於第(2)項,值s/b'Dummy'。你有'TEX' –