我需要使用 xsl 3.0 累加器將以下 xml 轉換為預期輸出。
輸入 XML:
<AggregatedData>
<Data>
<Entry>
<legacyID>ABC</legacyID>
<legacyLocation>Test_Loc1,Test_Loc2</legacyLocation>
<AssociateID>123</AssociateID>
</Entry>
<Entry>
<legacyID>ABC</legacyID>
<legacyLocation>Test_Loc3</legacyLocation>
<AssociateID>123</AssociateID>
</Entry>
<Entry>
<legacyID>CDE</legacyID>
<legacyLocation>Test_Loc4,Test_Loc5</legacyLocation>
<AssociateID>456</AssociateID>
</Entry>
</Data>
<root>
<row>
<legacyID>ABC</legacyID>
<legacyLocation>Test_Loc1</legacyLocation>
<company>Test Company 1</company>
<firstname>Test1</firstname>
</row>
<row>
<legacyID>CDE</legacyID>
<legacyLocation>Test_Loc5</legacyLocation>
<company>Test Company 2</company>
<firstname>Test2</firstname>
</row>
</root>
</AggregatedData>
下的值<Data>可以包含逗號分隔的值,<legacyLocation>而下的值<root>僅包含一個值<legacyLocation>。我需要映射這些值并將輸出作為下面的預期輸出。有沒有辦法使用legacyID和legacyLocation使用 XSLT 3.0 累加器來映射值?
預期輸出:
<root>
<worker>
<row>
<AssociateID>123</AssociateID>
<legacyID>ABC</legacyID>
<legacyLocation>Test_Loc1</legacyLocation>
<company>Test Company 1</company>
<firstname>Test1</firstname>
</row>
<row>
<AssociateID>456</AssociateID>
<legacyID>CDE</legacyID>
<legacyLocation>Test_Loc5</legacyLocation>
<company>Test Company 2</company>
<firstname>Test2</firstname>
</row>
</worker>
</root>
uj5u.com熱心網友回復:
我想你可以使用像
<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:key name="assoc-id" match="Entry" use="(legacyLocation => tokenize(',') => sort()) ! (current()/legacyID || ',' || .)"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/AggregatedData">
<xsl:apply-templates select="root"/>
</xsl:template>
<xsl:template match="row">
<xsl:copy>
<xsl:apply-templates select="key('assoc-id', (legacyID || ',' || legacyLocation))/AssociateID, node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我不明白為什么累加器會表現得更好,如果您需要與流交叉參考,我主要會嘗試使用它。
累加器的非流式使用將是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
expand-text="yes"
version="3.0">
<xsl:accumulator name="assoc-id" as="map(xs:string, xs:string)" initial-value="map{}">
<xsl:accumulator-rule
match="Entry"
select="let $entry := .
return fold-left(legacyLocation => tokenize(',') => sort(), $value, function($a, $k) { map:put($a, $entry/legacyID || ',' || $k, $entry/AssociateID/string()) })"/>
</xsl:accumulator>
<xsl:mode on-no-match="shallow-copy" use-accumulators="assoc-id"/>
<xsl:template match="/AggregatedData">
<xsl:apply-templates select="root"/>
</xsl:template>
<xsl:template match="row">
<xsl:copy>
<AssociateID>{accumulator-before('assoc-id')(legacyID || ',' || legacyLocation)}</AssociateID>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489237.html
