I have a XSL that needs to filter out specific data found in the XML.
Somewhere in my XML there will be a node like:
<id root="2.16.840.1.113883.3.51.1.1.6.1" extension="9494949494949" />
The XSL I have below deletes the extension node and adds a nullFlavor="MSK" to the node.
What I need to do now, is take the value from the extension node, and search the entire XML document for that value, and replace it with **.
But I'm not sure how to take the extension attribute, and find all instances of that value in the XML (they could be burried in text and inside attributes) and turn them into ** (4 *).
The example below is just an example. I cannot hard code the XSL to look at specific nodes, it needs to look through all text / attribute text in the xml (reason for this is there are 5+ different versions of XML that this will be applied to).
I need to find the Extension in the node, then replace (delete really) that value from the rest of the XML. I'm looking for a 1 solution fits all messages, so a global search->wipe of the Extension value.
Example:
<identifiedPerson classCode="IDENT">
<id root="2.16.840.1.113883.3.51.1.1.6.1" extension="9494949494949" displayable="true" />
<addr use="PHYS">
<city>KAMLOOPS</city>
<country>CA</country>
<postalCode>V1B3C1</postalCode>
<state>BC</state>
<streetAddressLine>1A</streetAddressLine>
<streetAddressLine>2A</streetAddressLine>
<streetAddressLine>9494949494949</streetAddressLine>
<streetAddressLine>4A</streetAddressLine>
</addr>
<note text="9494949494949 should be stars"/>
Should be (The below XSLT already masks the extension in the node with the matching OID).
<identifiedPerson classCode="IDENT">
<id root="2.16.840.1.113883.3.51.1.1.6.1" nullFlavor="MSK" displayable="true" />
<addr use="PHYS">
<city>KAMLOOPS</city>
<country>CA</country>
<postalCode>V1B3C1</postalCode>
<state>BC</state>
<streetAddressLine>1A</streetAddressLine>
<streetAddressLine>2A</streetAddressLine>
<streetAddressLine>****</streetAddressLine>
<streetAddressLine>4A</streetAddressLine>
</addr>
<note text="**** should be stars"/>
Any help would be appreciated.
I am able to use XSL 2.0
I have the current XSL.IT works fine. It matches any tag where the root is '2.16.840.1.113883.3.51.1.1.6.1', kills all attributes and adds a nullFlavor="MSK". However, this will not search the entire XML for that same #.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="attrToKeep" select="'root'" />
<xsl:template match="* | node()">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="#*">
<xsl:choose>
<xsl:when test="../#root = '2.16.840.1.113883.3.51.1.1.6.1'">
<xsl:copy-of select=".[contains($attrToKeep, name())]" />
<xsl:attribute name="nullFlavor">MSK</xsl:attribute>
<!-- Need some way to use the value found in this node and hide the extension -->
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Any help would be appreciated.
Thanks,
Try using a variable to hold the value of the text to be replaced. Like this:
<xsl:variable
name="rootVar"
select="//*[#root = '2.16.840.1.113883.3.51.1.1.6.1']/#extension" />
And then you should just be able to use the replace function to replace them.
<xsl:template match="'//#*' | text()">
<xsl:sequence select="replace(., $rootVar, '****')"/>
</xsl:template>
The XSLT 2.0 stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="replacement" select="'****'"/>
<xsl:param name="new" select="'MKS'"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="identifiedPerson">
<xsl:copy>
<xsl:apply-templates select="#* , node()">
<xsl:with-param name="to-be-replaced" select="id/#extension" tunnel="yes"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="identifiedPerson//text()">
<xsl:param name="to-be-replaced" tunnel="yes"/>
<xsl:sequence select="replace(., $to-be-replaced, $replacement)"/>
</xsl:template>
<xsl:template match="identifiedPerson//#*">
<xsl:param name="to-be-replaced" tunnel="yes"/>
<xsl:attribute name="{name()}" namespace="{namespace-uri()}" select="replace(., $to-be-replaced, $replacement)"/>
</xsl:template>
<xsl:template match="identifiedPerson/id">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:attribute name="nullFlavor" select="$new"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="identifiedPerson/id/#extension"/>
</xsl:stylesheet>
transforms
<identifiedPerson classCode="IDENT">
<id root="2.16.840.1.113883.3.51.1.1.6.1" extension="9494949494949" displayable="true" />
<addr use="PHYS">
<city>KAMLOOPS</city>
<country>CA</country>
<postalCode>V1B3C1</postalCode>
<state>BC</state>
<streetAddressLine>1A</streetAddressLine>
<streetAddressLine>2A</streetAddressLine>
<streetAddressLine>9494949494949</streetAddressLine>
<streetAddressLine>4A</streetAddressLine>
</addr>
<note text="9494949494949 should be stars"/>
</identifiedPerson>
with Saxon 9.4 into
<?xml version="1.0" encoding="UTF-8"?><identifiedPerson classCode="IDENT">
<id root="2.16.840.1.113883.3.51.1.1.6.1" displayable="true" nullFlavor="MKS"/>
<addr use="PHYS">
<city>KAMLOOPS</city>
<country>CA</country>
<postalCode>V1B3C1</postalCode>
<state>BC</state>
<streetAddressLine>1A</streetAddressLine>
<streetAddressLine>2A</streetAddressLine>
<streetAddressLine>****</streetAddressLine>
<streetAddressLine>4A</streetAddressLine>
</addr>
<note text="**** should be stars"/>
</identifiedPerson>
So for the sample it solves that problem I think. I am not sure whether there can be more context around that sample and whether you want to change values outside of the identifiedPerson element as well or don't want to change them (which above stylesheet does). If other elements also need to be changed consider to post longer input and wanted result samples to illustrate and also explain what determines the node where the value to be replaced is found.
[edit]
Based on your comment I adapted the stylesheet, it now has a parameter to pass in a id (e.g. 2.16.840.1.113883.3.51.1.1.6.1), then it looks for an element of any name with a root attribute having that passed in id value and replaces the extension attribute value found in all attributes and all text nodes found in the document. Furthermore a nullFlavor attribute is added to the element with the id and its extension attribute is removed.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="root-id" select="'2.16.840.1.113883.3.51.1.1.6.1'"/>
<xsl:variable name="to-be-replaced" select="//*[#root = $root-id]/#extension"/>
<xsl:param name="replacement" select="'****'"/>
<xsl:param name="new" select="'MKS'"/>
<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:sequence select="replace(., $to-be-replaced, $replacement)"/>
</xsl:template>
<xsl:template match="#*">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}" select="replace(., $to-be-replaced, $replacement)"/>
</xsl:template>
<xsl:template match="*[#root = $root-id]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:attribute name="nullFlavor" select="$new"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[#root = $root-id]/#extension"/>
</xsl:stylesheet>
Related
Given this XML
<dmodule>
<content>
<warningsAndCautionsRef>
<warningRef id="w001" warningIdentNumber="warning-001">
</warningRef>
<warningRef id="w002" warningIdentNumber="warning-002">
</warningRef>
<cautionRef id="c001" cautionIdentNumber="caution-001">
</cautionRef>
<cautionRef id="c002" cautionIdentNumber="caution-002">
</cautionRef>
</warningsAndCautionsRef>
<faultReporting>
<preliminaryRqmts>
<reqSafety>
<safetyRqmts cautionRefs="c001 c002" warningRefs="w001 w002"/>
</reqSafety>
</preliminaryRqmts>
</faultReporting>
</content>
</dmodule>
I would like to tokenize the attributes #cautionRefs (and #warningRefs) and then find the cautionRef element that matches its #id to the tokenized value:
<xsl:template match="#cautionRefs">
<xsl:for-each select="tokenize(.,'\s')">
<xsl:apply-templates select="//*[#id=.]"/>
</xsl:for-each>
</xsl:template>
but the apply-templates fails: Fatal error during transformation Leading '/' selects nothing: the context item is not a node. It works if I don't tokenize and use string functions instead but that is not desirable.
Desired result:
Tokenize #cautionRefs="c001 c002" (which has multiple parent elements)
So each value is passed to the <cautionRef>template that will retrieve the caution and warning statements, to be displayed in a PDF:
<xsl:apply-templates select="//*[#id='c001']"/>
<xsl:apply-templates select="//*[#id='c002']"/>
I tried using <xsl:key name="id" match="*" use="#id"/> with
<xsl:for-each select="key('id',tokenize(.,'\s'))">
but the for-each is blank.
The above apply-templates will match with this <cautionRef> template, which retrieves the caution and warning statements correctly. I just need help with the context of the #cautionRefs template:
<xsl:template match="cautionRef">
<xsl:variable name="IdentNumber" select="#cautionIdentNumber"/>
<xsl:apply-templates select="//cautionSpec[cautionIdent/#cautionIdentNumber=$IdentNumber]"/>
</xsl:template>
You could use a variable and use that for context:
<xsl:template match="#cautionRefs|#warningRefs">
<xsl:variable name="ctx" select="/"/>
<xsl:for-each select="tokenize(.,'\s')">
<xsl:apply-templates select="$ctx//*[#id=.]"/>
</xsl:for-each>
</xsl:template>
but I would use a key like you hinted at (updated to include context based on comments)...
<xsl:key name="by_id" match="*[#id]" use="#id"/>
<xsl:variable name="root" select="/"/>
<xsl:template match="#cautionRefs|#warningRefs">
<xsl:for-each select="tokenize(.,'\s')">
<xsl:apply-templates select="key('by_id',.,$root)"/>
</xsl:for-each>
</xsl:template>
Here's a full working example. NB it's best to have this level of detail in the actual question; i.e. a sample input file, the XSLT, and output, along with an example of what you want the output to look like.
Input:
<test>
<safetyRqmts cautionRefs="c001 c002" warningRefs="w001"/>
<cautionRef id="c001" cautionIdentNumber="caution-001"/>
<cautionRef id="c002" cautionIdentNumber="caution-001"/>
</test>
Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="*|#*">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:key name="by_id" match="*[#id]" use="#id"/>
<xsl:template match="#cautionRefs|#warningRefs">
<xsl:apply-templates select="key('by_id', tokenize(.))"/>
</xsl:template>
</xsl:stylesheet>
Output:
<test>
<safetyRqmts><cautionRef id="c001" cautionIdentNumber="caution-001"/><cautionRef id="c002" cautionIdentNumber="caution-001"/></safetyRqmts>
<cautionRef id="c001" cautionIdentNumber="caution-001"/>
<cautionRef id="c002" cautionIdentNumber="caution-001"/>
</test>
I have one xml A and want to create a new xml B. The outer elements of B are different from A, but some of the child nodes are the same. I have written an XSL file but am struggling to get it right. Can anyone tell me where I am going wrong? How do I copy individual elements from A to B? The issue is, when the transformation happens, the text in A is copied to B. Looking at previous stack overflow questions, this is because there is an error in my xsl but I cant figure out what.
A:
<Request>
<Source>
<RequestorID Client="1" EMailAddress="test#test.com" Password="pwd"/>
<RequestorPreferences Country="JP" Currency="jpy" Language="en">
<RequestMode>SYNCHRONOUS</RequestMode>
</RequestorPreferences>
</Source>
<RequestDetails>
<SearchHotelPriceRequest>
<ItemDestination DestinationCode="LON" DestinationType="city"/>
<ItemCode>98i</ItemCode>
<PeriodOfStay>
<CheckInDate>2015-05-20</CheckInDate>
<CheckOutDate>2015-05-21</CheckOutDate>
</PeriodOfStay>
<IncludePriceBreakdown/>
<IncludeChargeConditions/>
<Rooms>
<Room Code="tb" NumberOfCots="0" NumberOfRooms="1">
<ExtraBeds/>
</Room>
</Rooms>
</SearchHotelPriceRequest>
</RequestDetails>
</Request>
B:
<WebRequest>
<RequestDetails>
<WebSearchHotelPriceRequest
CallCentreClientUI="2577"
Client="1"
Country="JP"
Currency="jpy"
Language="en"
LoginID="">
<GcPriceOptions ShowDeduped="true"/>
<ItemDestination DestinationCode="LON" DestinationType="city"/>
<ItemName></ItemName>
<ItemCode>98i</ItemCode>
<EffectiveDate>2014-08-15</EffectiveDate>
<StarRatingRange>
<Min>0</Min>
<Max>0</Max>
</StarRatingRange>
<PeriodOfStay>
<CheckInDate>2015-05-20</CheckInDate>
<CheckOutDate>2015-05-21</CheckOutDate>
</PeriodOfStay>
<IncludeChargeConditions/>
<Rooms>
<Room Code="tb" NumberOfRooms="1"></Room>
</Rooms>
<SiteId>008</SiteId>
</WebSearchHotelPriceRequest>
</RequestDetails>
</WebRequest>
XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Request">
<xsl:element name="WebRequest">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="RequestDetails">
<xsl:element name="RequestDetails">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="SearchHotelPriceRequest">
<xsl:element name="WebSearchHotelPriceRequest">
<xsl:attribute name="CallCentreClientUI">1</xsl:attribute>
<xsl:attribute name="Client">1</xsl:attribute>
<xsl:attribute name="Country">UK</xsl:attribute>
<xsl:attribute name="Currency">GBP</xsl:attribute>
<xsl:attribute name="Language">EN</xsl:attribute>
<xsl:attribute name="LoginID">100</xsl:attribute>
<SiteId>001</SiteId>
<EffectiveDate>01-01-99</EffectiveDate>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="ItemDestination">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="ItemCode">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="ItemName">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="PeriodOfStay">
<xsl:copy>
<xsl:copy-of select="node()" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="IncludeChargeConditions">
<xsl:copy>
<xsl:copy-of select="node()" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="Rooms">
<xsl:copy>
<xsl:copy-of select="node()" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Incorrect Output:
<?xml version="1.0" encoding="UTF-8"?>
<WebRequest>SYNCHRONOUS<RequestDetails>
<WebSearchHotelPriceRequest CallCentreClientUI="1" Client="1" Country="UK" Currency="GBP" Language="EN" LoginID="100">
<SiteId>001</SiteId>
<EffectiveDate>01-01-99</EffectiveDate>
<ItemDestination DestinationCode="LON" DestinationType="city"/>
<ItemCode>98i</ItemCode>
<PeriodOfStay>
<CheckInDate>2015-05-20</CheckInDate>
<CheckOutDate>2015-05-21</CheckOutDate>2015-05-202015-05-21</PeriodOfStay>
<IncludeChargeConditions/>
<Rooms>
<Room Code="tb" NumberOfCots="0" NumberOfRooms="1">
<ExtraBeds/>
</Room>
</Rooms>
</WebSearchHotelPriceRequest>
</RequestDetails>
</WebRequest>
I think you are making this much more complicated than it needs to be. The simplest way to copy elements (along with all they contain, i.e. "deep copy") is to use xsl:copy-of. Also, you don't need to use xsl:element if you know the name of the element; just output the element literally. Similarly, for xsl:attribute you can write it directly and - if necessary - use attribute value template to insert the value from the input.
Have a look at the following stylesheet as an example:
<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"/>
<xsl:template match="/">
<WebRequest>
<xsl:for-each select="Request/RequestDetails/SearchHotelPriceRequest">
<RequestDetails>
<WebSearchHotelPriceRequest
CallCentreClientUI="2577"
Client="2577"
Country="J"
Currency="USD"
Language="E"
LoginID="">
<GcPriceOptions ShowDeduped="true"/>
<xsl:copy-of select="ItemDestination"/>
<ItemName></ItemName>
<xsl:copy-of select="ItemCode"/>
<EffectiveDate>2014-08-15</EffectiveDate>
<StarRatingRange>
<Min>0</Min>
<Max>0</Max>
</StarRatingRange>
<xsl:copy-of select="PeriodOfStay | IncludeChargeConditions | Rooms"/>
<SiteId>008</SiteId>
</WebSearchHotelPriceRequest>
</RequestDetails>
</xsl:for-each>
</WebRequest>
</xsl:template>
</xsl:stylesheet>
This is of course not exactly what you need, but I don't know which values go where.
If someone could tell me why the extra text (e.g. SYNCHRONOUS) is
being erroneously copied?
Because you are using xsl:apply templates indiscriminately - and text nodes are copied by default using the built-in templates.
Given the following XML document:
<dialog>
<speech speaker="Robert">
<line>"Once more into the breach", he said.</line>
I can't believe him. I just <emphasis>can't</emphasis> believe him!
</speech>
</dialog>
I'd like to try and capture eveything within speech that isn't in a line already and wrap it in a line, however I need to capture any other elements along with it (eg. the emphasis in the example above).
The result I'd like to achieve is:
<dialog>
<speech speaker="Robert">
<line>"Once more into the breach", he said.</line>
<line>I can't believe him. I just <emphasis>can't</emphasis> believe him!</line>
</speech>
</dialog>
I'm using libxslt and libxml, so I'm stuck with XSLT 1.0.
One way to approach that in XSLT 1.0 is through sibling recursion, as outlined in the following example:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="speech">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="node()[1]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="speech/line">
<xsl:call-template name="identity"/>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="speech/node()[not(self::line)
and (not(preceding-sibling::node())
or
preceding-sibling::node()[1][self::line])]">
<xsl:if test="normalize-space() or following-sibling::node()[1][not(self::line)]">
<line>
<xsl:call-template name="identity"/>
<xsl:apply-templates select="following-sibling::node()[1][not(self::line)]"/>
</line>
</xsl:if>
<xsl:apply-templates select="following-sibling::line[1]"/>
</xsl:template>
<xsl:template match="speech/node()[not(self::line)
and preceding-sibling::node()[1][not(self::line)]]">
<xsl:call-template name="identity"/>
<xsl:apply-templates select="following-sibling::node()[1][not(self::line)]"/>
</xsl:template>
</xsl:stylesheet>
With that stylesheet an input sample like
<dialog>
<speech speaker="Robert">
<line>"Once more into the breach", he said.</line>
I can't believe him. I just <emphasis>can't</emphasis> believe him!
</speech>
<speech speaker="Foo">This is a test.
<line>This line is wrapped and should be copied unchanged.</line>
<em>This</em> needs to be <it>wrapped</it>.
</speech>
<speech speaker="Bar"> <em>This</em> should be wrapped.
<line>This line is wrapped and should be copied unchanged.</line>
<it>Test</it>
</speech>
</dialog>
is transformed into
<dialog>
<speech speaker="Robert"><line>"Once more into the breach", he said.</line><line>
I can't believe him. I just <emphasis>can't</emphasis> believe him!
</line></speech>
<speech speaker="Foo"><line>This is a test.
</line><line>This line is wrapped and should be copied unchanged.</line><line>
<em>This</em> needs to be <it>wrapped</it>.
</line></speech>
<speech speaker="Bar"><line> <em>This</em> should be wrapped.
</line><line>This line is wrapped and should be copied unchanged.</line><line>
<it>Test</it>
</line></speech>
</dialog>
I have couple of custom types which have 2, 3 or 4 childs. So wherever I get these childs I need to combine them into a single element which is the parent tag itself in the output XML. I tried but could not do due to lack of experience with xslt. Can any one help.
My input XML.
<PERSON>
<ID>194</ID>
<NAME>IKHAJA</NAME>
<DETAILS>
<NUMBER>100</NUMBER>
<Description />
<NUMBER01 />
<NUMBER02>Test</NUMBER02>
</DETAILS>
<STATUS>
<NUMBER>ACTIVE</NUMBER>
<Description>ACTIVE</Description>
<NUMBER01 />
<NUMBER02>ACTIVE</NUMBER02>
</STATUS>
<employer>
<ID>123456</ID>
<FNAME>EMPLOYER F NAME</FNAME>
<LNAME>EMPLOYER L NAME</LNAME>
</employer>
<PERSON_OFF>
<TYPE>
<NUMBER>41</NUMBER>
<Description>AMPLIFIERS</Description>
<NUMBER01>77</NUMBER01>
<NUMBER02 />
</TYPE>
<REPORT>
<NUMBER />
<Description />
<NUMBER01 />
<NUMBER02 />
</REPORT>
<SERIAL>111</SERIAL>
<ADDITIONAL_DESC>TEST</ADDITIONAL_DESC>
<KEY>5</KEY>
<CREATED_BY>Test Guy</CREATED_BY>
<CREATED_ON>2013-03-13T10:03:00</CREATED_ON>
<PERSON_OFF_ONE>
<BULK>
<NUMBER>98078</NUMBER>
<Description>BULK</Description>
<NUMBER01 />
<NUMBER02>8563</NUMBER02>
</BULK>
<CHECKED>Y</CHECKED>
</PERSON_OFF_ONE>
</PERSON_OFF>
</PERSON>
And my output XML should be like this:
<PERSON>
<ID>194</ID>
<NAME>IKHAJA</NAME>
<DETAILS>100;;;Test</DETAILS>
<STATUS>ACTIVE;ACTIVE;;ACTIVE</STATUS>
<employer>123456:EMPLOYER F NAME,EMPLOYER L NAME</employer>
<PERSON_OFF>
<TYPE>41;AMPLIFIERS;77;</TYPE>
<REPORT>;;;</REPORT>
<SERIAL>111</SERIAL>
<ADDITIONAL_DESC>TEST</ADDITIONAL_DESC>
<KEY>5</KEY>
<CREATED_BY>Test Guy</CREATED_BY>
<CREATED_ON>2013-03-13T10:03:00</CREATED_ON>
<PERSON_OFF_ONE>
<BULK>98078;BULK;;8563</BULK>
<CHECKED>Y</CHECKED>
</PERSON_OFF_ONE>
</PERSON_OFF>
</PERSON>
If you observe here details, status, bulk etc. are my custom types which have child nodes NUMBER, Description, NUMBER01, NUMBRER02. and I need to combine them with a separator ";" if they are empty or null just I will have ";;;" in my destination column as shown in REPORT field.
Also I have some fields of employer type like employer with childs ID, FNAME and LNAME and I should combine them as ID: FNAME, LNAME as shown in employer field.
I think if I know handling one custom type, I can handle the other types easily.
Can you please help? I already spent whole day on this and I need to do this very badly ASAP.
OP's comment to the answer by JLRishe:
It works if I list all of my custom types but, here I have so many
fields that are of my custom types. Instead of listing out all those
like "DETAILS | STATUS | TYPE | REPORT | BULK", is there any way to
merge fields of these.
This can be done easily, using the fact that all these possibly hundreds of parent elements have one of four children.
A minor adjustment to JLRishe's solution works with unlimited number of parent names:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[NUMBER|Description|NUMBER01|NUMBER02]">
<xsl:copy>
<xsl:apply-templates select="*" mode="delimit" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[position() > 1]" mode="delimit">
<xsl:value-of select="concat(';', .)"/>
</xsl:template>
<xsl:template match="employer">
<xsl:copy>
<xsl:apply-templates select="*" mode="idlist" />
</xsl:copy>
</xsl:template>
<xsl:template match="ID" mode="idlist">
<xsl:value-of select="concat(., ':')" />
</xsl:template>
<xsl:template match="*[not(self::ID)][position() > 1]" mode="idlist">
<xsl:value-of select="concat(',', .)" />
</xsl:template>
</xsl:stylesheet>
As you see, this transformation doesn't mention at all any parent names such as DETAILS, STATUS, TYPE, REPORT, BULK, ..., etc.
and when applied on the provided XML document, produces the wanted, correct result:
<PERSON>
<ID>194</ID>
<NAME>IKHAJA</NAME>
<DETAILS>100;;;Test</DETAILS>
<STATUS>ACTIVE;ACTIVE;;ACTIVE</STATUS>
<employer>123456:EMPLOYER F NAME,EMPLOYER L NAME</employer>
<PERSON_OFF>
<TYPE>41;AMPLIFIERS;77;</TYPE>
<REPORT>;;;</REPORT>
<SERIAL>111</SERIAL>
<ADDITIONAL_DESC>TEST</ADDITIONAL_DESC>
<KEY>5</KEY>
<CREATED_BY>Test Guy</CREATED_BY>
<CREATED_ON>2013-03-13T10:03:00</CREATED_ON>
<PERSON_OFF_ONE>
<BULK>98078;BULK;;8563</BULK>
<CHECKED>Y</CHECKED>
</PERSON_OFF_ONE>
</PERSON_OFF>
</PERSON>
Please give this a try:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DETAILS | STATUS | TYPE | REPORT | BULK">
<xsl:copy>
<xsl:apply-templates select="*" mode="delimit" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[position() > 1]" mode="delimit">
<xsl:value-of select="concat(';', .)"/>
</xsl:template>
<xsl:template match="employer">
<xsl:copy>
<xsl:apply-templates select="*" mode="idlist" />
</xsl:copy>
</xsl:template>
<xsl:template match="ID" mode="idlist">
<xsl:value-of select="concat(., ':')" />
</xsl:template>
<xsl:template match="*[not(self::ID)][position() > 1]" mode="idlist">
<xsl:value-of select="concat(',', .)" />
</xsl:template>
</xsl:stylesheet>
When run on your sample input, the result is:
<PERSON>
<ID>194</ID>
<NAME>IKHAJA</NAME>
<DETAILS>100;;;Test</DETAILS>
<STATUS>ACTIVE;ACTIVE;;ACTIVE</STATUS>
<employer>123456:EMPLOYER F NAME,EMPLOYER L NAME</employer>
<PERSON_OFF>
<TYPE>41;AMPLIFIERS;77;</TYPE>
<REPORT>;;;</REPORT>
<SERIAL>111</SERIAL>
<ADDITIONAL_DESC>TEST</ADDITIONAL_DESC>
<KEY>5</KEY>
<CREATED_BY>Test Guy</CREATED_BY>
<CREATED_ON>2013-03-13T10:03:00</CREATED_ON>
<PERSON_OFF_ONE>
<BULK>98078;BULK;;8563</BULK>
<CHECKED>Y</CHECKED>
</PERSON_OFF_ONE>
</PERSON_OFF>
</PERSON>
Are there any XSLT statements that will execute in consideration of other XSLT statements within the same stylesheet?
For example, if I have two copy statements matched to the same node (but only desire one copied node that contains the modifications declared in BOTH copy statements) is there a statement that will do this?
Assume that I cannot put all the transformations in one copy node, but instead have to use two or more.
---Clearer example---
//XML
<toy></toy>
//XSLT
<xsl:template match="toy">
<xsl:copy>
<xsl:attribute name="label">SOME TOY</xsl:attribute>
</xsl:copy>
</xsl:template>
<xsl:template match="toy">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:element name="range">
<xsl:element name="min">200001</xsl:element>
<xsl:element name="max">999999</xsl:element>
</xsl:element>
</xsl:copy>
</xsl:template>
My desired result would be a new toy node that is copied to a new file that has both things applied to it, so something like:
<toy label='SOME TOY'>
<range>
<min>200001</min>
<max>999999</max>
</range>
</toy>
Not two different copies
Is this possible? Is there some way I can redo the first template so that will make this one outcome?
There is rule in XSLT specification, which forbid this - Conflict Resolution for Template Rules.
If node fits several templates - only one template will be executed - in relation to the template import precedence, priority or document order, etc.
But you can separate it with named templates:
<xsl:template match="toy">
<xsl:call-template name="toyAttribute" />
<xsl:call-template name="toyElements" />
</xsl:template>
<xsl:template name="toyAttribute">
<xsl:copy>
<xsl:attribute name="label">SOME TOY</xsl:attribute>
</xsl:copy>
</xsl:template>
<xsl:template name="toyElements">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:element name="range">
<xsl:element name="min">200001</xsl:element>
<xsl:element name="max">999999</xsl:element>
</xsl:element>
</xsl:copy>
</xsl:template>
Update:
If you asking about only updating <toy> node with attribute and elements you don't need separate templates:
<!--toy template -->
<xsl:template match="/toys/toy">
<!--copy toy node with namespaces -->
<xsl:copy>
<!-- copy toy node attributes -->
<xsl:apply-templates select="#*" />
<!-- add new attribute or xsl:call-template name="toyAttribute"-->
<xsl:attribute name="label">SOME TOY</xsl:attribute>
<!-- copy toy node child elements -->
<xsl:apply-templates select="node()" />
<!-- add new elements - or xsl:call-template name="toyElements"-->
<xsl:element name="range">
<xsl:element name="min">200001</xsl:element>
<xsl:element name="max">999999</xsl:element>
</xsl:element>
</xsl:copy>
</xsl:template>
<!--Copy node content -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
which for XML:
<?xml version="1.0" encoding="UTF-8"?>
<toys>
<toy name="a">
<toy-part/>
</toy>
<toy name="b">
<toy-part/>
</toy>
</toys>
will give following result:
<?xml version="1.0" encoding="utf-8"?><toys>
<toy name="a" label="SOME TOY">
<toy-part/>
<range>
<min>200001</min>
<max>999999</max>
</range>
</toy>
<toy name="b" label="SOME TOY">
<toy-part/>
<range>
<min>200001</min>
<max>999999</max>
</range>
</toy>
</toys>