How to Skip segment based on a field value in XSLT - xslt

I have an XML input which i have to map into a target structure
Here is the link to Fiddler: LINK
<?xml version='1.0' encoding='UTF-8'?>
<root>
<d>
<Campaign>
<CampaignId>0000000112</CampaignId>
<Name>Connected Water DMI Fail Test</Name>
<MarketingAreaId>CXXGLOBAL</MarketingAreaId>
<SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
<ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
</Campaign>
<PackageId>47</PackageId>
<ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
<ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
<CampaignTargetGroupMembers>
<OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
<PackageId>47</PackageId>
<ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR</AttributeId>
<Value></Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
</TargetGroupMemberAttributeData>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST</AttributeId>
<Value>Bailey</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
</TargetGroupMemberAttributeData>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST</AttributeId>
<Value>Chandler</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>A071C1B1F437B581DC597E5116B38BED039CA864</OutboundId>
</TargetGroupMemberAttributeData>
</CampaignTargetGroupMembers>
<CampaignTargetGroupMembers>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
<PackageId>47</PackageId>
<ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR</AttributeId>
<Value>rcgrymm#gmail.com</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
</TargetGroupMemberAttributeData>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST</AttributeId>
<Value>Carmona</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
</TargetGroupMemberAttributeData>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST</AttributeId>
<Value>Ruben</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
</TargetGroupMemberAttributeData>
</CampaignTargetGroupMembers>
</d>
</root>
I have to write XSLT code so that node and subnodes only get copied and formatted with new field names when AttributeId = DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR and corresponding Value != ''
How do i modify XSLT Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="campaign" select="root/d/Campaign/CampaignId/text()" />
<xsl:template match="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST']">
<FIRST_NAME><xsl:value-of select="Value"/></FIRST_NAME>
</xsl:template>
<xsl:template match="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST']">
<LAST_NAME><xsl:value-of select="Value" /></LAST_NAME>
<xsl:template match="CampaignTargetGroupMembers/ExecutionRunKey">
<CAMPAIGN_ID><xsl:copy-of select="$campaign"/></CAMPAIGN_ID>
</xsl:template>
<xsl:template match="TargetGroupMemberAttributeData[AttributeId='OUTBOUND_INTERACTION']">
<UUID><xsl:value-of select="Value" /></UUID>
</xsl:template>
</xsl:stylesheet>
The output Should be:
<?xml version="1.0" encoding="UTF-8"?><root>
<d>
<Campaign>
<CampaignId>0000000112</CampaignId>
<Name>Connected Water DMI Fail Test</Name>
<MarketingAreaId>CXXGLOBAL</MarketingAreaId>
<SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
<ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
</Campaign>
<PackageId>47</PackageId>
<ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
<ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
<CampaignTargetGroupMembers>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
<PackageId>47</PackageId>
<CAMPAIGN_ID>0000000112</CAMPAIGN_ID>
<Email>rcgrymm#gmail.com</Email>
<LAST_NAME>Carmona</LAST_NAME>
<FIRST_NAME>Ruben</FIRST_NAME>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
<Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
</TargetGroupMemberAttributeData>
</CampaignTargetGroupMembers>
</d>
</root>

To get the desired output, you can use the following stylesheet. But not all values could be derived from your source, so I put in some static values. I guess that you could fill in the rest.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="campaign" select="root/d/Campaign/CampaignId/text()" />
<xsl:mode on-no-match="shallow-copy" />
<xsl:strip-space elements="*" />
<xsl:template match="CampaignTargetGroupMembers | TargetGroupMemberAttributeData" />
<xsl:template match="CampaignTargetGroupMembers[TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR' and Value!='']]">
<xsl:copy>
<xsl:apply-templates select="#* | OutboundId | PackageId"/>
<CAMPAIGN_ID><xsl:value-of select="$campaign" /></CAMPAIGN_ID>
<Email><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-SMTP_ADDR']/Value" /></Email>
<LAST_NAME><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_LAST']/Value" /></LAST_NAME>
<FIRST_NAME><xsl:value-of select="TargetGroupMemberAttributeData[AttributeId='DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-NAME_FIRST']/Value"/></FIRST_NAME>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
<Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
<EdmTypeId><xsl:value-of select="TargetGroupMemberAttributeData[1]/EdmTypeId" /></EdmTypeId>
<OutboundId><xsl:value-of select="TargetGroupMemberAttributeData[1]/OutboundId" /></OutboundId>
</TargetGroupMemberAttributeData>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Its output is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<d>
<Campaign>
<CampaignId>0000000112</CampaignId>
<Name>Connected Water DMI Fail Test</Name>
<MarketingAreaId>CXXGLOBAL</MarketingAreaId>
<SegmentationObject>SAP_CONTACT_ENGAGEMENT_SIN</SegmentationObject>
<ImplementationId>ZOC_EXPORT_ACOUSTIC</ImplementationId>
</Campaign>
<PackageId>47</PackageId>
<ExecutionStartDateTime>2021-06-30T15:28:49Z</ExecutionStartDateTime>
<ExecutionRunKey>42010A0350331EDBB6B6EDEF969FD623</ExecutionRunKey>
<CampaignTargetGroupMembers>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
<PackageId>47</PackageId>
<CAMPAIGN_ID>0000000112</CAMPAIGN_ID>
<Email>rcgrymm#gmail.com</Email>
<LAST_NAME>Carmona</LAST_NAME>
<FIRST_NAME>Ruben</FIRST_NAME>
<TargetGroupMemberAttributeData>
<AttributeId>DA-_SAP_CF_CE_CONTACT_IA_ERP_CUST-INITIATIVE_ID</AttributeId>
<Value>0000000105,0000000092,0000000041,,0000000054,0000000076</Value>
<EdmTypeId>Edm.String</EdmTypeId>
<OutboundId>E6713D258EAC1ADACE6AC905BB3ECF5E1CEF4776</OutboundId>
</TargetGroupMemberAttributeData>
</CampaignTargetGroupMembers>
</d>
</root>
Which is as desired (with some static values which cannot be deduced from the input XML).

Related

Sorting on attribute collected from reference list

I am trying to sort a list of categorized xml elements, using XSLT 2.0. Each element has a unique ID and the categorization is defined in another list containing these and more elements. Here's an example of a starting XML document. The section that I want sorted is /Atlas/VisitedCities. It should be sorted according to area of the world and date of the visit:
<?xml version="1.0" encoding="UTF-8"?>
<Atlas>
<Cities>
<City id="1" worldPart="Africa">
<Name>Luxor</Name>
<Founded>-3200</Founded>
<Location>Egypt</Location>
</City>
<City id="2" worldPart="Africa">
<Name>Tripoli</Name>
<Founded>-700</Founded>
<Location>Libya</Location>
</City>
<City id="3" worldPart="Americas">
<Name>Cholula</Name>
<Founded>-200</Founded>
<Location>Mexico</Location>
</City>
<City id="4" worldPart="Americas">
<Name>Flores</Name>
<Founded>-1000</Founded>
<Location>Guatemala</Location>
</City>
<City id="5" worldPart="Europe">
<Name>Argos</Name>
<Founded>-5000</Founded>
<Location>Greece</Location>
</City>
<City id="6" worldPart="Europe">
<Name>Athens</Name>
<Founded>-4000</Founded>
<Location>Greece</Location>
</City>
</Cities>
<VisitedCities lastUpdate="2018-09-10">
<VisitedCity cityID="6">
<Date>1883-08-26</Date>
<Visitor>Dora</Visitor>
</VisitedCity>
<VisitedCity cityID="3">
<Date>1907-01-02</Date>
<Visitor>Nemo</Visitor>
</VisitedCity>
<VisitedCity cityID="4">
<Date>1940-02-08</Date>
<Visitor>Jimenez</Visitor>
</VisitedCity>
<VisitedCity cityID="2">
<Date>1886-06-10</Date>
<Visitor>James T. Kirk</Visitor>
</VisitedCity>
</VisitedCities>
</Atlas>
The wanted output is this:
<?xml version="1.0" encoding="UTF-8"?>
<Atlas>
<Cities>
<City id="1" worldPart="Africa">
<Name>Luxor</Name>
<Founded>-3200</Founded>
<Location>Egypt</Location>
</City>
<City id="2" worldPart="Africa">
<Name>Tripoli</Name>
<Founded>-700</Founded>
<Location>Libya</Location>
</City>
<City id="3" worldPart="Americas">
<Name>Cholula</Name>
<Founded>-200</Founded>
<Location>Mexico</Location>
</City>
<City id="4" worldPart="Americas">
<Name>Flores</Name>
<Founded>-1000</Founded>
<Location>Guatemala</Location>
</City>
<City id="5" worldPart="Europe">
<Name>Argos</Name>
<Founded>-5000</Founded>
<Location>Greece</Location>
</City>
<City id="6" worldPart="Europe">
<Name>Athens</Name>
<Founded>-4000</Founded>
<Location>Greece</Location>
</City>
</Cities>
<VisitedCities lastUpdate="2018-09-10">
<VisitedCity cityID="2">
<Date>1886-06-10</Date>
<Visitor>James T. Kirk</Visitor>
</VisitedCity>
<VisitedCity cityID="6">
<Date>1883-08-26</Date>
<Visitor>Dora</Visitor>
</VisitedCity>
<VisitedCity cityID="3">
<Date>1907-01-02</Date>
<Visitor>Nemo</Visitor>
</VisitedCity>
<VisitedCity cityID="4">
<Date>1940-02-08</Date>
<Visitor>Jimenez</Visitor>
</VisitedCity>
</VisitedCities>
</Atlas>
The stylesheet (XSLT 2.0) that I am struggling with looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<!-- Format output -->
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<!-- Copy everything that does not match later templates. -->
<xsl:template match="node()|#*" priority="-1">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="city.list" select="/Atlas/Cities"/>
<xsl:variable name="sort-order" as="element()*">
<wPart>Africa</wPart>
<wPart>Europe</wPart>
<wPart>Americas</wPart>
</xsl:variable>
<xsl:template match="/Atlas/VisitedCities">
<xsl:variable name="city-list" select="."/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each select="$sort-order">
<xsl:variable name="this-wpart" select="./text()"/>
<!-- How to select VisitedCity based on info in other list??? -->
<xsl:apply-templates select="$city-list/VisitedCity[$city.list/City[#cityID=$city-list/VisitedCity/#cityID]/#worldPart=$this-wpart]">
<xsl:sort select="./Date"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I think I understand why this stylesheet will not work (it does not sort at all), as I don't know how to make the selection in the (last) apply-templates. I don't see how to refer to the outermost elements from the inner parts of this expression.
It might suffice to set up a key to reference the City elements by the id attribute to then, in the xsl:sort select expression reference the worldPart attribute. Additionally you could replace the for-each on your continent order with an index-of() call with
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:key name="city-by-id" match="Cities/City" use="#id"/>
<xsl:variable name="sort-order" as="element()*">
<wPart>Africa</wPart>
<wPart>Europe</wPart>
<wPart>Americas</wPart>
</xsl:variable>
<xsl:template match="VisitedCities">
<xsl:copy>
<xsl:apply-templates select="VisitedCity">
<xsl:sort select="index-of($sort-order, key('city-by-id', #cityID)/#worldPart)"/>
<xsl:sort select="Date"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/eiZQaFJ
That complete example is XSLT 3 but to use it with XSLT 2 you would just replace the xsl:mode declaration in there with your template you have prefixed with the comment <!-- Copy everything that does not match later templates. -->, that is, with the identity transformation template.

Attempting to remove element and reorder elements in resulting document

I have the following XML and XSLT.
I am attempting to exclude the NotUsed2 element from the resulting document.
I need to reorder the resulting so that AutoTypesetInfo follows NotUsed3.
XML:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<Plan xmlns="http://www.ccieurope.com/xmlns/CCIPlanner">
<Header>
<Update>0</Update>
<NewsPaper>Pub1</NewsPaper>
<Date>080818</Date>
<Zone>MAIN</Zone>
<Edition>1st</Edition>
<Pages>26</Pages>
<Section>A B C </Section>
<Number>08082018</Number>
<Format>
<Height>21.70i</Height>
<Width>9.87i</Width>
</Format>
<PrintingInfo/>
<Version/>
<Config/>
<Default/>
<FileFormat>2.0</FileFormat>
<IssueName/>
</Header>
<Page>
<PhysicalBook>A</PhysicalBook>
<BookPageNumber>1</BookPageNumber>
<Desk>A</Desk>
<SubDesk/>
<SpreadPage>0</SpreadPage>
<Status/>
<Deadline>0708182230</Deadline>
<PagePrintInfo/>
<Cmyk>CMYK</Cmyk>
<Spot1/>
<Spot2/>
<Spot3/>
<CyanFilm/>
<MagFilm/>
<YelFilm/>
<BlackFilm/>
<Spot1Film/>
<Spot2Film/>
<Spot3Film/>
<PageId>MAIN#F</PageId>
<TemplatePage>
<Content>c#FolioStandard#i#</Content>
<Layout></Layout>
</TemplatePage>
<ProdForm/>
<TextSourceDir/>
<TextSourcePage/>
<RunningPage/>
<CmyDeadline/>
<ColorGrp/>
<AdRules>1</AdRules>
<CustChar/>
<CustDate/>
<Active>1</Active>
<NoMaster>0</NoMaster>
<Cust2/>
<PageType>EditorialOnly</PageType>
<Cust4/>
<Cust5/>
<Cust6/>
<Cust7/>
<Cust8/>
<Cust9/>
<Cust10/>
<Desk2/>
<Desk2NewsHole/>
<Desk2Placement/>
<Cust11/>
<NotUsed2/>
<NotUsed3/>
<Ad>
<BookingNumber>12345</BookingNumber>
<Description>Desc1</Description>
<AdRelatedInfo> </AdRelatedInfo>
<FirstPubDate>080818</FirstPubDate>
<LastPubDate>080818</LastPubDate>
<Customer>Customer</Customer>
<AdColor>Proces</AdColor>
<AdWidth>710.9</AdWidth>
<AdHeight>144.0</AdHeight>
<MatType>EPS</MatType>
<AdType>0</AdType>
<AdPos>
<XPos>0.00</XPos>
<YPos>1404.03</YPos>
</AdPos>
<AdProdInfo/>
<AdNotUsed2/>
<AdNotUsed3/>
<AdCust1/>
<AdCust2/>
<AdCust3/>
</Ad>
<AutoTypesetInfo>0:5-60</AutoTypesetInfo></Page>
<Page>
<PhysicalBook>A</PhysicalBook>
<BookPageNumber>2</BookPageNumber>
<Desk>A</Desk>
<SubDesk/>
<SpreadPage>0</SpreadPage>
<Status/>
<Deadline>0708182230</Deadline>
<PagePrintInfo/>
<Cmyk>CMYK</Cmyk>
<Spot1/>
<Spot2/>
<Spot3/>
<CyanFilm/>
<MagFilm/>
<YelFilm/>
<BlackFilm/>
<Spot1Film/>
<Spot2Film/>
<Spot3Film/>
<PageId>MAIN#1</PageId>
<TemplatePage>
<Content>c#FolioStandard#i#</Content>
<Layout></Layout>
</TemplatePage>
<ProdForm/>
<TextSourceDir/>
<TextSourcePage/>
<RunningPage/>
<CmyDeadline/>
<ColorGrp/>
<AdRules>1</AdRules>
<CustChar/>
<CustDate/>
<Active>1</Active>
<NoMaster>0</NoMaster>
<Cust2/>
<PageType>Mixed</PageType>
<Cust4/>
<Cust5/>
<Cust6/>
<Cust7/>
<Cust8/>
<Cust9/>
<Cust10/>
<Desk2/>
<Desk2NewsHole/>
<Desk2Placement/>
<Cust11/>
<NotUsed2/>
<NotUsed3/>
<Ad>
<BookingNumber>152345</BookingNumber>
<Description>Description</Description>
<AdRelatedInfo> </AdRelatedInfo>
<FirstPubDate>080818</FirstPubDate>
<LastPubDate>080818</LastPubDate>
<Customer>Customer</Customer>
<AdColor>000K</AdColor>
<AdWidth>351.9</AdWidth>
<AdHeight>360.0</AdHeight>
<MatType>EPS</MatType>
<AdType>0</AdType>
<AdPos>
<XPos>0.00</XPos>
<YPos>1188.03</YPos>
</AdPos>
<AdProdInfo/>
<AdNotUsed2/>
<AdNotUsed3/>
<AdCust1/>
<AdCust2/>
<AdCust3/>
</Ad>
<Ad>
<BookingNumber>12345</BookingNumber>
<Description>Description</Description>
<AdRelatedInfo> </AdRelatedInfo>
<FirstPubDate>080818</FirstPubDate>
<LastPubDate>080818</LastPubDate>
<Customer>Customer</Customer>
<AdColor>Proces</AdColor>
<AdWidth>351.9</AdWidth>
<AdHeight>360.0</AdHeight>
<MatType>EPS</MatType>
<AdType>0</AdType>
<AdPos>
<XPos>358.99</XPos>
<YPos>1188.03</YPos>
</AdPos>
<AdProdInfo/>
<AdNotUsed2/>
<AdNotUsed3/>
<AdCust1/>
<AdCust2/>
<AdCust3/>
</Ad>
<AutoTypesetInfo>0:5-60</AutoTypesetInfo>
</Page>
</Plan>
XSL:
<?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:cci="http://www.ccieurope.com/xmlns/CCIPlanner"
exclude-result-prefixes="msxsl cci"
>
<xsl:output indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!-- If element name match on NotUsed2, exclude from result -->
<xsl:template match="NotUsed2" />
</xsl:stylesheet>
Expected in the resulting Page node:
<Page>
<PhysicalBook>A</PhysicalBook>
<BookPageNumber>1</BookPageNumber>
<Desk>A</Desk>
<SubDesk/>
<SpreadPage>0</SpreadPage>
<Status/>
<Deadline>0708182230</Deadline>
<PagePrintInfo/>
<Cmyk>CMYK</Cmyk>
<Spot1/>
<Spot2/>
<Spot3/>
<CyanFilm/>
<MagFilm/>
<YelFilm/>
<BlackFilm/>
<Spot1Film/>
<Spot2Film/>
<Spot3Film/>
<PageId>MAIN#F</PageId>
<TemplatePage>
<Content>c#FolioStandard#i#</Content>
<Layout></Layout>
</TemplatePage>
<ProdForm/>
<TextSourceDir/>
<TextSourcePage/>
<RunningPage/>
<CmyDeadline/>
<ColorGrp/>
<AdRules>1</AdRules>
<CustChar/>
<CustDate/>
<Active>1</Active>
<NoMaster>0</NoMaster>
<Cust2/>
<PageType>EditorialOnly</PageType>
<Cust4/>
<Cust5/>
<Cust6/>
<Cust7/>
<Cust8/>
<Cust9/>
<Cust10/>
<Desk2/>
<Desk2NewsHole/>
<Desk2Placement/>
<Cust11/>
<NotUsed3/>
<AutoTypesetInfo>0:5-60</AutoTypesetInfo>
<Ad>
<BookingNumber>12345</BookingNumber>
<Description>Desc1</Description>
<AdRelatedInfo> </AdRelatedInfo>
<FirstPubDate>080818</FirstPubDate>
<LastPubDate>080818</LastPubDate>
<Customer>Customer</Customer>
<AdColor>Proces</AdColor>
<AdWidth>710.9</AdWidth>
<AdHeight>144.0</AdHeight>
<MatType>EPS</MatType>
<AdType>0</AdType>
<AdPos>
<XPos>0.00</XPos>
<YPos>1404.03</YPos>
</AdPos>
<AdProdInfo/>
<AdNotUsed2/>
<AdNotUsed3/>
<AdCust1/>
<AdCust2/>
<AdCust3/>
</Ad>
</Page>
I can't get the code to exclude the NotUsed2 element.
I'm not sure where to begin to tackle reordering the elements within nodes.
Any help would be appreciated.
Your NotUsed2 is not being removed because you have not accounted for the fact the element is in a default namespace in the XML. Your XSLT is looking for an element with no namespace.
You have declared the relevant namespace in your XSLT, so you just need to use the relevant prefix in the template match
<xsl:template match="cci:NotUsed2" />
For moving AutoTypesetInfo after NotUsed3 you should have a template that matches NotUsed3 where you can copy both the matched node, and the AutoTypesetInfo
<xsl:template match="cci:NotUsed3">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
<xsl:copy-of select="../cci:AutoTypesetInfo" />
</xsl:template>
You would also need a template to ensure the AutoTypesetInfo still doesn't get copied in its current position too
<xsl:template match="cci:AutoTypesetInfo" />
Try this XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:cci="http://www.ccieurope.com/xmlns/CCIPlanner"
exclude-result-prefixes="msxsl cci">
<xsl:output indent="yes"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!-- If element name match on NotUsed2, exclude from result -->
<xsl:template match="cci:NotUsed2" />
<xsl:template match="cci:NotUsed3">
<xsl:call-template name="identity" />
<xsl:copy-of select="../cci:AutoTypesetInfo" />
</xsl:template>
<xsl:template match="cci:AutoTypesetInfo" />
</xsl:stylesheet>
This does assume NotUsed3 is always present. If not, change the last template to this to ensure the original AutoTypesetInfo is not removed in this case.
<xsl:template match="cci:AutoTypesetInfo[../cci:NotUsed3]" />
Try below 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:cci="http://www.ccieurope.com/xmlns/CCIPlanner"
exclude-result-prefixes="msxsl cci"
>
<xsl:output indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<!-- If element name match on NotUsed2, exclude from result -->
<xsl:template match="*[local-name()='NotUsed2']" />
</xsl:stylesheet>

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 : Cummulative Sum (Conditional)

I need to do a conditional sum using XSLT. The sum of 'Oty' for each 'SKU' should be calculated only for providers listed within the 'Provider' node. In the provided example, the Qty for providerCode 4 should be skipped as its not in the 'Providers' list. I'm restricted to using XSLT 1.0.
I would appreciate any help. Thanks!
Here is the sample XML.
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Providers>
<ProviderCode>1</ProviderCode>
<ProviderCode>2</ProviderCode>
<ProviderCode>3</ProviderCode>
</Providers>
<SKU>
<SKU>XYZ</SKU>
<Description>XYZ Description</Description>
<Provider>
<ProviderCode>1</ProviderCode>
<Qty>100</Qty>
</Provider>
<Provider>
<ProviderCode>2</ProviderCode>
<Qty>67</Qty>
</Provider>
<Provider>
<ProviderCode>3</ProviderCode>
<Qty>74</Qty>
</Provider>
<Provider>
<ProviderCode>4</ProviderCode>
<Qty>62</Qty>
</Provider>
</SKU>
<SKU>
<SKU>ABC</SKU>
<Description>ABC Description</Description>
<Provider>
<ProviderCode>1</ProviderCode>
<Qty>20</Qty>
</Provider>
<Provider>
<ProviderCode>2</ProviderCode>
<Qty>77</Qty>
</Provider>
<Provider>
<ProviderCode>3</ProviderCode>
<Qty>42</Qty>
</Provider>
<Provider>
<ProviderCode>4</ProviderCode>
<Qty>631</Qty>
</Provider>
</SKU>
</Root>
Here is the required output.
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<SKU>
<SKU>XYZ</SKU>
<Qty>241</Qty>
</SKU>
<SKU>
<SKU>ABC</SKU>
<Qty>139</Qty>
</SKU>
</Root>
You can simply use sum on the nodes you want, either by comparing sum(Provider[ProviderCode = //Providers/ProviderCode]/Qty) or by using a key:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="prov" match="Providers/ProviderCode" use="."/>
<xsl:template match="Root">
<xsl:copy>
<xsl:apply-templates select="SKU"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Root/SKU">
<xsl:copy>
<xsl:copy-of select="SKU"/>
<Qty><xsl:value-of select="sum(Provider[key('prov', ProviderCode)]/Qty)"/></Qty>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

XSLT issue with renaming the elements -- changing namespace

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.