根据子字符串

时间:2019-01-11 22:53:11

标签: xml xslt xslt-2.0

我试图根据子字符串match来整理节点。以下是我要处理的XML:-

<?xml version="1.0" encoding="utf-8"?>
<CurrencyPairs xmlns:wd="urn:com.workday/bsvc">
    <ActiveCurrenciesNumber>5</ActiveCurrenciesNumber>
    <CurrencyPair>AEDAFN</CurrencyPair>
    <CurrencyPair>AEDCHE</CurrencyPair>
    <CurrencyPair>AEDEUR</CurrencyPair>
    <CurrencyPair>AEDUSD</CurrencyPair>
    <CurrencyPair>AFNAED</CurrencyPair>
    <CurrencyPair>AFNCHE</CurrencyPair>
    <CurrencyPair>AFNEUR</CurrencyPair>
    <CurrencyPair>AFNUSD</CurrencyPair>
    <CurrencyPair>CHEAED</CurrencyPair>
    <CurrencyPair>CHEAFN</CurrencyPair>
    <CurrencyPair>CHEEUR</CurrencyPair>
    <CurrencyPair>CHEUSD</CurrencyPair>
    <CurrencyPair>EURAED</CurrencyPair>
    <CurrencyPair>EURAFN</CurrencyPair>
    <CurrencyPair>EURCHE</CurrencyPair>
    <CurrencyPair>EURUSD</CurrencyPair>
    <CurrencyPair>USDAED</CurrencyPair>
    <CurrencyPair>USDAFN</CurrencyPair>
    <CurrencyPair>USDCHE</CurrencyPair>
    <CurrencyPair>USDEUR</CurrencyPair>
    <PairsNumber>20</PairsNumber>
</CurrencyPairs>

我已经尝试了多种方法,但是基于子字符串匹配,我无法获得所需的输出。
所需的输出应如下所示:-

<?xml version="1.0" encoding="utf-8"?>
<CurrencyPairs xmlns:wd="urn:com.workday/bsvc">
    <ActiveCurrenciesNumber>5</ActiveCurrenciesNumber>
    <CurrencyPair>AEDAFN,AEDCHE,AEDEUR,AEDUSD</CurrencyPair>
    <CurrencyPair>AFNAED,AFNCHE,AFNEUR,AFNUSD</CurrencyPair>
    <CurrencyPair>CHEAED,CHEAFN,CHEEUR,CHEUSD</CurrencyPair>
    <CurrencyPair>EURAED,EURAFN,EURCHE,EURUSD</CurrencyPair>
    <CurrencyPair>USDAED,USDAFN,USDCHE,USDEUR</CurrencyPair>
    </CurrencyPairs>

我尝试了使用分组等多种方法,但是所有的努力都徒劳无功。如果有人可以分享这种情况的示例,那就太好了。
任何人都可以协助获得所需的输出吗?

1 个答案:

答案 0 :(得分:2)

不确定您尝试了什么-看起来很简单,甚至微不足道:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/CurrencyPairs">
    <xsl:copy>
        <xsl:copy-of select="ActiveCurrenciesNumber"/>
        <xsl:for-each-group select="CurrencyPair" group-by="substring(., 1, 3)">
            <CurrencyPair> 
                <xsl:value-of select="current-group()" separator=","/>
            </CurrencyPair>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>