Rich Text content in PDF using XSL FO and Oracle APEX - oracle-apex

I am using Oracle APEX(22.1) for web Applications and APACHE-FOP(2.6) print server to generate PDF.
We have a functionality in Oracle APEX. Which is Users can create Templates using Rich Text Editor page item in Oracle APEX. Below is an example.
We are storing that template in "Templates" table.
and Content is stored like below in Database.
I am working on preparing XSL-FO layout to display the content in PDF.
I created a Report Query with "Select content from templates"
and Below is Data XML for the report Query.
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<ROWSET>
<ROW>
<NAME>TEST</NAME>
<DESCRIPTION><p><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Program Details:</strong></p>
<p><strong>Reimbursement for ABC Users:</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1-2 Students <em><strong>$300</strong></em>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3-5 Students <em><strong>$500</strong></em></p>
<p><strong>Hotels for ABC Users:</strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1-2 Students <em><strong>$100</strong></em></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3-5 Students <em><strong>$300</strong></em></p>
<p>&nbsp;</p>
</DESCRIPTION>
</ROW>
</ROWSET>
</DOCUMENT>
Below is XSL-FO layout code. which is used in report Layouts.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" version="2.0">
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-width="216mm" page-height="279mm" margin-top="1cm" margin-bottom="1cm" margin-left="1cm" margin-right="1cm">
<fo:region-body margin="0.0cm" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<xsl:if test="DOCUMENT/REGION/ROWSET/ROW | /DOCUMENT/ROWSET/ROW | /ROWSET/ROW">
<xsl:for-each select="DOCUMENT/REGION/ROWSET/ROW | /DOCUMENT/ROWSET/ROW | /ROWSET/ROW">
<fo:block>
<xsl:value-of select="DESCRIPTION" />
</fo:block>
</xsl:for-each>
</xsl:if>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
Below is the OUTPUT.
But this output is wrong. it is supposed to be displayed as User's entered in Rich text item. Please help me how to achieve this Using Oracle APEX and XSL-FO.

Related

XSLT3 Streaming for appending integer position of node

I have a large XML file to transform using XSLT to append the integer position of sibling node . I’m using XSLT3 streaming and accumulators. I did get desired output. However, my code looks so lengthy that I’m unable to simplify my code. I also need to group same sibling nodes as sibling nodes in the source xml is not grouped always. Could someone help me here please?
Requirement: Sibling nodes such as Positions, Payments etc.. need to be appended with their corresponding integer position such as <Locations1>, <Locations2>etc.<Payments1>,< Payments2> etc..
Now that I have declared two accumulators, each for each sibling nodes. However, my source XML has many sibling nodes.. I’m not sure if I need to use as many accumulators and template match as my sibling nodes.
Input XML
``
<?xml version="1.0" encoding="UTF-8"?>
<Members>
<Member>
<Name>
<fname>Fred</fname>
<id>1234</id>
</Name>
<Locations>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations>
<Locations>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations>
<Payments>
<amount>1000</amount>
<currency>USD</currency>
</Payments>
<Payments>
<amount>1000</amount>
<currency>USD</currency>
</Payments>
<Locations>
<name>New York</name>
<days>5</days>
<hours>40</hours>
</Locations>
<Locations>
<name>Boston</name>
<days>4</days>
<hours>32</hours>
</Locations>
</Member>
<Member>
<Name>
<fname>Jack</fname>
<id>4567</id>
</Name>
<Locations>
<name>New York</name>
<days>5</days>
<hours>30</hours>
</Locations>
<Locations>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations>
<Payments>
<amount>1500</amount>
<currency>USD</currency>
</Payments>
<Payments>
<amount>1800</amount>
<currency>USD</currency>
</Payments>
</Member>
</Members>
``
Expected Output
``
<?xml version="1.0" encoding="UTF-8"?>
<Members>
<Member>
<Name>
<fname>Fred</fname>
<id>1234</id>
</Name>
<Locations_1>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_1>
<Locations_2>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_2>
<Locations_3>
<name>New York</name>
<days>5</days>
<hours>40</hours>
</Locations_3>
<Locations_4>
<name>Boston</name>
<days>4</days>
<hours>32</hours>
</Locations_4>
<Payments_1>
<amount>1000</amount>
<currency>USD</currency>
</Payments_1>
<Payments_2>
<amount>1000</amount>
<currency>USD</currency>
</Payments_2>
</Member>
<Member>
<Name>
<fname>Jack</fname>
<id>4567</id>
</Name>
<Locations_1>
<name>New York</name>
<days>5</days>
<hours>30</hours>
</Locations_1>
<Locations_2>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_2>
<Payments_1>
<amount>1500</amount>
<currency>USD</currency>
</Payments_1>
<Payments_2>
<amount>1800</amount>
<currency>USD</currency>
</Payments_2>
</Member>
</Members>
``
Current code
``
<?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="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:mode streamable="yes" on-no-match="shallow-copy" use-accumulators="#all"/>
<xsl:accumulator name="loc-count" as="xs:integer" initial-value="0" streamable="yes">
<xsl:accumulator-rule match="Member" select="0"/>
<xsl:accumulator-rule match="Member/Locations" select="$value + 1"/>
</xsl:accumulator>
<xsl:accumulator name="pay-count" as="xs:integer" initial-value="0" streamable="yes">
<xsl:accumulator-rule match="Member" select="0"/>
<xsl:accumulator-rule match="Member/Payments" select="$value + 1"/>
</xsl:accumulator>
<xsl:template match="Locations">
<xsl:element name="Locations_{accumulator-before('loc-count')}">
<xsl:copy-of select="#* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="Payments">
<xsl:element name="Payments_{accumulator-before('pay-count')}">
<xsl:copy-of select="#* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
``
Current Output
<?xml version="1.0" encoding="UTF-8"?>
<Members>
<Member>
<Name>
<fname>Fred</fname>
<id>1234</id>
</Name>
<Locations_1>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_1>
<Locations_2>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_2>
<Payments_1>
<amount>1000</amount>
<currency>USD</currency>
</Payments_1>
<Payments_2>
<amount>1000</amount>
<currency>USD</currency>
</Payments_2>
<Locations_3>
<name>New York</name>
<days>5</days>
<hours>40</hours>
</Locations_3>
<Locations_4>
<name>Boston</name>
<days>4</days>
<hours>32</hours>
</Locations_4>
</Member>
<Member>
<Name>
<fname>Jack</fname>
<id>4567</id>
</Name>
<Locations_1>
<name>New York</name>
<days>5</days>
<hours>30</hours>
</Locations_1>
<Locations_2>
<name>Chicago</name>
<days>3</days>
<hours>24</hours>
</Locations_2>
<Payments_1>
<amount>1500</amount>
<currency>USD</currency>
</Payments_1>
<Payments_2>
<amount>1800</amount>
<currency>USD</currency>
</Payments_2>
</Member>
</Members>
If you want to group the Member child elements by node-name() then I think you need to wrap the xsl:for-each-group into xsl:fork:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" version="3.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy" streamable="yes" use-accumulators="counters"/>
<xsl:accumulator name="counters" as="map(xs:QName, xs:integer)" initial-value="map{}" streamable="yes">
<xsl:accumulator-rule match="Member" select="map{}"/>
<xsl:accumulator-rule match="Member/*"
select="map:put($value, node-name(), if (map:contains($value, node-name())) then map:get($value, node-name()) + 1 else 1)"/>
</xsl:accumulator>
<xsl:template match="Member">
<xsl:copy>
<xsl:fork>
<xsl:for-each-group select="*" group-by="node-name()">
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</xsl:fork>
</xsl:copy>
</xsl:template>
<xsl:template match="Member/*">
<xsl:element name="{node-name()}_{accumulator-before('counters')(node-name())}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This approach only shows the grouping, it doesn't try to special case Name elements or some other way to not output an index if there is only one such element.
Firstly, my sympathy. XML that uses names like Payments_1 and Payments_2 is really bad news, someone is going to hate you for generating it like this. But if that's the kind of XML you've been told to produce, I guess it's not your job to question it.
As far as the requirements are concerned, you haven't made it clear whether the various kinds of sibling nodes are always grouped as in your example (all Locations, then all Payments, etc), or whether they can be interleaved.
One way you might be able to reduce the volume of code is by having a single accumulator holding a map. The map would use element names as the key and the current sibling count for that element as the value.
<accumulator name="counters" as="map(xs:QName, xs:integer)" initial-value="map{}">
<xsl:accumulator-rule match="Member" select="map{}"/>
<xsl:accumulator-rule match="Member/*" select="map:put($value, node-name(.), if (map:contains($value, node-name(.)) then map:get($value, node-name(.))+1 else 1"/>
</accumulator>
<xsl:template match="Members/*">
<xsl:element name="{name()}_{accumulator-before('counters')(node-name(.))}">
....
Another way to do the conditional map:put is
map:put($value, node-name(.), ($value(node-name(.)), 0)[1] + 1)

Problem with namespaces when generating kml dynamically

I am using a test-driven-development process to create some xslt to create kml data dynamically.
With xslt I use xspec to manage the tests.
And I am experiencing a default namespace problem.
The xsl stylesheet has a large number of namespace declarations
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xs xd kml gx atom"
version="1.0">
The xpec test file has a simpler declaration
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec"
Part of the xslt code copies swathes of kml style elements into the output stream, e.g.
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
This is defined in an element.
In the xspec test file I am specifying what kml to expect and I just copy and paste this code into the appropriate element.
But the test fails as what is generated contains an empty xmlns="" namespace declaration, e.g.
XSPEC file
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="KMLLibraryTest.xsl">
<x:scenario label="just a test">
<x:call template="make-ghost-namespace"/>
<x:expect label="Test output">
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Las Salinetas Port Limits</name>
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Placemark id="{generate-id()}">
<Snippet> </Snippet>
<name>Test</name>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>
</LineString>
</Placemark>
</Document>
</kml>
</x:expect>
</x:scenario>
</x:description>
XSL file
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xs xd kml gx atom"
version="1.0">
<xsl:variable name="test-style">
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
</xsl:variable>
<xd:doc scope="component">
<xd:desc>
<xd:p>Testing the creation of ghost empty namespace declarations</xd:p>
</xd:desc>
</xd:doc>
<xsl:template name="make-ghost-namespace">
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Las Salinetas Port Limits</name>
<xsl:copy-of select="$test-style"/>
<Placemark id="{generate-id()}">
<Snippet> </Snippet>
<name>Test</name>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>
</LineString>
</Placemark>
</Document>
</kml>
</xsl:template>
</xsl:stylesheet>
The single test fails as the actual output is
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Las Salinetas Port Limits</name>
<StyleMap xmlns="" id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Placemark id="d5">
<Snippet> </Snippet>
<name>Test</name>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<LineString>
<tessellate>1</tessellate>
<coordinates>-25.3633333,46.9916667,0 -25.3566667,46.9383333,0 </coordinates>
</LineString>
</Placemark>
</Document>
</kml>
So what do I need to do in order avoid those default namespaces being created during testing?
EXPANSION:
So the first answer does work, but if I add another template to the sylesheet
<xsl:template name="make-network-link">
<xsl:param name="name"/>
<xsl:param name="uri"/>
<xsl:param name="vis" select="0"/>
<xsl:param name="open" select="0"/>
<xsl:param name="refresh"/>
<NetworkLink>
<name><xsl:value-of select="$name"/></name>
<visibility><xsl:value-of select="$vis"/></visibility>
<open><xsl:value-of select="$open"/></open>
<xsl:if test="$refresh">
<refreshVisibility><xsl:value-of select="$refresh"/></refreshVisibility>
</xsl:if>
<Link>
<href><xsl:value-of select="$uri"/></href>
</Link>
</NetworkLink>
</xsl:template>
and change the declaration to include the default namespace as well as suppress #default as follows
<xsl:stylesheet xmlns="http://www.opengis.net/kml/2.2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="#default xs xd kml gx atom"
version="1.0">
and add another test to the xspec file
<x:scenario label="Create test Network link">
<x:call template="make-network-link">
<x:param name="name" select="'Test network link etc'"/>
<x:param name="uri" select="'http://kmlbalahblah.com/GE/TZFolder.kml'"/>
<x:param name="vis" select="'1'"/>
</x:call>
<x:expect label="Default Hardcoded links">
<NetworkLink>
<name>Test network link etc</name>
<visibility>1</visibility>
<open>0</open>
<Link>
<href>http://kmlbalahblah.com/GE/TZFolder.kml</href>
</Link>
</NetworkLink>
</x:expect>
</x:scenario>
The output produced still comes through with the default namespace, i.e.
<NetworkLink xmlns="http://www.opengis.net/kml/2.2">
<name>Test network link etc</name>
<visibility>1</visibility>
<open>0</open>
<Link>
<href>http://kmlbalahblah.com/GE/TZFolder.kml</href>
</Link>
</NetworkLink>
How do I suppress THAT namespace now?

BizTalk XSLT Mapping For Grouping and Sorting with Multiple Records

Input Message :
The Input File Has Three Records inside the Detail is in the order “Member” , “Product” and “Dependent” , In each Record there is a common Field which is “ Identifier” For Some reason we getting like Each Member and Product are looped into one detail and Each dependent is looping into separate Detail
................................................................................
<ns0:Root xmlns:ns0="Test">
<Detail>
<Member>
<Name>Jerry</Name>
<Address>Miami</Address>
<PhoneNumber>7008084201</PhoneNumber>
<Identifier>225692067</Identifier>
</Member>
<Product>
<Name>Phone</Name>
<Type>Personal</Type>
<Serial>000000111111</Serial>
<Identifier>225692067</Identifier>
</Product>
</Detail>
<Detail>
<Dependent>
<DependentName>Tom</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
</Detail>
<Detail>
<Dependent>
<DependentName>Tom1</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>8228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
</Detail>
<Detail>
<Dependent>
<DependentName>Tom2</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>9228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
</Detail>
<Detail>
<Member>
<Name>John</Name>
<Address>Kansas</Address>
<PhoneNumber>5007684306</PhoneNumber>
<Identifier>699039521</Identifier>
</Member>
<Product>
<Name>Xbox</Name>
<Type>Personal</Type>
<Serial>000000222222</Serial>
<Identifier>699039521</Identifier>
</Product>
</Detail>
<Detail>
<Member>
<Name>Larry</Name>
<Address>Newjersey</Address>
<PhoneNumber>6004567307</PhoneNumber>
<Identifier>230903815</Identifier>
</Member>
<Product>
<Name>Iphone</Name>
<Type>Personal</Type>
<Serial>0000003333333</Serial>
<Identifier>230903815</Identifier>
</Product>
</Detail>
<Detail>
<Dependent>
<DependentName>Luis</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7897684302</DependentPhoneNumber>
<Identifier>230903815</Identifier>
</Dependent>
</Detail>
<Detail>
<Dependent>
<DependentName>LuisMead</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7229876302</DependentPhoneNumber>
<Identifier>230903815</Identifier>
</Dependent>
</Detail>
</ns0:Root>
Expected OutPut XML :
The OutPut File is also Similar to the Input File but the order is “Member” , “Dependent” and “Product” . The Common Field “Identifier” is the common on in this case also.The idea is to make Detail to loop on “Member” , “Dependent” and “Product” order.
......................................................................................
<ns0:Root xmlns:ns0="Test">
<Detail>
<Member>
<Name>Jerry</Name>
<Address>Miami</Address>
<PhoneNumber>7008084201</PhoneNumber>
<Identifier>225692067</Identifier>
</Member>
<Dependent>
<DependentName>Tom</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
<Dependent>
<DependentName>Tom1</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>8228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
<Dependent>
<DependentName>Tom2</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>9228084302</DependentPhoneNumber>
<Identifier>225692067</Identifier>
</Dependent>
<Product>
<Name>Phone</Name>
<Type>Personal</Type>
<Serial>000000111111</Serial>
<Identifier>225692067</Identifier>
</Product>
</Detail>
<Detail>
<Member>
<Name>John</Name>
<Address>Kansas</Address>
<PhoneNumber>5007684306</PhoneNumber>
<Identifier>699039521</Identifier>
</Member>
<Product>
<Name>Xbox</Name>
<Type>Personal</Type>
<Serial>000000222222</Serial>
<Identifier>699039521</Identifier>
</Product>
</Detail>
<Detail>
<Member>
<Name>Larry</Name>
<Address>Newjersey</Address>
<PhoneNumber>6004567307</PhoneNumber>
<Identifier>230903815</Identifier>
</Member>
<Dependent>
<DependentName>Luis</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7897684302</DependentPhoneNumber>
<Identifier>230903815</Identifier>
</Dependent>
<Dependent>
<DependentName>LuisMead</DependentName>
<DependentAddress>Miami</DependentAddress>
<DependentPhoneNumber>7229876302</DependentPhoneNumber>
<Identifier>230903815</Identifier>
</Dependent>
<Product>
<Name>Iphone</Name>
<Type>Personal</Type>
<Serial>0000003333333</Serial>
<Identifier>230903815</Identifier>
</Product>
</Detail>
</ns0:Root>
Need Suggestion on writing XSLT 1.0 Code for this.
You can do this in XSLT-1.0 with Muenchian Grouping. Search for it on StackOverflow and you will find a lot of examples. Applying it created the following answer:
Create an xsl:key with the nodes Member|Dependent|Product using its Identifier element value as a key
Create a sortingOrder variable which provides indices for sorting the entries in the inner xsl:for-each
Match and copy the root node /ns0:Root with a template
Loop over all children of the Detail elements in a xsl:for-each. The expression applies the Muenchian Grouping method
Create a Detail elements and loop over the results of the previous xsl:for-each sorted by the index of the occurrence of the name of the current element in the sortingOrder variable. Copy its content. The method of ordering the elements was taken from this SO answer: "Sorting XML in XSLT based on a list of values".
The stylesheet could look like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="Test">
<xsl:output indent="yes"/>
<xsl:key name="id" match="Member|Dependent|Product" use="Identifier" />
<xsl:variable name="sortingOrder" select="'Member,Dependent,Product'" />
<xsl:template match="/ns0:Root">
<xsl:copy>
<xsl:for-each select="Detail/*[generate-id() = generate-id(key('id',Identifier)[1])]">
<Detail>
<xsl:for-each select="key('id',Identifier)">
<xsl:sort data-type="number" select="string-length(substring-before($sortingOrder,local-name()))" />
<xsl:copy-of select="."/>
</xsl:for-each>
</Detail>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The output should be as desired.

XSLT distinct value of the ancestor element

This is the input XML:
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<book>
<chapter label="Chapter 1">
<para>Text Text<link role="kwd" linkend="Gloss_1">Term 1</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 1</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 2">
<para>Text Text<link role="kwd" linkend="Gloss_3">Term 3</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_1">Term 1</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_1">Term 1</link> Def 3</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_3">Term 3</link> Def 1</para></listitem>
</itemizedlist>
</section>
</chapter>
<chapter label="Chapter 3">
<para>Text Text<link role="kwd" linkend="Gloss_4">Term 4</link> Text</para>
<para>Text Text Text<link role="kwd" linkend="Gloss_2">Term 2</link></para>
<para>Text Text Text<link role="kwd" linkend="Gloss_5">Term 5</link></para>
<section>
<title>Key Terms</title>
<itemizedlist mark="none">
<listitem><para><link role="kwd" linkend="Gloss_2">Term 2</link> Def 2</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_4">Term 4</link> Def 4</para></listitem>
<listitem><para><link role="kwd" linkend="Gloss_5">Term 5</link> Def 5</para></listitem>
</itemizedlist>
</section>
</chapter>
<glossary>
<glossentry xml:id="Gloss_1"><glossterm>Term 1</glossterm><glossdef>Def 1</glossdef></glossentry>
<glossentry xml:id="Gloss_2"><glossterm>Term 2</glossterm><glossdef>Def 2</glossdef></glossentry>
<glossentry xml:id="Gloss_3"><glossterm>Term 3</glossterm><glossdef>Def 3</glossdef></glossentry>
<glossentry xml:id="Gloss_4"><glossterm>Term 4</glossterm><glossdef>Def 4</glossdef></glossentry>
<glossentry xml:id="Gloss_5"><glossterm>Term 5</glossterm><glossdef>Def 5</glossdef></glossentry>
</glossary>
</book>
Output would be:
<?xml version="1.0" encoding="utf-8"?>
<glossary>
<row>
<col1>Chapter 1</col1>
<col1>Chapter 2</col1>
<col2>Term 1</col2>
</row>
<row>
<col1>Chapter 1</col1>
<col1>Chapter 3</col1>
<col2>Term 2</col2>
</row>
<row>
<col1>Chapter 2</col1>
<col2>Term 3</col2>
</row>
<row>
<col1>Chapter 3</col1>
<col2>Term 4</col2>
</row>
<row>
<col1>Chapter 3</col1>
<col2>Term 5</col2>
</row>
</glossary>
My code is:
<xsl:result-document href="out.xml">
<glossary>
<xsl:for-each select="book/glossary/glossentry">
<row>
<xsl:for-each select="key('num', #xml:id)">
<col1>
<xsl:value-of select="ancestor::chapter/#label"/>
</col1>
</xsl:for-each>
<col2><xsl:value-of select="glossterm"/></col2>
</row>
</xsl:for-each>
</glossary>
</xsl:result-document>
The glossary items are listed twice in the book - One at the end of each chapter and a consolidated items at the end. I would like to get the chapter number(s) for each glossary term listed at the end of the book. I tried various things but I am unable to get distinct value of the ancestor element. Can someone please help?
I think you could simply reverse your point-of-view:
<xsl:key name="chapter-by-link" match="chapter" use="descendant::link/#linkend" />
<xsl:template match="/">
<!-- other stuff -->
<glossary>
<xsl:for-each select="book/glossary/glossentry">
<row>
<xsl:for-each select="key('chapter-by-link', #xml:id)">
<col1>
<xsl:value-of select="#label"/>
</col1>
</xsl:for-each>
<col2>
<xsl:value-of select="glossterm"/>
</col2>
</row>
</xsl:for-each>
</glossary>
</xsl:template>
I suppose it suffices to select the ancestor::chapter/#label attributes as duplicates would be eliminated with any step selecting nodes so you would just change
<xsl:for-each select="key('num', #xml:id)">
<col1>
<xsl:value-of select="ancestor::chapter/#label"/>
</col1>
</xsl:for-each>
to
<xsl:for-each select="key('num', #xml:id)/ancestor::chapter/#label">
<col1>
<xsl:value-of select="."/>
</col1>
</xsl:for-each>
or perhaps organize the code into small templates to have cleaner code:
<?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="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:key name="ref" match="link[#role = 'kwd']" use="#linkend"/>
<xsl:template match="book">
<glossary>
<xsl:apply-templates select="glossary/glossentry"/>
</glossary>
</xsl:template>
<xsl:template match="glossentry">
<row>
<xsl:apply-templates select="key('ref', #xml:id)/ancestor::chapter/#label"/>
<col2>
<xsl:value-of select="glossterm"/>
</col2>
</row>
</xsl:template>
<xsl:template match="chapter/#label">
<col1>
<xsl:value-of select="."/>
</col1>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jyH9rNv

Trying to understand XSLT Muenchian Method transformation

I came across the Muenchian Method when looking for a way of grouping elements in an XML file that has been produced by converted a CSV file.
Source
<file>
<patient>
<Lab_Specimen_Number>L,18.1342718.Y</Lab_Specimen_Number>
<Patient_Number>LOC0000015</Patient_Number>
<ORG/>
<Specimen/>
<Antibiotic_Amox_Ampicillin/>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1342727.V</Lab_Specimen_Number>
<Patient_Number>LOC0000001</Patient_Number>
<ORG>Coliform</ORG>
<Specimen>L,18.1342727.VA</Specimen>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</patient>
<patient>
<Lab_Specimen_Number/>
<Patient_Number/>
<ORG>Staphylococcus aureus</ORG>
<Specimen>L,18.1342727.VA</Specimen>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1346290.T</Lab_Specimen_Number>
<Patient_Number>LOC0000001</Patient_Number>
<ORG>Coliform</ORG>
<Specimen>L,18.1346290.TA</Specimen>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1342713.X</Lab_Specimen_Number>
<Patient_Number>LOC0000009</Patient_Number>
<ORG/>
<Specimen/>
<Antibiotic_Amox_Ampicillin/>
</patient>
</file>
Based on the article I have changed the match when assigning a key to patient[Specimen != ''] instead of just patient as it is possible for the Specimen value to be blank and these would be missing from the final output if just patient was used.
Transformation
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="patients-by-specimen" match="patient[Specimen != '']" use="Specimen" />
<xsl:template match="file">
<file>
<xsl:for-each select="patient[count(. | key('patients-by-specimen', Specimen)[1]) = 1]">
<patient>
<xsl:copy-of select="Lab_Specimen_Number" />
<xsl:copy-of select="Patient_Number" />
<Specimen>
<xsl:copy-of select="Specimen" />
<Organisms>
<xsl:for-each select="key('patients-by-specimen', Specimen)">
<Organism>
<xsl:copy-of select="ORG"/>
<xsl:copy-of select="Antibiotic_Amox_Ampicillin"/>
</Organism>
</xsl:for-each>
</Organisms>
</Specimen>
</patient>
</xsl:for-each>
</file>
</xsl:template>
</xsl:stylesheet>
Whilst the transformation above gives me the desired output I don't fully understand how this line is working:
<xsl:for-each select="patient[count(. | key('patients-by-specimen', Specimen)[1]) = 1]">
Could someone explain this iteration in the context of my source file?
Output
<file>
<patient>
<Lab_Specimen_Number>L,18.1342718.Y</Lab_Specimen_Number>
<Patient_Number>LOC0000015</Patient_Number>
<Specimen>
<Specimen/>
<Organisms/>
</Specimen>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1342727.V</Lab_Specimen_Number>
<Patient_Number>LOC0000001</Patient_Number>
<Specimen>
<Specimen>L,18.1342727.VA</Specimen>
<Organisms>
<Organism>
<ORG>Coliform</ORG>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</Organism>
<Organism>
<ORG>Staphylococcus aureus</ORG>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</Organism>
</Organisms>
</Specimen>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1346290.T</Lab_Specimen_Number>
<Patient_Number>LOC0000001</Patient_Number>
<Specimen>
<Specimen>L,18.1346290.TA</Specimen>
<Organisms>
<Organism>
<ORG>Coliform</ORG>
<Antibiotic_Amox_Ampicillin>S</Antibiotic_Amox_Ampicillin>
</Organism>
</Organisms>
</Specimen>
</patient>
<patient>
<Lab_Specimen_Number>L,18.1342713.X</Lab_Specimen_Number>
<Patient_Number>LOC0000009</Patient_Number>
<Specimen>
<Specimen/>
<Organisms/>
</Specimen>
</patient>
</file>