The input xml structure is like this:
<Envelopes>
<env:Envelope>
<urn:MyFunction>
<parameter1 attr1='df'>fdad</parameter1>
<parameter2 attr2='ww'>dfsa</parameter2>
<productData>
<Id></Id>
<Description></Description>
<Price><Price>
</productData>
</urn:MyFunction>
</env:Envelope>
<env:Envelope>
<urn:MyFunction1>
<parameter1 attr1='df'>fdad</parameter1>
<parameter2 attr2='ww'>dfsa</parameter2>
<productData>
<Id></Id>
<Description></Description>
<Price><Price>
</productData>
</urn:MyFunction>
</env:Envelope>
<env:Envelope>
<urn:MyFunction1>
<parameter1 attr1='df'>fdad</parameter1>
<parameter2 attr2='ww'>dfsa</parameter2>
<productData>
<Id></Id>
<Description></Description>
<Price><Price>
</productData>
</urn:MyFunction>
</env:Envelope>
<Envelopes>
In my xsl I am doing the below:
<xsl:template match="/">
<NewEnvelopes>
<xsl:for-each select="//productData">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</NewEnvelopes>
</xsl:template>
<xsl:template match="productData/Description">
<Description>new Description</Description>
</xsl:template>
I intend to keep the rest of the productData elements and attributes same, but modify some of them. But the resulting xml gives the description element with the new value, but only the text nodes for the rest of the elements. How can I get all the rest of the nodes of productData?
You need an identity template that will copy the input content. Try adding this to your XSLT:
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
Related
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>
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.
Is it possible to keep comments in an XML when applying an XSLT to it?
Example (source):
<rootNode>
<!-- My comment --><childElement>5</childElement>
</rootNode>
The sample result after the transformation shall be:
<newRoot>
<!-- My comment --><newChildElement>5</newChildElement>
</newRoot>
How would you write the stylesheet?
That is not proper XML comment syntax in your sample but you can keep all nodes with
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
then you add templates for the nodes to be transformed
<xsl:template match="childElement">
<newChildElement>
<xsl:apply-templates select="#* | node()"/>
</newChildElement>
</xsl:template>
My suggestions assumes the sample as
<rootNode>
<!-- My comment--><childElement>5</childElement>
</rootNode>
and the result as
<rootNode>
<!-- My comment--><newChildElement>5</newChildElement>
</rootNode>
If the childElement is inside the comment then it is more difficult.
Pretty straightforward, if I understand the question correctly:
<xsl:template match="comment()">
<xsl:copy/>
</xsl:template>
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>
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>