我需要使用 xsl 3.0 或 xsl 2.0將以下 xml 轉換為預期輸出。需要通過結合其legacyID和company<key> </key>添加為每個元素呼叫的新元素。<row>
輸入 XML:
<AggregatedData>
<Data>
<Entry>
<legacyID>ABC</legacyID>
<AssociateID>123</AssociateID>
</Entry>
<Entry>
<legacyID>CDE</legacyID>
<AssociateID>456</AssociateID>
</Entry>
</Data>
<root>
<row>
<legacyID>ABC</legacyID>
<company>Test Company 1</company>
<firstname>Test1</firstname>
</row>
<row>
<legacyID>CDE</legacyID>
<company>Test Company 2</company>
<firstname>Test2</firstname>
</row>
</root>
</AggregatedData>
預期輸出:
<AggregatedData>
<Data>
<Entry>
<legacyID>ABC<legacyID>
<AssociateID>123<AssociateID>
<Entry>
<Entry>
<legacyID>CDE</legacyID>
<AssociateID>456</AssociateID>
</Entry>
</Data>
<root>
<row>
<key>ABC_Test Company 1</key>
<legacyID>ABC</legacyID>
<company>Test Company 1</company>
<firstname>Test1</firstname>
</row>
<row>
<key>ABC_Test Company 2</key>
<legacyID>CDE</legacyID>
<company>Test Company 2</company>
<firstname>Test2</firstname>
</row>
</root>
</AggregatedData>
我使用了以下 xsl 代碼,
<?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" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="root/*">
<xsl:copy>
<xsl:element name="key">
<xsl:value-of select="concat(//root/row/legacyID,' ',/row/company)" />
</xsl:element>
<xsl:call-template name="copy-children" />
</xsl:copy>
</xsl:template>
<!-- Copy the children of the current node. -->
<xsl:template name="copy-children">
<xsl:copy-of select="./*" />
</xsl:template>
<!-- Generic identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但這給出了如下所示的輸出,
<?xml version="1.0" encoding="UTF-8"?>
<AggregatedData>
<Data>
<Entry>
<legacyID>ABC</legacyID>
<AssociateID>123</AssociateID>
</Entry>
<Entry>
<legacyID>CDE</legacyID>
<AssociateID>456</AssociateID>
</Entry>
</Data>
<root>
<row>
<key>ABC_Test Company 1</key>
<legacyID>ABC</legacyID>
<company>Test Company 1</company>
<firstname>Test1</firstname>
</row>
<row>
<key>ABC_Test Company 1</key>
<legacyID>CDE</legacyID>
<company>Test Company 2</company>
<firstname>Test2</firstname>
</row>
</root>
</AggregatedData>
根據上面的輸出,兩個行值都用相同的鍵值更新。我需要糾正它并需要使用 xsl 2.0。
uj5u.com熱心網友回復:
你可以這樣做:
<?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"/>
<xsl:template match="row">
<xsl:copy>
<key>
<xsl:value-of select="concat(legacyID,'_',company)"/>
</key>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
看到它在這里作業:https ://xsltfiddle.liberty-development.net/
uj5u.com熱心網友回復:
在XSLT 3.0中,您可以將代碼簡化為:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="row/legacyID">
<key>
<xsl:value-of select="., ../company" separator="_"/>
</key>
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477374.html
