I am new to XSLT and I am trying to simply print one of the values of a key in XSLT3.0. Please help me with this. My XML:
<AggregatedData>
<wd:Report_Data xmlns:wd="urn:com.workday.report/myreport">
<wd:Report_Entry>
<wd:Worker_ID>001</wd:Worker_ID>
<wd:Email>[email protected]</wd:Email>
<wd:ADlogon>TQSWD</wd:ADlogon>
<wd:Domain>testDomain1</wd:Domain>
<wd:Telephone>123456</wd:Telephone>
<wd:Mobile>987456</wd:Mobile>
</wd:Report_Entry>
<wd:Report_Entry>
<wd:Worker_ID>002</wd:Worker_ID>
<wd:Email>[email protected]</wd:Email>
<wd:ADlogon>RGTH</wd:ADlogon>
<wd:Domain>testDomain2</wd:Domain>
<wd:Telephone>578963</wd:Telephone>
<wd:Mobile>5478253</wd:Mobile>
</wd:Report_Entry>
Here is my XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:wd="urn:com.workday.report/myreport"
xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
xmlns:tube="java:com.capeclear.mediation.impl.cc.MediationTube"
xmlns:ctx="java:com.capeclear.mediation.MediationContext"
xmlns:functx="http://www.functx.com" exclude-result-prefixes="xs map wd functx is" version="3.0">
<xsl:output method="text" indent="no"/>
<xsl:template match="AggregatedData">
<xsl:variable name="reportMap" as="map(xs:string, element()*)">
<!-- Build a map from Report_Entry -->
<xsl:map>
<xsl:for-each select="wd:Report_Data/wd:Report_Entry">
<xsl:map-entry key="string(wd:Worker_ID)" >
<xsl:sequence>
<R_email><xsl:value-of select="wd:Email"/></r_email>
<R_ADlogon><xsl:value-of select="wd:ADlogon"/></R_ADlogon>
<R_Domain><xsl:value-of select="wd:Domain"/></R_Domain>
<R_Telephone><xsl:value-of select="wd:Telephone"/></R_Telephone>
<R_Mobile><xsl:value-of select="wd:Mobile"/></R_Mobile>
</xsl:sequence>
</xsl:map-entry>
</xsl:for-each>
</xsl:map>
</xsl:variable>
<output>
<xsl:for-each select="map:keys($reportMap)">
<xsl:value-of select="map:get($reportMap, .)"/>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Current Output, this prints all the values of a key.
[email protected] TQSWD testDomain1 123456 987456
[email protected] RGTH testDomain2 578963 5478253
But I want to print only the email for key or print both email and mobile. How do I achieve it?
Expected Output1:
[email protected]
[email protected]
Expected Output2:
[email protected] 987456
[email protected] 5478253
map:get($reportMap, .)[1].map:get($reportMap, .)[self::R_email]would also work. Of course as you construct the map value, if you want to select by element name you might want to have a different map type and value, maps are often chosen as they can be more lightweight than XML trees/nodes so you could make a map holding a map and then select withmap:get($reportMap, .)?email