How to transform a XML document with metadata using XSLT? - xslt

I have a report generator tool that produces XML documents for different kinds of reports. Each of those XML documents will have a set of fields (reporting:columns in the below XML document) which are used to describe the actual data (reporting:line in the below XML document) in it. Since the list of fields is dynamic and keep changing for every kind of report, how to write a generic XSL template that use the 'fields' to transform the data into a csv file.
My sample XML document:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<reporting:root xmlns:reporting="http://www.something.net/reporting">
<reporting:default0 reporting:type="Portfolio">
<reporting:header>
<reporting:configuration>
<reporting:columns>
<reporting:column reporting:group="instrument" reporting:name="Ident" reporting:tag="ident" reporting:type="int"/>
<reporting:column reporting:group="prices" reporting:name="Last (Time)" reporting:tag="lastTime" reporting:type="string"/>
<reporting:column reporting:group="noGroup" reporting:name="RIC" reporting:tag="ric" reporting:type="string"/>
<reporting:column reporting:group="instrument" reporting:name="Reference" reporting:tag="reference" reporting:type="string"/>
<reporting:column reporting:group="result" reporting:name="Currency" reporting:tag="currency" reporting:type="string"/>
</reporting:columns>
</reporting:configuration>
</reporting:header>
<reporting:window reporting:Id="36674" reporting:level="0" reporting:name="MY_PORTFOLIO" reporting:parentId="11991">
<reporting:line reporting:Id="67520135" reporting:level="1" reporting:name="INTERNATIONAL BUSINESS MACHINES CORP" reporting:parentId="36674" reporting:positionType="0">
<reporting:ident>643633</reporting:ident>
<reporting:reference>IBM.USD</reporting:reference>
<reporting:currency>USD</reporting:currency>
</reporting:line>
<reporting:line reporting:Id="67520179" reporting:level="1" reporting:name="GENERAL ELECTRIC CO" reporting:parentId="36674" reporting:positionType="0">
<reporting:ident>643635</reporting:ident>
<reporting:ric>GE.N</reporting:ric>
<reporting:reference>GE.USD</reporting:reference>
<reporting:currency>USD</reporting:currency>
</reporting:line>
</reporting:window>
</reporting:default0>
</reporting:root>
I need to see the output formated like:
ident,lastTime,ric,reference,currency
643633,,,IBM.USD,USD
643635,,GE.N,GE.USD,USD
I did some work on it but it was half way through. Could not find how the handle the 'dynamic' list of fields and use them as tags that describe the actual data.
Can somebody help ?
Thanks.

There are various ways of tackling this in XSLT, but I think the most elegant and maintainable is to write a stylesheet that processes the source document given, and outputs an XSLT stylesheet to do the actual reporting. I've seen this approach used very succesfully in a number of projects.

<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="/*:root/*:default0/*:header/*:configuration/*:columns/*:column">
<xsl:value-of select="#reporting:tag"/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:for-each select="/*:root/*:default0/*:window/*:line">
<xsl:variable name="theline" select="."/>
<xsl:text>
</xsl:text>
<xsl:for-each select="/*:root/*:default0/*:header/*:configuration/*:columns/*:column" >
<xsl:variable name="oname" select="#reporting:tag" />
<xsl:value-of select="$theline/*[local-name()=$oname]" /><xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Will produce:
ident,lastTime,ric,reference,currency,
643633,,,IBM.USD,USD,
643635,,GE.N,GE.USD,USD,
( You'll need to stick on a check for last to get rid of the final comma. )

Try
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://www.something.net/reporting">
<xsl:output method="text" version="1.0"/>
<xsl:template match="/">
<xsl:for-each select="/r:root/r:default0/r:header/r:configuration/r:columns/r:column">
<xsl:value-of select="#r:tag"/>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:for-each select="/r:root/r:default0/r:window/r:line">
<xsl:variable name="POSITION" select="position()"/>
<xsl:for-each select="//r:root/r:default0/r:header/r:configuration/r:columns/r:column">
<xsl:variable name="TAGNAME" select="#r:tag"/><xsl:value-of select="//r:line[$POSITION]/*[local-name()=$TAGNAME]"/><xsl:if test="position() != last()">,</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note that I just shortened the namespace prefix to make it easier to read (and type).

This short and simple transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:r="http://www.something.net/reporting">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vColNames" select="/*/*/r:header/*/*/*/#r:tag"/>
<xsl:template match="/">
<xsl:for-each select="$vColNames">
<xsl:value-of select="concat(., ',')"/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="r:line">
<xsl:variable name="vThis" select="."/>
<xsl:text>
</xsl:text>
<xsl:for-each select="$vColNames">
<xsl:if test="position() > 1">,</xsl:if>
<xsl:apply-templates select="$vThis/*[local-name() = current()]"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
when run on the provided XML document:
<reporting:root xmlns:reporting="http://www.something.net/reporting">
<reporting:default0 reporting:type="Portfolio">
<reporting:header>
<reporting:configuration>
<reporting:columns>
<reporting:column reporting:group="instrument" reporting:name="Ident" reporting:tag="ident" reporting:type="int"/>
<reporting:column reporting:group="prices" reporting:name="Last (Time)" reporting:tag="lastTime" reporting:type="string"/>
<reporting:column reporting:group="noGroup" reporting:name="RIC" reporting:tag="ric" reporting:type="string"/>
<reporting:column reporting:group="instrument" reporting:name="Reference" reporting:tag="reference" reporting:type="string"/>
<reporting:column reporting:group="result" reporting:name="Currency" reporting:tag="currency" reporting:type="string"/>
</reporting:columns>
</reporting:configuration>
</reporting:header>
<reporting:window reporting:Id="36674" reporting:level="0" reporting:name="MY_PORTFOLIO" reporting:parentId="11991">
<reporting:line reporting:Id="67520135" reporting:level="1" reporting:name="INTERNATIONAL BUSINESS MACHINES CORP" reporting:parentId="36674" reporting:positionType="0">
<reporting:ident>643633</reporting:ident>
<reporting:reference>IBM.USD</reporting:reference>
<reporting:currency>USD</reporting:currency>
</reporting:line>
<reporting:line reporting:Id="67520179" reporting:level="1" reporting:name="GENERAL ELECTRIC CO" reporting:parentId="36674" reporting:positionType="0">
<reporting:ident>643635</reporting:ident>
<reporting:ric>GE.N</reporting:ric>
<reporting:reference>GE.USD</reporting:reference>
<reporting:currency>USD</reporting:currency>
</reporting:line>
</reporting:window>
</reporting:default0>
</reporting:root>
produces the wanted, correct result:
ident,lastTime,ric,reference,currency,
643633,,,IBM.USD,USD
643635,,GE.N,GE.USD,USD

Related

Remove duplicate values in child nodes of XML file using XSLT

I have the below xml data and I want remove the duplicate values from this xml.
<Report_Data>
<Report_Entry>
<Classifications_group>
<ClassificationGroupName Descriptor="EEO-1 Job Categories">
</ClassificationGroupName>
<ClassificationDescription>Professionals</ClassificationDescription>
</Classifications_group>
<Classifications_group>
<ClassificationGroupName Descriptor="Hartford Job Category">
</ClassificationGroupName>
<ClassificationDescription>Other</ClassificationDescription>
</Classifications_group>
<Classifications_group>
<ClassificationGroupName Descriptor="Hartford Job Category">
</ClassificationGroupName>
<ClassificationDescription>Other</ClassificationDescription>
</Classifications_group>
</Report_Entry>
<Report_Entry>
<Classifications_group>
<ClassificationGroupName Descriptor="EEO-1 Job Categories">
</ClassificationGroupName>
<ClassificationDescription>Administrative Support Workers</ClassificationDescription>
</Classifications_group>
<Classifications_group>
<ClassificationGroupName Descriptor="Hartford Job Category">
</ClassificationGroupName>
<ClassificationDescription>Other</ClassificationDescription>
</Classifications_group>
</Report_Entry>
</Report_Data>
I have used the following XSLT to remove duplicate values.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="2.0">
<xsl:variable name="CRLF" select="'
'"/>
<xsl:output indent="no" method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Report_Data">
<xsl:value-of select="'ClassificationGroupName,ClassificationDescription'"/>
<xsl:value-of select="$CRLF"/>
<xsl:for-each select="Report_Entry">
<xsl:for-each-group select="Classifications_group" group-by="concat(ClassificationGroupName/#Descriptor, '|', ClassificationDescription)">
<xsl:value-of select="ClassificationGroupName/#Descriptor"/>
<xsl:value-of select="','"/>
<xsl:value-of select="ClassificationDescription"/>
<xsl:value-of select="$CRLF"/>
</xsl:for-each-group>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
ClassificationGroupName,ClassificationDescription
EEO-1 Job Categories,Professionals
Hartford Job Category,Other
EEO-1 Job Categories,Administrative Support Workers
Hartford Job Category,Other
Excepted output:
ClassificationGroupName,ClassificationDescription
EEO-1 Job Categories,Professionals
Hartford Job Category,Other
EEO-1 Job Categories,Administrative Support Workers
With the code that I have written, it removes duplicates only within the Report_Entry. I want to remove if there any other duplicate values with both ClassificationGroupName and ClassificationDescription of Classifications_group in any other Report_Entry as well.
What are the changes do I need to do to get the expected output?
I think you want to group all entries e.g. in XSLT 3 with a composite key
<xsl:template match="Report_Data">
<xsl:for-each-group select="Report_Entry/Classifications_group" composite="yes" group-by="ClassificationGroupName/#Descriptor, ClassificationDescription">
<xsl:value-of select="current-grouping-key()" separator=", "/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
or, with your XSLT 2 approach, using
<xsl:template match="Report_Data">
<xsl:value-of select="'ClassificationGroupName,ClassificationDescription'"/>
<xsl:value-of select="$CRLF"/>
<xsl:for-each-group select="Report_Entry/Classifications_group" group-by="concat(ClassificationGroupName/#Descriptor, '|', ClassificationDescription)">
<xsl:value-of select="ClassificationGroupName/#Descriptor"/>
<xsl:value-of select="','"/>
<xsl:value-of select="ClassificationDescription"/>
<xsl:value-of select="$CRLF"/>
</xsl:for-each-group>
</xsl:template>

How to remove duplicate entry - XSLT

I am try to remove duplicate entry after entity § and if contains the , in entry and after tokenize the start-with the ( round bracket then entry e.g (17200(b)(2), (4)–(6)) s/b e.g (<p>17200(b)(2)</p><p>17200(b)(4)–(6)</p>).
Input XML
<root>
<p>CC §1(a), (b), (c)</p>
<p>Civil Code §1(a), (b)</p>
<p>CC §§2(a)</p>
<p>Civil Code §3(a)</p>
<p>CC §1(c)</p>
<p>Civil Code §1(a), (b), (c)</p>
<p>Civil Code §17200(b)(2), (4)–(6), (8), (12), (16), (20), and (21)</p>
</root>
Expected Output
<root>
<sec specific-use="CC">
<title content-type="Sta_Head3">CIVIL CODE</title>
<p>1(a)</p>
<p>1(b)</p>
<p>1(c)</p>
<p>2(a)</p>
<p>3(a)</p>
<p>17200(b)(2)</p>
<p>17200(b)(4)–(6)</p>
<p>17200(b)(8)</p>
<p>17200(b)(12)</p>
<p>17200(b)(16)</p>
<p>17200(b)(20)</p>
<p>17200(b)(21)</p>
</sec>
</root>
XSLT Code
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="p[(starts-with(., 'CC ') or starts-with(., 'Civil Code'))]" group-by="replace(substring-before(., ' §'), 'Civil Code', 'CC')">
<xsl:text>
</xsl:text>
<sec specific-use="{current-grouping-key()}">
<xsl:text>
</xsl:text>
<title content-type="Sta_Head3">CIVIL CODE</title>
<xsl:for-each-group select="current-group()" group-by="replace(substring-after(., '§'), '§', '')">
<xsl:sort select="replace(current-grouping-key(), '[^0-9.].*$', '')" data-type="number" order="ascending"/>
<xsl:for-each
select="distinct-values(
current-grouping-key() !
(let $tokens := tokenize(current-grouping-key(), ', and |, | and ')
return (head($tokens), tail($tokens) ! (substring-before(head($tokens), '(') || .)))
)" expand-text="yes">
<p>{.}</p>
</xsl:for-each>
</xsl:for-each-group>
</sec>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
You could do it like this, in a two-step approach where you first compute the list of existing elements and then use a for-each-group to remove duplicates.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="listP">
<xsl:apply-templates select="root/p"/>
</xsl:variable>
<xsl:for-each-group select="$listP" group-by="p">
<p><xsl:value-of select="current-grouping-key()"/></p>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<xsl:variable name="input" select="replace(substring-after(.,'§'),'§','')"/>
<xsl:variable name="chapter" select="substring-before($input,'(')"/>
<xsl:for-each select="tokenize(substring-after($input, $chapter),',')">
<p><xsl:value-of select="concat($chapter,replace(replace(.,' ',''),'and',''))"/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/gVrvcxQ

xsl:if first and last node not retrieved

I have an XML as follows:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ExistingReservations>
<reservations>
<reservation>
<type>1</type>
<start_time>2019-03-09T16:11:14Z</start_time>
<stop_time>2019-03-09T16:23:23Z</stop_time>
</reservation>
<reservation>
<type>2</type>
<start_time>2019-03-09T11:23:12Z</start_time>
<stop_time>2019-03-09T11:32:18Z</stop_time>
</reservation>
<reservation>
<type>2</type>
<start_time>2019-03-09T12:23:12Z</start_time>
<stop_time>2019-03-09T12:32:18Z</stop_time>
</reservation>
</reservations>
</ExistingReservations>
I want to only look at reservations of type '2' and then get the range of dates. i.e. the first start_time and the last end time.
But i'm struggling with my xsl as I can't seem to get the first and last positions.
My xsl is as follows:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="ExistingReservations">
<ReservationSchedule>
<xsl:for-each select="//reservation">
<xsl:if test="type='2'">
<period>
<xsl:if test="position()=1">
<start_time><xsl:value-of select="start_time"/><start_time>
</xsl:if>
<xsl:if test="position() = last()">
<end_time><xsl:value-of select="stop_time"/></end_time>
</xsl:if>
</period>
</xsl:if>
</xsl:for-each>
</ReservationSchedule>
</xsl:template>
</xsl:stylesheet>
So I want to transform to the following:
<ReservationSchedule>
<period>
<start_time>2019-03-09T11:23:12Z</start_time>
<end_time>2019-03-09T12:32:18Z</end_time>
</period>
</ReservationSchedule>
I think in the <xsl:if test="position()=1"> is not working because it's looking at the first node whichi s type 1.. i.e. it's igoring the <xsl:if test="type='2'"> logic.
Any help appreciated.
Your xsl:for-each selects all reservations, so position() will be based on that selected node set. The xsl:if will not affect the position() function.
What you need to do is change the select statement itself, so only reservations of type 2 are selected in the first place
<xsl:template match="ExistingReservations">
<ReservationSchedule>
<xsl:for-each select="//reservation[type='2']">
<period>
<xsl:if test="position()=1">
<start_time><xsl:value-of select="start_time"/></start_time>
</xsl:if>
<xsl:if test="position() = last()">
<end_time><xsl:value-of select="stop_time"/></end_time>
</xsl:if>
</period>
</xsl:for-each>
</ReservationSchedule>
</xsl:template>
Alternatively, you can do away with the xsl:for-each and write it like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="ExistingReservations">
<ReservationSchedule>
<xsl:variable name="type2s" select="//reservation[type='2']" />
<period>
<start_time><xsl:value-of select="$type2s[1]/start_time"/></start_time>
<end_time><xsl:value-of select="$type2s[last()]/stop_time"/></end_time>
</period>
</ReservationSchedule>
</xsl:template>
</xsl:stylesheet>

How to Optimize XSLT for Referencing data in other XMLs

In an input XML file, along with Static Columns, columns expecting data from other files (reference)is also available.
But for each reference, the input xml has separate row with same ID or UID.
The output file has to have all references and relations in one row (based on the ID or UID)
I wrote the XSLT for this transformation also. This XSLT is faster when the row count is less (< 100 or < 200). But, as the count grows, the output xml generation taking long time (for count of 1000 rows, around 30 mins).
I am using
<xsl:for-each select="z:row/#ID[generate-id() = generate-id(key('UniqueID',.))]">
in the XSLT. Because for the same ID in each row of input xml, it has to check for multiple references (like section) and relations (like Child) and populate the same as columns
Input Raw XML File.
<xml xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:z="#RowsetSchema">
<rs:data>
<z:row UID="PARENT_001_1221AD_A878" GroupID="" GroupRel="" ID="37" Name="Outer Asset Details" RelProduct="Line1" RelUID="CHILD1_101_9899_9POOU99" RelName="CHILD1" RelType="Child" Size="22"/>
<z:row UID="PARENT_001_1221AD_A878" GroupID="" GroupRel="" ID="37" Name="Outer Asset Details" RelProduct="Line1" RelUID="CHILD2_201_5646546_9890PBS" RelName="CHILD1" RelType="Child" Size="22"/>
<z:row UID="PARENT_001_1221AD_A878" GroupID="" GroupRel="" ID="37" Name="Outer Asset Details" RelProduct="Line1" RelUID="SEC_999_99565_998AFSD" RelName="Hydraulic Section" RelType="Section" Size="22"/>
</rs:data>
Child.xml
<Child xsi:noNamespaceSchemaLocation="../XSD/Child.xsd" FILE="Child" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row UID="CHILD1_101_9899_9POOU99">
<Name>CHILD1</Name>
<Description>This has details about the Hydraulic sections of the automobile</Description>
</Row>
<Row UID="CHILD2_201_5646546_9890PBS">
<Name>CHILD2</Name>
<Description>This has details about the manual sections of the automobile</Description>
</Row>
Section.xml
<Section xsi:noNamespaceSchemaLocation="../XSD/Section.xsd" FILE="Section" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row UID="SEC_999_99565_998AFSD">
<Name>Hydraulic Section</Name>
<Description>This has details about the Sections in which the Hydraulic Systems are used.</Description>
</Row>
XSLT File
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" exclude-result-prefixes="s dt z rs msxsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="UniqueID" match="z:row/#ID" use="."/>
<xsl:template match="/">
<Parent xsi:noNamespaceSchemaLocation="../XSD/Parent.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" FILE="Parent">
<xsl:for-each select="xml">
<xsl:apply-templates select="rs:data"/>
</xsl:for-each>
</Parent>
</xsl:template>
<xsl:template match="rs:data">
<xsl:for-each select="z:row/#ID[generate-id() = generate-id(key('UniqueID',.))]">
<xsl:variable name="FRId">
<xsl:value-of select="current()"/>
</xsl:variable>
<xsl:variable name="curNSet" select="//z:row[#ID=$FRId]"/>
<xsl:copy-of select="current()"/>
<Record>
<xsl:attribute name="UID"><xsl:value-of select="$curNSet/#UID"/></xsl:attribute>
<xsl:element name="Size">
<xsl:value-of select="$curNSet/#Size"/>
</xsl:element>
<xsl:element name="Child">
<xsl:apply-templates select="$curNSet[#RelType='Child']" mode="Relations">
<xsl:with-param name="RelType" select="'Child'"/>
<xsl:with-param name="DstFileName" select="'../Files/Child.xml'"/>
</xsl:apply-templates>
</xsl:element>
<xsl:element name="Section">
<xsl:apply-templates select="$curNSet[#RelType='Section']" mode="References">
<xsl:with-param name="RelType" select="'Section'"/>
<xsl:with-param name="DstFileName" select="'../Files/Section.xml'"/>
</xsl:apply-templates>
</xsl:element>
</Record>
</xsl:for-each>
</xsl:template>
<xsl:template match="z:row" mode="Relations">
<xsl:param name="RelType"/>
<xsl:param name="DstFileName"/>
<xsl:element name="{$RelType}">
<xsl:attribute name="DestinationKey"><xsl:value-of select="#RelUID"/></xsl:attribute>
<xsl:attribute name="RelFilePath"><xsl:value-of select="$DstFileName"/></xsl:attribute>
<xsl:attribute name="SequenceNumber"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:value-of select="#RelName"/>
</xsl:element>
</xsl:template>
<xsl:template match="z:row" mode="References">
<xsl:param name="DstFileName"/>
<xsl:attribute name="DestinationKey"><xsl:value-of select="#RelUID"/></xsl:attribute>
<xsl:attribute name="RelFilePath"><xsl:value-of select="$DstFileName"/></xsl:attribute>
<xsl:attribute name="SequenceNumber"><xsl:value-of select="position()"/></xsl:attribute>
<xsl:value-of select="#RelName"/>
</xsl:template>
Output.xml
<Parent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../XSD/Parent.xsd" FILE="Parent" ID="37">
<Record UID="PARENT_001_1221AD_A878">
<Size>22</Size>
<Child>
<Child DestinationKey="CHILD1_101_9899_9POOU99" RelFilePath="../Files/Child.xml" SequenceNumber="1">CHILD1</Child>
<Child DestinationKey="CHILD2_201_5646546_9890PBS" RelFilePath="../Files/Child.xml" SequenceNumber="2">CHILD1</Child>
</Child>
<Section DestinationKey="SEC_999_99565_998AFSD" RelFilePath="../Files/Section.xml" SequenceNumber="1">Hydraulic Section</Section>
</Record>
Please help me in optimizing the XSLT, so that the output file is generated faster
Consider to use
<xsl:key name="UniqueID" match="z:row" use="#ID"/>
then
<xsl:for-each select="z:row/#ID[generate-id() = generate-id(key('UniqueID',.))]">
<xsl:variable name="FRId">
<xsl:value-of select="current()"/>
</xsl:variable>
<xsl:variable name="curNSet" select="//z:row[#ID=$FRId]"/>
can be replaced with
<xsl:for-each select="z:row[generate-id() = generate-id(key('UniqueID', #ID))]">
<xsl:variable name="FRId" select="#ID"/>
<xsl:variable name="curNSet" select="key('UniqueID', #ID"/>
I am not sure you need the variable FRId at all but defining it with a select attribute instead of a nested value-of is certainly consuming less resources.
To make
<xsl:apply-templates select="$curNSet[#RelType='Child']" mode="Relations">
more efficient define a key
<xsl:key name="rel" match="z:row" use="concat(#ID, '|', #RelType)"/>
then use
<xsl:apply-templates select="key('rel', concat(#ID, '|', 'Child')" mode="Relations">
Then use the same approach for the other apply-templates.
All of the above is untested but should give you an idea.

XSLT: Replace string with Abbreviations

I would like to know how to replace the string with the abbreviations.
My XML looks like below
<concept reltype="CONTAINS" name="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" type="NUM">
<code meaning="Left Ventricular Major Axis Diastolic Dimension, 4-chamber view" value="18074-5" schema="LN" />
<measurement value="5.7585187646">
<code value="cm" schema="UCUM" />
</measurement>
<content>
<concept reltype="HAS ACQ CONTEXT" name="Image Mode" type="CODE">
<code meaning="Image Mode" value="G-0373" schema="SRT" />
<code meaning="2D mode" value="G-03A2" schema="SRT" />
</concept>
</content>
</concept>
and I am selecting some value from the xml like,
<xsl:value-of select="concept/measurement/code/#value"/>
Now what I want is, I have to replace cm with centimeter. I have so many words like this. I would like to have a xml for abbreviations and replace from them.
I saw one similar example here.
Using a Map in XSL for expanding abbreviations
But it replaces node text, but I have text as attribute. Also, it would be better for me If I can find and replace when I select text using xsl:valueof select instead of having a separate xsl:template. Please help. I am new to xslt.
I have created XSLT v "1.1". For abbreviations I have created XML file as you have mentioned:
Abbreviation.xml:
<Abbreviations>
<Abbreviation>
<Short>cm</Short>
<Full>centimeter</Full>
</Abbreviation>
<Abbreviation>
<Short>m</Short>
<Full>meter</Full>
</Abbreviation>
</Abbreviations>
XSLT:
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" />
<xsl:param name="AbbreviationDoc" select="document('Abbreviation.xml')"/>
<xsl:template match="/">
<xsl:call-template name="Convert">
<xsl:with-param name="present" select="concept/measurement/code/#value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Convert">
<xsl:param name="present"/>
<xsl:choose>
<xsl:when test="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]">
<xsl:value-of select="$AbbreviationDoc/Abbreviations/Abbreviation[Short = $present]/Full"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$present"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
INPUT:
as you have given <xsl:value-of select="concept/measurement/code/#value"/>
OUTPUT:
centimeter
You just need to enhance this Abbreviation.xml to keep short and full value of abbreviation and call 'Convert' template with passing current value to get desired output.
Here a little shorter version:
- with abbreviations in xslt file
- make use of apply-templates with mode to make usage shorter.
But with xslt 1.0 node-set extension is required.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="abbreviations_txt">
<abbreviation abbrev="cm" >centimeter</abbreviation>
<abbreviation abbrev="m" >meter</abbreviation>
</xsl:variable>
<xsl:variable name="abbreviations" select="exsl:node-set($abbreviations_txt)" />
<xsl:template match="/">
<xsl:apply-templates select="concept/measurement/code/#value" mode="abbrev_to_text"/>
</xsl:template>
<xsl:template match="* | #*" mode="abbrev_to_text">
<xsl:variable name="abbrev" select="." />
<xsl:variable name="long_text" select="$abbreviations//abbreviation[#abbrev = $abbrev]/text()" />
<xsl:value-of select="$long_text"/>
<xsl:if test="not ($long_text)">
<xsl:value-of select="$abbrev"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>