Jump to content

Using Xslt To Only Copy Specific Nodes.


darrinps
 Share

Recommended Posts

I know how to do an identity transform, and I know how to filter specific nodes out, but how do I specify that you copy only specific nodes and everything else (comments, etc.)?For example:

<mmmm:RetrieveSomething xmlns:clcs="http://www.mycorp.com/mmmm/retrieval/retrievesomthing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mycorp.com/mmmm/retrieval/retrievesomething RetrieveSomething.xsd" CorrelationId="123456789" Version="1.0">	<Return Status="Success"/>	<AccountType>M</AccountType>	<ProgramName>TEST NAME</ProgramName>	<EnrollmentChannel>Web</EnrollmentChannel>	<EnrollmentDetails>http://mycorp</EnrollmentDetails>	<CustomerId>12345678901234</CustomerId>	<MemberRowId>222222222222222</MemberRowId></mmmm:RetrieveSomething>

Suppose all I wanted to see was:

<mmmm:RetrieveSomething xmlns:clcs="http://www.mycorp.com/mmmm/retrieval/retrievesomthing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mycorp.com/mmmm/retrieval/retrievesomething RetrieveSomething.xsd" CorrelationId="123456789" Version="1.0">	<Return Status="Success"/>	<EnrollmentChannel>Web</EnrollmentChannel>	<MemberRowId>222222222222222</MemberRowId></mmmm:RetrieveSomething>

What should the xslt look like to accomplish that?I thought that I could try to first copy over all non-nodes then focus on the nodes and list the specific ones I wanted to copy but I could not get it to work.Again, I don't want to filter out specific nodes, I just want to copy specific ones!Thanks.

Link to comment
Share on other sites

You can have a template that matches the nodes you can to copy, and make it copy the matched node, then applying further matches again.In other words:

<xsl:template match="/"><xsl:apply-templates/></xsl:template><xsl:template match="RetrieveSomething|RetrieveSomething|RetrieveSomething|MemberRowId"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>

(Do adjust your namespaces though... I'm not seeing what does the "mmmm" prefix stand for, so I haven't adjusted it)

Edited by boen_robot
Link to comment
Share on other sites

You can have a template that matches the nodes you can to copy, and make it copy the matched node, then applying further matches again.In other words:
<xsl:template match="/"><xsl:apply-templates/></xsl:template><xsl:template match="RetrieveSomething|RetrieveSomething|RetrieveSomething|MemberRowId"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>

(Do adjust your namespaces though... I'm not seeing what does the "mmmm" prefix stand for, so I haven't adjusted it)

I'll give it a try. Thanks!
Link to comment
Share on other sites

Well I tried that and got something similar to what I have been getting with the various things I was trying before.Here is what I did:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>	<xsl:template match="/">		<xsl:apply-templates/>	</xsl:template>	<xsl:template match="EnrollmentChannel[parent::Customer]|EnrollmentDetails[parent::Customer]">		<xsl:copy>			<xsl:apply-templates/>		</xsl:copy>	</xsl:template></xsl:stylesheet>

Here is the sample XML I'm trying to filter on:

<group:Retrieve xmlns:clcs="http://www.mycorp.com/group/retrieval/retrieve" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mycorp.com/name/retrieval/retrieve Retrieve.xsd" CorrelationId="123456789" Version="1.0">	<Return Status="Success"/>	<Customer>		<AccountType>M</AccountType>		<ProgramName>PROGNAME</ProgramName> 		<EnrollmentChannel>Web</EnrollmentChannel>		<EnrollmentDetails>http://mycorp</EnrollmentDetails>		<CustomerId>12345678901234</CustomerId>		<MemberRowId>222222222222222</MemberRowId>	<Customer></group>

Here is what I get when I apply that:

<?xml version="1.0" encoding="UTF-8"?>MPROGNAME<EnrollmentChannel xmlns:group="http://www.mycorp.com/group/retrieval/retrieve" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Web</EnrollmentChannel><EnrollmentDetails xmlns:group="http://www.mycorp.com/group/retrieval/retrieve" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">http://mycorp</EnrollmentDetails>12345678901234222222222222222888888888888888John SmithJ5KL6767Mr.JohnJamesSmithJrM909666LASYx456710/23/200623541966-11-2006/05/2009NYNNYYY02/23/200501/06/2010666666666666666100 Main Street#14143rd FloorDallasTXQuebec75219USAPersonalFloor3Wells Fargo999999999999999Cell214-456-7654Y777777777777777Personalrojcar@gmail.comMilitary01/23/200001/28/200201/23/2009x7777NavyCollege09/01/198112/28/198504/28/2008x8888TAMUActive01/23/200501/25/200501/26/20051300012000110001000012/03/2002Individual4000204/02/200104/03/2003NNYK101SWAY01/01/20090

It's like it adds the data OK, but all the formatting (white space) is gone. How can I get that back?When I just filtered out specific nodes it worked, but I want to be able to only ask for specific nodes but still keep everything else (including white space). :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...