XSLT issue with renaming the elements -- changing namespace - xslt

I am trying to rename/remove the namespace prefix from the output XML.
However, one of the top element should contain the namespace declaration without prefix.
The XSLT is not working properly. In some instances I can still see the namespace prefix in the output XML and the namespace doesn't show up in the element I want to see.
I've tried to use the exclude-result-prefixes and some templates but not working properly.
Just to clarify I'm using XSLT 1.0
Thanks in Advance ...
XSLT:
<?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"
xmlns:ns0="http://somenamespace"
xmlns:ac="http://ac.namespace"
exclude-result-prefixes="ns0 msxsl ac">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="utf-8"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="node()|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!--<xsl:template match="#*[local-name(.)='ns0']"/>-->
<xsl:template match="ns0:Cedent/ns0:Party/ns0:Id[. = '']">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="../../following-sibling::ns0:Broker[1]/ns0:Party/ns0:Id/node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Cedent/ns0:Party/ns0:Id/#Agency[. = '']">
<xsl:attribute name="Agency">DUNS_dun_and_bradstreet</xsl:attribute>
</xsl:template>
<xsl:template match="ns0:Reinsurer[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/#Agency)]" />
<xsl:template match="ns0:Reinsurer/ns0:Contact[not(ns0:PersonName)]" />
<xsl:template match="ns0:Reinsurer/ns0:Contact/*[not(node())]" />
<xsl:template match="ns0:Broker/ns0:Contact/ns0:Telephone[.='']" />
<xsl:template match="ns0:ServiceProvider[. = '6']" />
<xsl:template match="ns0:ServiceProvider[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/#Agency)]" />
<xsl:template match="ns0:Contract/ns0:ContractGroupName[not(node())]" />
<xsl:template match="ns0:Endorsement[ns0:Placing/ns0:PlacingStage = 'endorsement']" />
<xsl:template match="ns0:Endorsement/ns0:EndorsementReference[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:EndorsementName[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:Description[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:EffectiveDate[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:BrokerSharePercentage[not(node()) and ns0:ContractSection/ns0:BrokerSharePercentage/ns0:Rate > 0]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation[not(node()) or (ns0:PlacingTransactionFunction = 'request_for_line_or_binder' or ns0:PlacingTransactionFunction = 'signed_line_advice' or ns0:PlacingTransactionFunction = 'quotation_request' or ns0:PlacingTransactionFunction = 'endorsement_request')]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Supraentity[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Country[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:PerilsIncluded[not(ns0:Peril/ns0:PerilType !='')]" />
<xsl:template match="ns0:OrderPercentage">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<ns0:LinesPercentageOfOrderIndicator>
<xsl:value-of select="ancestor::ns0:Placing/ns0:PlacingTransactionFunction = 'signed_line_advice' and ns0:Rate > 100"/>
</ns0:LinesPercentageOfOrderIndicator>
</xsl:template>
<xsl:template match="ns0:ContractSection/ns0:Brokerage[descendant::ns0:Rate = '' and not(ns0:ContractSection/ns0:BrokeragePercentage/ns0:Rate > 0)]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:Reinsurer[descendant::ns0:Id = '' and not(ns0:Party/ns0:Id/#Agency[. != ''])]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerContractReference[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerQuoteMaximumSharePercentage[descendant::ns0:Rate = '']" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerWrittenPercentage[descendant::ns0:Rate = '' and ns0:PlacingTransactionFunction = 'signed_line_advice']" />
</xsl:stylesheet>
Sample Input:
<ns0:Root xmlns:ns0="http://somenamespace">
<ns0:Placing Sender="broker" Receiver="serviceprovider">
<ns0:UUId>GUID</ns0:UUId>
<ns0:BrokerReference>2B3B8992-3185-48EE-A030-0F61EFF7C7EB</ns0:BrokerReference>
<ns0:ServiceProviderReference>16</ns0:ServiceProviderReference>
<ns0:PlacingStage>order</ns0:PlacingStage>
<ns0:PlacingTransactionFunction>signed_line_advice</ns0:PlacingTransactionFunction>
<ns0:TransactionReasonDescription></ns0:TransactionReasonDescription>
<ns0:Cedent>
<ns0:Party>
<ns0:Id Agency=""></ns0:Id>
<ns0:Name>Bahamas First General Insurance Co/Aon Benfield Canada</ns0:Name>
</ns0:Party>
</ns0:Cedent>
<ns0:Reinsurer>
<ns0:Party>
<ns0:Id Agency=""></ns0:Id>
<ns0:Name>RI3K</ns0:Name>
</ns0:Party>
<ns0:Contact>
<ns0:PersonName>test</ns0:PersonName>
<ns0:Telephone></ns0:Telephone>
<ns0:Email>test#ri3k.com</ns0:Email>
</ns0:Contact>
</ns0:Reinsurer>
<ns0:Broker>
<ns0:Party>
<ns0:Id Agency="DUNS_dun_and_bradstreet">292320710</ns0:Id>
<ns0:Name>Aon Benfield UK</ns0:Name>
</ns0:Party>
<ns0:Contact>
<ns0:PersonName>Jenny Edwards</ns0:PersonName>
<ns0:Telephone>reinsurance_contract</ns0:Telephone>
<ns0:Email>jenny.edwards#aonbenfield.com</ns0:Email>
</ns0:Contact>
</ns0:Broker>
<ns0:ServiceProvider>
<ns0:Party>
<ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id>
</ns0:Party>
</ns0:ServiceProvider>
<ns0:Contract>
<ns0:ContractName>FINCO QUOTA SHARE TREATY</ns0:ContractName>
<ns0:ContractGroupName>BFG FINCO QUOTA SHARE TREATY</ns0:ContractGroupName>
<ns0:ContractType>0</ns0:ContractType>
<ns0:BrokerReference>B110813BDO1053</ns0:BrokerReference>
<ns0:BrokerGroupReference>200153436</ns0:BrokerGroupReference>
<ns0:BrokerRiskReference>13BDO1053</ns0:BrokerRiskReference>
</ns0:Contract>
<ns0:Endorsement>
<ns0:EndorsementReference>0</ns0:EndorsementReference>
<ns0:EndorsementName></ns0:EndorsementName>
<ns0:Description></ns0:Description>
<ns0:EffectiveDate></ns0:EffectiveDate>
</ns0:Endorsement>
<ns0:ContractSection ContractReportingLevel="section_level">
<ns0:HighLevelReference>01</ns0:HighLevelReference>
<ns0:CoverType>quota_share</ns0:CoverType>
<ns0:ContractPeriod>
<ns0:StartDate DateIndicator="Jan 1 2013 12:00AM"></ns0:StartDate>
<ns0:EndDate DateIndicator="Dec 31 2013 12:00AM"></ns0:EndDate>
</ns0:ContractPeriod>
<ns0:UnderwritingYear>Jan </ns0:UnderwritingYear>
<ns0:ContractSectionClass>
<ns0:JvClassOfBusiness>wind_storm_unspecified</ns0:JvClassOfBusiness>
<ns0:ClassOfBusinessDescription>wind_storm_unspecified</ns0:ClassOfBusinessDescription>
</ns0:ContractSectionClass>
<ns0:RiskLocation>
<ns0:Address>
<ns0:Country></ns0:Country>
</ns0:Address>
<ns0:Location>
<ns0:Supraentity></ns0:Supraentity>
<ns0:Country>BS</ns0:Country>
</ns0:Location>
</ns0:RiskLocation>
<ns0:PerilsIncluded>
<ns0:Peril>
<ns0:PerilType></ns0:PerilType>
</ns0:Peril>
</ns0:PerilsIncluded>
<ns0:ContractAmountsCurrency>
<ns0:Ccy>GBP</ns0:Ccy>
</ns0:ContractAmountsCurrency>
<ns0:BrokerSharePercentage>
<ns0:Rate RateUnit="percentage">2.500000</ns0:Rate>
</ns0:BrokerSharePercentage>
<ns0:OrderPercentage>
<ns0:Rate RateUnit="percentage">41.000000</ns0:Rate>
</ns0:OrderPercentage>
<ns0:Brokerage>
<ns0:BrokeragePercentage>
<ns0:Rate RateUnit="percentage">2.500000</ns0:Rate>
</ns0:BrokeragePercentage>
</ns0:Brokerage>
<ns0:ContractMarket>
<ns0:Reinsurer>
<ns0:Party>
<ns0:Id Agency="DUNS_dun_and_bradstreet">239195295</ns0:Id>
</ns0:Party>
</ns0:Reinsurer>
<ns0:ParticipantFunction>leader</ns0:ParticipantFunction>
<ns0:ReinsurerContractReference></ns0:ReinsurerContractReference>
<ns0:ReinsurerQuoteMaximumSharePercentage>
<ns0:Rate RateUnit="percentage">100.000000</ns0:Rate>
</ns0:ReinsurerQuoteMaximumSharePercentage>
<ns0:ReinsurerWrittenPercentage>
<ns0:Rate RateUnit="percentage">100.000000</ns0:Rate>
</ns0:ReinsurerWrittenPercentage>
</ns0:ContractMarket>
</ns0:ContractSection>
</ns0:Placing>
</ns0:Root>
Expected OutPut:
<Root xmlns="http://somenamespace" xmlns="http://ac.namespace">
<Placing xmlns="http://somenamespace" Sender="broker" Receiver="serviceprovider">
<UUId>GUID</UUId>
<BrokerReference>2B3B8992-3185-48EE-A030-0F61EFF7C7EB</BrokerReference>
<ServiceProviderReference>16</ServiceProviderReference>
<PlacingStage>order</PlacingStage>
<PlacingTransactionFunction>signed_line_advice</PlacingTransactionFunction>
<TransactionReasonDescription />
<Cedent>
<Party>
<Id Agency="DUNS_dun_and_bradstreet" >292320710</Id>
<Name>Bahamas First General Insurance Co/Aon Benfield Canada</Name>
</Party>
</Cedent>
<Reinsurer>
<Party>
<Id Agency="" />
<Name>RI3K</Name>
</Party>
<Contact>
<PersonName>test</PersonName>
<Email>test#ri3k.com</Email>
</Contact>
</Reinsurer>
<Broker>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">292320710</Id>
<Name>Aon Benfield UK</Name>
</Party>
<Contact>
<PersonName>Jenny Edwards</PersonName>
<Telephone>reinsurance_contract</Telephone>
<Email>jenny.edwards#aonbenfield.com</Email>
</Contact>
</Broker>
<ServiceProvider>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">239195295</Id>
</Party>
</ServiceProvider>
<Contract>
<ContractName>FINCO QUOTA SHARE TREATY</ContractName>
<ContractGroupName>BFG FINCO QUOTA SHARE TREATY</ContractGroupName>
<ContractType>0</ContractType>
<BrokerReference>B110813BDO1053</BrokerReference>
<BrokerGroupReference>200153436</BrokerGroupReference>
<BrokerRiskReference>13BDO1053</BrokerRiskReference>
</Contract>
<Endorsement>
<EndorsementReference>0</EndorsementReference>
</Endorsement>
<ContractSection ContractReportingLevel="section_level">
<HighLevelReference>01</HighLevelReference>
<CoverType>quota_share</CoverType>
<ContractPeriod>
<StartDate DateIndicator="Jan 1 2013 12:00AM" />
<EndDate DateIndicator="Dec 31 2013 12:00AM" />
</ContractPeriod>
<UnderwritingYear>Jan </UnderwritingYear>
<ContractSectionClass>
<JvClassOfBusiness>wind_storm_unspecified</JvClassOfBusiness>
<ClassOfBusinessDescription>wind_storm_unspecified</ClassOfBusinessDescription>
</ContractSectionClass>
<RiskLocation>
<Address>
<Country />
</Address>
<Location>
<Country>BS</Country>
</Location>
</RiskLocation>
<ContractAmountsCurrency>
<Ccy>GBP</Ccy>
</ContractAmountsCurrency>
<BrokerSharePercentage>
<Rate RateUnit="percentage">2.500000</Rate>
</BrokerSharePercentage>
<OrderPercentage >
<Rate RateUnit="percentage">41.000000</Rate>
</OrderPercentage>
<LinesPercentageOfOrderIndicator>false</LinesPercentageOfOrderIndicator>
<Brokerage>
<BrokeragePercentage>
<Rate RateUnit="percentage">2.500000</Rate>
</BrokeragePercentage>
</Brokerage>
<ContractMarket>
<Reinsurer>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">239195295</Id>
</Party>
</Reinsurer>
<ParticipantFunction>leader</ParticipantFunction>
<ReinsurerQuoteMaximumSharePercentage>
<Rate RateUnit="percentage">100.000000</Rate>
</ReinsurerQuoteMaximumSharePercentage>
<ReinsurerWrittenPercentage>
<Rate RateUnit="percentage">100.000000</Rate>
</ReinsurerWrittenPercentage>
</ContractMarket>
</ContractSection>
</Placing>
</Root>

This XSLT does what I think you are actually trying to do (i.e. keep everything in its original namespace, and not exclude Root from it):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:ns0="http://somenamespace"
xmlns="http://somenamespace"
xmlns:ac="http://ac.namespace"
exclude-result-prefixes="ns0 msxsl ac">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes"
omit-xml-declaration="yes" encoding="utf-8"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*" />
</xsl:copy>
</xsl:template>
<xsl:template match="node()" priority="-2">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" name="CopyElement">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!--<xsl:template match="#*[local-name(.)='ns0']"/>-->
<xsl:template match="ns0:Cedent/ns0:Party/ns0:Id[. = '']">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="#*" />
<xsl:apply-templates
select="../../following-sibling::ns0:Broker[1]/ns0:Party/ns0:Id/node()" />
</xsl:element>
</xsl:template>
<xsl:template match="ns0:Cedent/ns0:Party/ns0:Id/#Agency[. = '']">
<xsl:attribute name="Agency">DUNS_dun_and_bradstreet</xsl:attribute>
</xsl:template>
<xsl:template match="ns0:Reinsurer[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/#Agency)]" />
<xsl:template match="ns0:Reinsurer/ns0:Contact[not(ns0:PersonName)]" />
<xsl:template match="ns0:Reinsurer/ns0:Contact/*[not(node())]" />
<xsl:template match="ns0:Broker/ns0:Contact/ns0:Telephone[.='']" />
<xsl:template match="ns0:ServiceProvider[. = '6']" />
<xsl:template match="ns0:ServiceProvider[not(ns0:Party/ns0:Id and ns0:Party/ns0:Id/#Agency)]" />
<xsl:template match="ns0:Contract/ns0:ContractGroupName[not(node())]" />
<xsl:template match="ns0:Endorsement[ns0:Placing/ns0:PlacingStage = 'endorsement']" />
<xsl:template match="ns0:Endorsement/ns0:EndorsementReference[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:EndorsementName[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:Description[not(node())]" />
<xsl:template match="ns0:Endorsement/ns0:EffectiveDate[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:BrokerSharePercentage[not(node()) and ns0:ContractSection/ns0:BrokerSharePercentage/ns0:Rate > 0]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation[not(node()) or (ns0:PlacingTransactionFunction = 'request_for_line_or_binder' or ns0:PlacingTransactionFunction = 'signed_line_advice' or ns0:PlacingTransactionFunction = 'quotation_request' or ns0:PlacingTransactionFunction = 'endorsement_request')]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Supraentity[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:RiskLocation/ns0:Location/ns0:Country[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:PerilsIncluded[not(ns0:Peril/ns0:PerilType !='')]" />
<xsl:template match="ns0:OrderPercentage">
<xsl:call-template name="CopyElement" />
<LinesPercentageOfOrderIndicator>
<xsl:value-of select="ancestor::ns0:Placing/ns0:PlacingTransactionFunction = 'signed_line_advice' and ns0:Rate > 100"/>
</LinesPercentageOfOrderIndicator>
</xsl:template>
<xsl:template match="ns0:ContractSection/ns0:Brokerage[descendant::ns0:Rate = '' and not(ns0:ContractSection/ns0:BrokeragePercentage/ns0:Rate > 0)]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:Reinsurer[descendant::ns0:Id = '' and not(ns0:Party/ns0:Id/#Agency[. != ''])]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerContractReference[not(node())]" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerQuoteMaximumSharePercentage[descendant::ns0:Rate = '']" />
<xsl:template match="ns0:ContractSection/ns0:ContractMarket/ns0:ReinsurerWrittenPercentage[descendant::ns0:Rate = '' and ns0:PlacingTransactionFunction = 'signed_line_advice']" />
</xsl:stylesheet>
When run on your sample input, the result is:
<Root xmlns="http://somenamespace">
<Placing Sender="broker" Receiver="serviceprovider">
<UUId>GUID</UUId>
<BrokerReference>2B3B8992-3185-48EE-A030-0F61EFF7C7EB</BrokerReference>
<ServiceProviderReference>16</ServiceProviderReference>
<PlacingStage>order</PlacingStage>
<PlacingTransactionFunction>signed_line_advice</PlacingTransactionFunction>
<TransactionReasonDescription />
<Cedent>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">292320710</Id>
<Name>Bahamas First General Insurance Co/Aon Benfield Canada</Name>
</Party>
</Cedent>
<Reinsurer>
<Party>
<Id Agency="" />
<Name>RI3K</Name>
</Party>
<Contact>
<PersonName>test</PersonName>
<Email>test#ri3k.com</Email>
</Contact>
</Reinsurer>
<Broker>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">292320710</Id>
<Name>Aon Benfield UK</Name>
</Party>
<Contact>
<PersonName>Jenny Edwards</PersonName>
<Telephone>reinsurance_contract</Telephone>
<Email>jenny.edwards#aonbenfield.com</Email>
</Contact>
</Broker>
<ServiceProvider>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">239195295</Id>
</Party>
</ServiceProvider>
<Contract>
<ContractName>FINCO QUOTA SHARE TREATY</ContractName>
<ContractGroupName>BFG FINCO QUOTA SHARE TREATY</ContractGroupName>
<ContractType>0</ContractType>
<BrokerReference>B110813BDO1053</BrokerReference>
<BrokerGroupReference>200153436</BrokerGroupReference>
<BrokerRiskReference>13BDO1053</BrokerRiskReference>
</Contract>
<Endorsement>
<EndorsementReference>0</EndorsementReference>
</Endorsement>
<ContractSection ContractReportingLevel="section_level">
<HighLevelReference>01</HighLevelReference>
<CoverType>quota_share</CoverType>
<ContractPeriod>
<StartDate DateIndicator="Jan 1 2013 12:00AM" />
<EndDate DateIndicator="Dec 31 2013 12:00AM" />
</ContractPeriod>
<UnderwritingYear>Jan </UnderwritingYear>
<ContractSectionClass>
<JvClassOfBusiness>wind_storm_unspecified</JvClassOfBusiness>
<ClassOfBusinessDescription>wind_storm_unspecified</ClassOfBusinessDescription>
</ContractSectionClass>
<RiskLocation>
<Address>
<Country />
</Address>
<Location>
<Country>BS</Country>
</Location>
</RiskLocation>
<ContractAmountsCurrency>
<Ccy>GBP</Ccy>
</ContractAmountsCurrency>
<BrokerSharePercentage>
<Rate RateUnit="percentage">2.500000</Rate>
</BrokerSharePercentage>
<OrderPercentage>
<Rate RateUnit="percentage">41.000000</Rate>
</OrderPercentage>
<LinesPercentageOfOrderIndicator>false</LinesPercentageOfOrderIndicator>
<Brokerage>
<BrokeragePercentage>
<Rate RateUnit="percentage">2.500000</Rate>
</BrokeragePercentage>
</Brokerage>
<ContractMarket>
<Reinsurer>
<Party>
<Id Agency="DUNS_dun_and_bradstreet">239195295</Id>
</Party>
</Reinsurer>
<ParticipantFunction>leader</ParticipantFunction>
<ReinsurerQuoteMaximumSharePercentage>
<Rate RateUnit="percentage">100.000000</Rate>
</ReinsurerQuoteMaximumSharePercentage>
<ReinsurerWrittenPercentage>
<Rate RateUnit="percentage">100.000000</Rate>
</ReinsurerWrittenPercentage>
</ContractMarket>
</ContractSection>
</Placing>
</Root>
Please tell me if you did actually want to exclude Root from the namespace.

Related

Copying parent node value to child nodes

I want to copy the value of parentnode element into child node, as shown below. Can someone point me in the right direction on how to achieve this?
Thank you!
Here is the xml file.
<mainpart id="295928" num="1-MS15" quantity="1">
<explicitQuantity>1</explicitQuantity>
<proxy id="1E2B4D" ACADID="1E2B4E" basepart="58A67">
<singlepart id="24558D" num="1-m81" ncFile="1-m81.nc1" quantity="1">
<explicitQuantity>1</explicitQuantity>
<part id="58A67" name="C4X6.25" class="Beam" ACADID="59582" dstvName="C4X6.25">
<explicitQuantity>1</explicitQuantity>
<role key="Frame" name="FRAME"/>
<length>1727.194</length>
<paintArea>618840.532</paintArea>
<numHoles>8</numHoles>
<ObjectTopLevel>0.739</ObjectTopLevel>
<ObjectBottomLevel>-101.741</ObjectBottomLevel>
<material key="ASTM-A36" name="A36"/>
<coating key="G" name="G"/>
<commodity>FRAME</commodity>
<weight>16064.68</weight>
<exactWeight>15945.55</exactWeight>
<weightPerMeter>9301.02</weightPerMeter>
<density>7850.00</density>
<section key="AISC 14.1 C Channel##§##ChannelsC4X6.25" name="C4X6.25"/>
<sysLength>1727.194</sysLength>
<sawLength>1727.194</sawLength>
<angleX1>0.0000</angleX1>
<angleY1>0.0000</angleY1>
<angleX2>0.0000</angleX2>
<angleY2>0.0000</angleY2>
<SawCutInfo>0</SawCutInfo>
<ElementID>7400</ElementID>
</part>
</singlepart>
<singlepart id="295ADA" num="1-p3" quantity="4">
<explicitQuantity>4</explicitQuantity>
<part id="295C18" name="PL 1/4"x1 3/8"" class="Plate" ACADID="295C14" dstvName="PL 1/4"">
<explicitQuantity>1</explicitQuantity>
<role key="Plate" name="PLATE"/>
<length>85.725</length>
<paintArea>7215.343</paintArea>
<ObjectTopLevel>-7.199</ObjectTopLevel>
<ObjectBottomLevel>-93.396</ObjectBottomLevel>
<material key="ASTM-A36" name="A36"/>
<coating key="G" name="G"/>
<weight>143.81</weight>
<exactWeight>143.81</exactWeight>
<density>7850.00</density>
<thickness>6.350</thickness>
<width>35.535</width>
<area1>2884.914</area1>
<area2>2884.914</area2>
<contourLength>227.640</contourLength>
<ElementID>52306</ElementID>
<ElementID>52304</ElementID>
<ElementID>52303</ElementID>
<ElementID>52305</ElementID>
</part>
</singlepart>
</proxy>
</mainpart>
Here is my xsl, I know it is wrong, but I can't figure out how to fix it.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="mainpart" >
<xsl:variable name="mainpartmark" select="#name" />
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="part">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
<UserAttribute10>"$mainpartmark"</UserAttribute10>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Here is the result I wanted, I want to add the node UserAttr10 on each end of Part node with value of num from mainpart node.
<mainpart id="295928" num="1-MS15" quantity="1">
<explicitQuantity>1</explicitQuantity>
<proxy id="1E2B4D" ACADID="1E2B4E" basepart="58A67">
<singlepart id="24558D" num="1-m81" ncFile="1-m81.nc1" quantity="1">
<explicitQuantity>1</explicitQuantity>
<part id="58A67" name="C4X6.25" class="Beam" ACADID="59582" dstvName="C4X6.25">
<explicitQuantity>1</explicitQuantity>
<role key="Frame" name="FRAME"/>
<length>1727.194</length>
<paintArea>618840.532</paintArea>
<numHoles>8</numHoles>
<ObjectTopLevel>0.739</ObjectTopLevel>
<ObjectBottomLevel>-101.741</ObjectBottomLevel>
<material key="ASTM-A36" name="A36"/>
<coating key="G" name="G"/>
<commodity>FRAME</commodity>
<weight>16064.68</weight>
<exactWeight>15945.55</exactWeight>
<weightPerMeter>9301.02</weightPerMeter>
<density>7850.00</density>
<section key="AISC 14.1 C Channel##§##ChannelsC4X6.25" name="C4X6.25"/>
<sysLength>1727.194</sysLength>
<sawLength>1727.194</sawLength>
<angleX1>0.0000</angleX1>
<angleY1>0.0000</angleY1>
<angleX2>0.0000</angleX2>
<angleY2>0.0000</angleY2>
<SawCutInfo>0</SawCutInfo>
<ElementID>7400</ElementID>
<UserAttr10>1-MS15</UserAttr10> <!--Add this line-->
</part>
</singlepart>
<singlepart id="295ADA" num="1-p3" quantity="4">
<explicitQuantity>4</explicitQuantity>
<part id="295C18" name="PL 1/4"x1 3/8"" class="Plate" ACADID="295C14" dstvName="PL 1/4"">
<explicitQuantity>1</explicitQuantity>
<role key="Plate" name="PLATE"/>
<length>85.725</length>
<paintArea>7215.343</paintArea>
<ObjectTopLevel>-7.199</ObjectTopLevel>
<ObjectBottomLevel>-93.396</ObjectBottomLevel>
<material key="ASTM-A36" name="A36"/>
<coating key="G" name="G"/>
<weight>143.81</weight>
<exactWeight>143.81</exactWeight>
<density>7850.00</density>
<thickness>6.350</thickness>
<width>35.535</width>
<area1>2884.914</area1>
<area2>2884.914</area2>
<contourLength>227.640</contourLength>
<ElementID>52306</ElementID>
<ElementID>52304</ElementID>
<ElementID>52303</ElementID>
<ElementID>52305</ElementID>
<UserAttr10>1-MS15</UserAttr10> <!--Add this line-->
</part>
</singlepart>
</proxy>
</mainpart>
As there is only one mainpart node (the root element), simply declare your variable as a global variable (a child of xsl:stylesheet)
<xsl:variable name="mainpartmark" select="/mainpart/#num" />
(You wouldn't need the template matching mainpart in this case).
Then, to use it, just do this...
<UserAttribute10>
<xsl:value-of select="/mainpart/#num" />
</UserAttribute10>
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:variable name="mainpartmark" select="/mainpart/#num" />
<xsl:template match="part">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
<UserAttribute10>
<xsl:value-of select="$mainpartmark" />
</UserAttribute10>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Of course, you could also do it without the variable at all, and just do <xsl:value-of select="/mainpart/#num" /> instead.
And, if there were multiple mainpart nodes (under a single root element), you could also do <xsl:value-of select="ancestor::mainpart/#num" />

XSLT for extracting elements from a non linear XML structure

I have a XML structure where the XML schema is irregular/not formatted. The structure looks like this-
<Host>
<element1>type0</element1>
<element2>Fruits</element2>
....
<elementn>Price0</elementn>
<Menu>
<NodeA>
<element1>type1</element1>
<element2>Fruits</element2>
....
<elementn>Price1</elementn>
<Menu>
<NodeB>
<element1>type2</element1>
<element2>Fruits</element2>
....
<elementn>Price2</elementn>
<Menu>
<NodeC>
<element1>type3</element1>
<element2>Fruits</element2>
....
<elementn>Price3</elementn>
<Menu>
<NodeD>
<Element1>type4</element1>
<Element2>Vegetables</Element2>
....
<Elementn>Price4</elementn>
</NodeD>
</Menu>
</NodeC>
</Menu>
</NodeB>
</Menu>
</NodeA>
<NodeE>
<element1>type5</element1>
<element2>Fruits</element2>
....
<elementn>Price5</elementn>
<Menu>
<NodeF>
<element1>type6</element1>
<element2>Vegetables</element2>
....
<elementn>Price6</elementn>
</NodeF>
</Menu>
</NodeE>
</Menu>
</Host>
Now my expected XML is as follows-
a) if <element2> == fruits in all the nodes, I need XML schema as follows. I may include or exclude the below n elements right under host -
`<element1>type0</element>
<element2>Fruits</element2>
....
<elementn>Price0</elementn>`
.Expected Result -
<Host>
<NodeA>
<element1>type1</element1>
<element2>Fruits</element2>
....
<elementn>Price1</elementn>
</NodeA>
<NodeB>
<element1>type2</element1>
<element2>Fruits</element2>
....
<elementn>Price2</elementn>
</NodeB>
<NodeC>
<element1>type3</element1>
<element2>Fruits</element2>
....
<elementn>Price3</elementn>
</NodeC>
<NodeE>
<element1>type5</element1>
<element2>Fruits</element2>
....
<elementn>Price5</elementn>
</NodeE>
</Host>
b) if <element2> == vegetables in all the nodes, I need XML schema as follows
Note: <element2> == Vegetables is always at the last node in the schema
<Host>
<NodeD>
<element1>type4</element1>
<element2>Vegetables</element2>
....
<elementn>Price4</elementn>
</NodeD>
<NodeF>
<element1>type6</element1>
<element2>Vegetables</element2>
....
<elementn>Price6</elementn>
</NodeF>
</Host>
Any help for getting the above XML formats through XSLT would be a great help.
If you want 2 separate document, you don't actually need 2 XSLTs. You can use one XSLT but with a parameter
<xsl:param name="element2" select="'Fruits'" />
(Here 'Fruits' is just the default value, should the parameter not be specified by the calling application).
You would start off by selecting the nodes which have element2 equal to the parameter (Do note XML and XSLT is case-sensitive, so element2 is not the same as Element2 in your XML, but I assumed that was a typo in your XML).
<xsl:apply-templates select="//*[element2=$element2]"/>
You would also need a template to ensure when a node is matched, it does not copy the child nodes...
<xsl:template match="*[element2]">
<xsl:copy>
<xsl:apply-templates select="*[not(*)]" />
</xsl:copy>
</xsl:template>
The other nodes would be handled by the identity template.
Try this XSLT...
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:param name="element2" select="'Fruits'" />
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="//*[element2=$element2]" mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[element2]" mode="copy">
<xsl:copy>
<xsl:apply-templates select="*[not(*)]" mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="#*|node()" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note that if you were using XSLT 2.0, you could create multiple documents in one call, using xsl:for-each-group to get the distinct groups, and xsl:result-document to create a file for each.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/*">
<xsl:for-each-group select="//*[element2]" group-by="element2">
<xsl:result-document href="{current-grouping-key()}.xml" method="xml">
<Host>
<xsl:apply-templates select="current-group()" mode="copy" />
</Host>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="*[element2]" mode="copy">
<xsl:copy>
<xsl:apply-templates select="*[not(*)]" mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="#*|node()" mode="copy" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

xslt code requirement [duplicate]

Below is input XML code and I want to replace wherever this __ character sequence is mentioned it to be replaced by a colon : character.
For example, if it is sbdh__sender, it should be replace by sbdh:sender.
<?xml version="1.0" encoding="UTF-8"?>
<ns1:EPCISDocument xmlns:ns1="http://apse.com" schemaVersion="" creationDate="">
<EPCISHeader>
<sbdh__StandardBusinessDocumentHeader>
<sbdh__HeaderVersion/>
<sbdh__Sender>
<sbdh__Identifier Authority=""/>
<sbdh__ContactInformation>
<sbdh__Contact/>
<sbdh__EmailAddress/>
<sbdh__FaxNumber/>
<sbdh__TelephoneNumber/>
<sbdh__ContactTypeIdentifier/>
</sbdh__ContactInformation>
</sbdh__Sender>
<sbdh__Receiver>
<sbdh__Identifier Authority=""/>
<sbdh__ContactInformation>
<sbdh__Contact/>
<sbdh__EmailAddress/>
<sbdh__FaxNumber/>
<sbdh__TelephoneNumber/>
<sbdh__ContactTypeIdentifier/>
</sbdh__ContactInformation>
</sbdh__Receiver>
<sbdh__Manifest>
<sbdh__NumberOfItems/>
<sbdh__ManifestItem>
<sbdh__MimeTypeQualifierCode/>
<sbdh__UniformResourceIdentifier/>
<sbdh__Description/>
<sbdh__LanguageCode/>
</sbdh__ManifestItem>
</sbdh__Manifest>
<sbdh__BusinessScope>
<sbdh__Scope>
<sbdh__BusinessService>
<sbdh__BusinessServiceName/>
<sbdh__ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform=""/>
</sbdh__BusinessService>
<sbdh__CorrelationInformation>
<sbdh__RequestingDocumentCreationDateTime/>
<sbdh__RequestingDocumentInstanceIdentifier/>
<sbdh__ExpectedResponseDateTime/>
</sbdh__CorrelationInformation>
</sbdh__Scope>
</sbdh__BusinessScope>
</sbdh__StandardBusinessDocumentHeader>
</EPCISHeader>
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime/>
<recordTime/>
<eventTimeZoneOffset/>
<epcList>
<epc type=""/>
</epcList>
<action/>
<bizStep/>
<disposition/>
<readPoint>
<id/>
</readPoint>
<bizLocation>
<id/>
</bizLocation>
<bizTransactionList>
<bizTransaction type=""/>
</bizTransactionList>
<gsk__GskEpcExtension>
<gsk__manufacturingDate>1234</gsk__manufacturingDate>
</gsk__GskEpcExtension>
</ObjectEvent>
</EventList>
</EPCISBody>
</ns1:EPCISDocument>
Any help on this will be appreciated.
XSLT ...
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://apse.com">
<xsl:output method="xml" indent="yes" />
<!-- replace root element to add new namespaces -->
<xsl:template match="ns1:EPCISDocument">
<ns1:EPCISDocument
xmlns:gsk="GSK Namespace Here"
xmlns:sbdh="SBDH Namespace Here"
>
<xsl:apply-templates select="#* | node()"/>
</ns1:EPCISDocument>
</xsl:template>
<!-- if item name has '__' then split it into QName with prefix:name -->
<xsl:template match="#* | node()[substring-after(local-name(), '__')]">
<xsl:variable name="prefix" select="substring-before(local-name(), '__')" />
<xsl:variable name="name" select="substring-after(local-name(), '__')" />
<xsl:variable name="namespace">
<xsl:choose>
<xsl:when test="$prefix = 'gsk'">GSK Namespace Here</xsl:when>
<xsl:when test="$prefix = 'sbdh'">SBDH Namespace Here</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$prefix}:{$name}" namespace="{$namespace}">
<xsl:apply-templates select="#* | node()"/>
</xsl:element>
</xsl:template>
<!-- otherwise just copy the item -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
... Output ...
<?xml version="1.0" encoding="utf-8"?>
<ns1:EPCISDocument schemaVersion="" creationDate="" xmlns:ns1="http://apse.com" xmlns:gsk="GSK Namespace Here" xmlns:sbdh="SBDH Namespace Here">
<EPCISHeader>
<sbdh:StandardBusinessDocumentHeader>
<sbdh:HeaderVersion />
<sbdh:Sender>
<sbdh:Identifier Authority="" />
<sbdh:ContactInformation>
<sbdh:Contact />
<sbdh:EmailAddress />
<sbdh:FaxNumber />
<sbdh:TelephoneNumber />
<sbdh:ContactTypeIdentifier />
</sbdh:ContactInformation>
</sbdh:Sender>
<sbdh:Receiver>
<sbdh:Identifier Authority="" />
<sbdh:ContactInformation>
<sbdh:Contact />
<sbdh:EmailAddress />
<sbdh:FaxNumber />
<sbdh:TelephoneNumber />
<sbdh:ContactTypeIdentifier />
</sbdh:ContactInformation>
</sbdh:Receiver>
<sbdh:Manifest>
<sbdh:NumberOfItems />
<sbdh:ManifestItem>
<sbdh:MimeTypeQualifierCode />
<sbdh:UniformResourceIdentifier />
<sbdh:Description />
<sbdh:LanguageCode />
</sbdh:ManifestItem>
</sbdh:Manifest>
<sbdh:BusinessScope>
<sbdh:Scope>
<sbdh:BusinessService>
<sbdh:BusinessServiceName />
<sbdh:ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform="" />
</sbdh:BusinessService>
<sbdh:CorrelationInformation>
<sbdh:RequestingDocumentCreationDateTime />
<sbdh:RequestingDocumentInstanceIdentifier />
<sbdh:ExpectedResponseDateTime />
</sbdh:CorrelationInformation>
</sbdh:Scope>
</sbdh:BusinessScope>
</sbdh:StandardBusinessDocumentHeader>
</EPCISHeader>
<EPCISBody>
<EventList>
<ObjectEvent>
<eventTime />
<recordTime />
<eventTimeZoneOffset />
<epcList>
<epc type="" />
</epcList>
<action />
<bizStep />
<disposition />
<readPoint>
<id />
</readPoint>
<bizLocation>
<id />
</bizLocation>
<bizTransactionList>
<bizTransaction type="" />
</bizTransactionList>
<gsk:GskEpcExtension>
<gsk:manufacturingDate>1234</gsk:manufacturingDate>
</gsk:GskEpcExtension>
</ObjectEvent>
</EventList>
</EPCISBody>
</ns1:EPCISDocument>

Remove parent based on child value?

I want to display only those orders which dont have OrderLineSource = YTR. All other should be displayed....
My Sample XML :
<Orders>
<Order>
<OrderID>34209649</OrderID>
<OrderStatus>checkout_complete</OrderStatus>
<Amount>32.93</Amount>
<OrderCreation>2014-02-08T00:00:03.00</OrderCreation>
<OrderCompletion>2014-02-08T00:00:03.00</OrderCompletion>
<CustomerGUID>303965683</CustomerGUID>
<CSMPurchaserGUID>0</CSMPurchaserGUID>
<Brand>TRFE</Brand>
<SourceECommerceSystem>Framework</SourceECommerceSystem>
<Currency>GBP</Currency>
<OrderChannel>Online</OrderChannel>
<TransactionSummary>
<TransactionID>2407065</TransactionID>
<MerchantReference>TEdV-5648-34209649</MerchantReference>
<CardCategory>Personal</CardCategory>
<CardScheme>VISA Debit</CardScheme>
<CardCountry>gbr</CardCountry>
<CardIssuer>sdfsdf sdf Bank asdf</CardIssuer>
<CardStartDate>0/0</CardStartDate>
<CardExpiryDate>2016/08</CardExpiryDate>
<Amount>32.93</Amount>
<Currency>GBP</Currency>
<CardPAN>************4585</CardPAN>
<Created>2014-02-07T23:56:48</Created>
<Updated>2014-02-08T00:00:03</Updated>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
<HostedPageIdentifier>dsfasdf-ee85-4afa-bb6a-0afc6dc99896</HostedPageIdentifier>
<HostedPageURL>https://hps.datacash.com/hps/</HostedPageURL>
<PaymentStatus>Paid</PaymentStatus>
<PaymentType>Debit Card</PaymentType>
<NameOnCard>Miss L J adsf</NameOnCard>
<DataCashRef>56456456454</DataCashRef>
<MerchantID>545646</MerchantID>
<ThreeDCard>1</ThreeDCard>
<ThreeDRequested>1</ThreeDRequested>
<IPAddress>127.89.560.1</IPAddress>
</TransactionSummary>
<OrderLine>
<OrderLineID>84598837</OrderLineID>
<OrderID>34209649</OrderID>
<OrderLineLabel>GAREGSBV</OrderLineLabel>
<OrderLineSource>GHR</OrderLineSource>
<Quantity>1</Quantity>
<UnitPrice>32.93</UnitPrice>
<Total>32.93</Total>
<SKUCode>P0032</SKUCode>
<Title>Miss.</Title>
<FirstName>ertwer</FirstName>
<FamilyName>sdaf</FamilyName>
<DateOfBirth>1984-05-30</DateOfBirth>
<Email>sdfasdfa#hotmail.com</Email>
<Mobile>645646454</Mobile>
<PostChannel>0</PostChannel>
<TelephoneChannel>0</TelephoneChannel>
<EmailChannel>0</EmailChannel>
<TextAndOtherChannel>0</TextAndOtherChannel>
<BuildingNumber>27</BuildingNumber>
<AddressLine1>27</AddressLine1>
<AddressLine2>dsfasdf Road</AddressLine2>
<Town>London</Town>
<Country>sdfasdf er</Country>
<Postcode>KL7 2NS</Postcode>
<AddressValidated>1</AddressValidated>
<HKPolicy>
<PolicyNum>PP01754397</PolicyNum>
<ProductDescription>sadfsadfasdfgasdg</ProductDescription>
<CoverTypeDesc>Individual</CoverTypeDesc>
<SingleParentFamilyFlag>0</SingleParentFamilyFlag>
<PolicyTypeRefID>S</PolicyTypeRefID>
<PolicyTypeDesc>Sinasdfnce</PolicyTypeDesc>
<TierDesc>Classic</TierDesc>
<DestinationDesc>Worldwide including USA, Canada, Caribbean</DestinationDesc>
<TotalTravellers>1</TotalTravellers>
<NumOfAdults>1</NumOfAdults>
<NumOfUnder18>0</NumOfUnder18>
<PolicyStartDate>2014-02-08</PolicyStartDate>
<PolicyEndDate>2014-02-12</PolicyEndDate>
<BaseCost>32.93</BaseCost>
<Commission>11.18</Commission>
<UpsoldInd>0</UpsoldInd>
<TierRefID>C</TierRefID>
<DestinationRefID>W2</DestinationRefID>
<CoverTypeRefID>I</CoverTypeRefID>
<AONToPostPolicy>yes</AONToPostPolicy>
<SalesChannel>0011002</SalesChannel>
<WhereYouHeardOfUs>Press advertising</WhereYouHeardOfUs>
<TIPOLTraveller>
<TravellerUUID>1864-1</TravellerUUID>
<PolicyNum>PI0e31754397</PolicyNum>
<Title>Miss</Title>
<FirstName>sdfsf</FirstName>
<FamilyName>sdfsdf</FamilyName>
<DateOfBirth>1984-05-30</DateOfBirth>
<AgeBand>1864</AgeBand>
<DependentFlag>0</DependentFlag>
</TIPOLTraveller>
</TIPOLPolicy>
</OrderLine>
<OrderCustomerDetails>
<Title nil="true" />
<FirstName nil="true" />
<SecondName nil="true" />
<FamilyName nil="true" />
<DateOfBirth nil="true" />
<Email nil="true" />
<Telephone nil="true" />
<Mobile nil="true" />
<Gender nil="true" />
<PostChannel nil="true" />
<TelephoneChannel nil="true" />
<EmailChannel nil="true" />
<TextAndOtherChannel nil="true" />
<BuildingNumber>27</BuildingNumber>
<AddressLine1>27</AddressLine1>
<AddressLine2>asdfa Road</AddressLine2>
<Town>asdfasdf</Town>
<Country>United dsf</Country>
<Postcode>KH9 2NS</Postcode>
<AddressValidated>1</AddressValidated>
</OrderCustomerDetails>
</Order>
<Order>
<OrderID>34209674</OrderID>
<OrderStatus>checkout_complete</OrderStatus>
<Amount>11.13</Amount>
<OrderCreation>2014-02-08T00:08:40.00</OrderCreation>
<OrderCompletion>2014-02-08T00:08:40.00</OrderCompletion>
<CustomerGUID>303965688</CustomerGUID>
<CSMPurchaserGUID>0</CSMPurchaserGUID>
<Brand>TRFDS</Brand>
<SourceECommerceSystem>Framework</SourceECommerceSystem>
<Currency>GBP</Currency>
<OrderChannel>Online</OrderChannel>
<TransactionSummary>
<TransactionID>8115032</TransactionID>
<MerchantReference>JHF-0800-34209674</MerchantReference>
<CardCategory>Personal</CardCategory>
<CardScheme>VISA Debit</CardScheme>
<CardCountry>gbr</CardCountry>
<CardIssuer>Unknown</CardIssuer>
<CardStartDate>0/0</CardStartDate>
<CardExpiryDate>2016/09</CardExpiryDate>
<Amount>11.13</Amount>
<Currency>GBP</Currency>
<CardPAN>************4849</CardPAN>
<Created>2014-02-08T00:08:00</Created>
<Updated>2014-02-08T00:08:40</Updated>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
<HostedPageIdentifier>f3306487-d6ea-4200-9eea-99b1d6832a2e</HostedPageIdentifier>
<HostedPageURL>https://hps.dat.com/hps/</HostedPageURL>
<PaymentStatus>Paid</PaymentStatus>
<PaymentType>Debit Card</PaymentType>
<NameOnCard>Miss Jor </NameOnCard>
<DataCashRef>380010093738013</DataCashRef>
<MerchantID>21877049</MerchantID>
<ThreeDCard>1</ThreeDCard>
<ThreeDRequested>1</ThreeDRequested>
<IPAddress>86..25640.99</IPAddress>
</TransactionSummary>
<OrderLine>
<OrderLineID>84598874</OrderLineID>
<OrderID>34209674</OrderID>
<OrderLineLabel>3-1008617753325</OrderLineLabel>
<OrderLineSource>YTR</OrderLineSource>
<Quantity>1</Quantity>
<UnitPrice>11.13</UnitPrice>
<Total>11.13</Total>
<Title>Miss.</Title>
<FirstName>Jordan</FirstName>
<SecondName>oirut</SecondName>
<FamilyName>dfgsdfgs</FamilyName>
<Email>dfgsdfg#hotmail.com</Email>
<Mobile>654756464</Mobile>
<PostChannel>0</PostChannel>
<TelephoneChannel>0</TelephoneChannel>
<EmailChannel>0</EmailChannel>
<TextAndOtherChannel>0</TextAndOtherChannel>
<BuildingNumber>12</BuildingNumber>
<AddressLine1>12</AddressLine1>
<AddressLine2>sfgsdfg End Gardens</AddressLine2>
<Town>HEMEL sfgaefa</Town>
<Country>adf dgfsdfg</Country>
<Postcode>HP1 1SN</Postcode>
<OrderLineDetail>
<NameValuePair>
<Name>dfgsdfg</Name>
<Value>628</Value>
</NameValuePair>
<NameValuePair>
<Name>NameOnCard</Name>
<Value>adsfgasdgf Piper</Value>
</NameValuePair>
<NameValuePair>
<Name>DateOnCard</Name>
<Value>2014-02-05</Value>
</NameValuePair>
<NameValuePair>
<Name>CustomsOrSurcharge</Name>
<Value>CUSTOMS CHARGE TO PAY</Value>
</NameValuePair>
</OrderLineDetail>
</OrderLine>
<OrderCustomerDetails>
<Title>Miss.</Title>
<FirstName>Jordan</FirstName>
<SecondName>asdgfasdgf</SecondName>
<FamilyName nil="true" />
<DateOfBirth />
<Email>adfadf#hotmail.com</Email>
<Telephone />
<Mobile>adfasdf</Mobile>
<Gender nil="true" />
<PostChannel nil="true" />
<TelephoneChannel nil="true" />
<EmailChannel nil="true" />
<TextAndOtherChannel nil="true" />
<BuildingNumber>12</BuildingNumber>
<AddressLine1>12</AddressLine1>
<AddressLine2>adfasdf End Gardens</AddressLine2>
<Town>adsfasdf HEMPSTEAD</Town>
<Country>United asdfasdf</Country>
<Postcode>asd 1SN</Postcode>
</OrderCustomerDetails>
</Order>
</Orders>
I tried using XSLT :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Orders -->
<xsl:template match="/*">
<xsl:element name="Orders">
<xsl:apply-templates select="./Order" />
</xsl:element>
</xsl:template>
<!-- Orders > Order -->
<xsl:template match="/Order">
<xsl:variable name="IsValid">
<xsl:call-template name="HasOrIsValidPOLine" />
</xsl:variable>
<xsl:if test="$IsValid='VALID'"> <!-- only display the order if there's a valid line under it-->
<xsl:element name="Order">
<xsl:apply-templates select=".//VORNR" />
</xsl:element>
</xsl:if>
</xsl:template>
<!-- Part Order List > Part Order > Operational BO Number -->
<xsl:template match="//VORNR">
<xsl:element name="./Order">
<xsl:apply-templates select="node()|#*"/>
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>
<xsl:template name="HasOrIsValidPOLine">
<xsl:choose>
<xsl:when test="./OrderLineSource/text() != 'YTR'">VALID</xsl:when>
<xsl:otherwise>INVALID</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Can you provide me the solution or let me know where I am going wrong
First, your sample XML is not well-formed: It contains a closing </TIPOLPolicy> tag that doesn't match the starting <HKPolicy> tag. Change that to </HKPolicy> first.
After that, the following XSLT 1.0 does what you want:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity transform -->
<!-- Default priority 0 for root node and -0.5 for the rest -->
<xsl:template match="/ | node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<!-- Do nothing for Order elements whose OrderLine/OrderLineSource equals 'YTR' -->
<!-- Default priority 0.5 -->
<xsl:template match="Order[OrderLine/OrderLineSource = 'YTR']"/>
</xsl:stylesheet>
It makes use of the identity transform and different default priorities: The identity transform with a lower default priority copies the input to the output unless another template with a higher priority exists for a given input match. This is the case for Order elements whose OrderLine/OrderLineSource descendant contains the text value 'YTR'. Due to its higher default priority, the more specific template takes precedence over the identity transform. Since the template doesn't produce any output, any matching Order elements are removed from the output.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Order[OrderLine/OrderLineSource[string() = 'YTR']]"/>
</xsl:stylesheet>

XSLT recursive sum troubles

When I try to recursive sum an attributes from multiple nodes, it's gluing like string :(
XML-file (second mileage-node include first mileage-node)
<mileage value="15000">
<operation title="Replacing the engine oil" cost="500" />
<sparepart title="Oil filter" cost="250" />
<sparepart title="Motor oil" cost="1050" />
</mileage>
<mileage value="30000">
<repeating mileage="15000" />
<operation title="Replacement of spark" cost="1200" />
</mileage>
XSL-template
<xsl:template match="mileage[#value]">
<xsl:param name="sum" select="number(0)" />
<xsl:variable name="milinkage"><xsl:value-of select="number(repeating/#mileage)" /></xsl:variable>
<xsl:apply-templates select="parent::*/mileage[#value=$milinkage]"><xsl:with-param name="sum" select="number($sum)" /></xsl:apply-templates>
<xsl:value-of select="number(sum(.//#cost))"/> <!-- + number($sum) -->
</xsl:template>
Glued result is 18001200, but I want see 3000 (1800 + 1200)
Please tell me what is wrong here?
Thanx!
Remove the dot and you will always see 3000 because all #costs (independent from starting point) will be summed.
<xsl:value-of select="number(sum(//#cost))"/> <!-- + number($sum) -->
Output will look like this: 30003000
But I assume that something is wrong with your approach. When you call a template recursive then the output will also will be printed as much as the template calls itself in your case. You need to print out the result at the end of your recursion
Given this input:
<root>
<mileage value="15000">
<operation title="Replacing the engine oil" cost="500" />
<sparepart title="Oil filter" cost="250" />
<sparepart title="Motor oil" cost="1050" />
</mileage>
<mileage value="30000">
<repeating mileage="15000" />
<operation title="Replacement of spark" cost="1200" />
</mileage>
</root>
and using this xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="root"/>
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates select="mileage[#value=30000]"/>
</xsl:template>
<xsl:template match="mileage[#value]">
<xsl:param name="sum" select="number(0)" />
<xsl:variable name="milinkage"><xsl:value-of select="number(repeating/#mileage)" /></xsl:variable>
<xsl:variable name="newsum">
<xsl:value-of select="number(sum(.//#cost)) + $sum"/>
</xsl:variable>
<xsl:apply-templates select="parent::*/mileage[#value=$milinkage]"><xsl:with-param name="sum" select="number($newsum)" /></xsl:apply-templates>
<xsl:if test="not(parent::*/mileage[#value=$milinkage])">
<xsl:value-of select="$newsum"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
gives the correct result: 3000
You need xmlns:exsl="http://exslt.org/common"
<xsl:template match="/">
<xsl:variable name="nodes">
<xsl:apply-templates select="root/mileage[position()=last()]"/>
</xsl:variable>
<xsl:copy-of select="sum(exsl:node-set($nodes)/*[#cost]/#cost)"/>
</xsl:template>
<xsl:template match="mileage">
<xsl:copy-of select="*[#cost]"/>
<xsl:apply-templates select="../mileage[#value=current()/repeating/#mileage]"/>
</xsl:template>`