XSLT 3.0中如何提取Map里Key对应的指定属性值?
XSLT 3.0 提取Map中指定Key对应属性值的问题
我是XSLT新手,正在XSLT 3.0中尝试打印Map内一个Key的指定值,目前代码会输出Key对应的所有值,但我希望仅打印邮箱,或同时打印邮箱与手机号,请求帮助。
XML输入
<AggregatedData> <wd:Report_Data xmlns:wd="urn:com.workday.report/myreport"> <wd:Report_Entry> <wd:Worker_ID>001</wd:Worker_ID> <wd:Email>test1@email.ccm</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>test2@email.ccm</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> </wd:Report_Data> </AggregatedData>
现有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>
当前输出
test1@email.ccm TQSWD testDomain1 123456 987456 test2@email.ccm RGTH testDomain2 578963 5478253
预期输出
预期输出1(仅邮箱)
test1@email.ccm test2@email.ccm
预期输出2(邮箱+手机号)
test1@email.ccm 987456 test2@email.ccm 5478253
解决方案
先修正代码中的标签错误
原代码中<R_email>的闭合标签写成了</r_email>(大小写不匹配),会导致XML结构错误,先统一修正为<R_email>...</R_email>。
实现预期输出1(仅提取邮箱)
修改遍历map的代码,在获取map值后筛选<R_email>元素,并添加换行符:
<xsl:for-each select="map:keys($reportMap)"> <xsl:value-of select="map:get($reportMap, .)/R_email"/> <xsl:text> </xsl:text> <!-- 添加换行 --> </xsl:for-each>
实现预期输出2(提取邮箱+手机号)
同时筛选<R_email>和<R_Mobile>元素,用空格分隔后添加换行:
<xsl:for-each select="map:keys($reportMap)"> <xsl:value-of select="map:get($reportMap, .)/(R_email, R_Mobile)" separator=" "/> <xsl:text> </xsl:text> <!-- 添加换行 --> </xsl:for-each>
完整修正后的XSLT(以预期输出2为例)
<?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> <xsl:for-each select="map:keys($reportMap)"> <xsl:value-of select="map:get($reportMap, .)/(R_email, R_Mobile)" separator=" "/> <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
内容的提问来源于stack exchange,提问作者manoj
相关产品推荐
相关产品推荐

