我想拆分这些元素:
<?xml version="1.0" encoding="UTF-8"?>
<CTP>
<name>ABSA bank</name>
<BAs>
<BA>bank</BA>
<BA>sector</BA>
<BA>issuer</BA>
</BAs>
<altIDs>
<altID>
<label.altSystem>ABSB|BBG</label.altSystem>
<label.altSystem>ASB|Reuters</label.altSystem>
</altID>
</altIDs>
</CTP>
采用以下格式:
<?xml version="1.0" encoding="UTF-8"?>
<CTP>
<name>ABSA bank</name>
<BAs>
<BA>bank</BA>
<BA>sector</BA>
<BA>issuer</BA>
</BAs>
<altIDs>
<aldID>
<label>ABSB</label>
<altSystem>BBG</altSystem>
</aldID>
<aldID>
<label>ASB</label>
<altSystem>Reuters</altSystem>
</aldID>
</altIDs>
</CTP>
我是Xslt的新手,我试图解决这个问题,我们怎么能在XSLT 1.0中做到这一点?
突出显示已转换的部分:
原始
<altID>
<label.altSystem>ABSB|BBG</label.altSystem>
<label.altSystem>ASB|Reuters</label.altSystem>
</altID>
转化
<aldID>
<label>ABSB</label>
<altSystem>BBG</altSystem>
</aldID>
<aldID>
<label>ASB</label>
<altSystem>Reuters</altSystem>
</aldID>
答案 0 :(得分:0)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[contains(name(), '_')]" name="nest">
<xsl:param name="name" select="name()" />
<xsl:param name="delimiter" select="'_'" />
<xsl:param name="text" select="." />
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:choose>
<xsl:when test="contains($name, $delimiter)">
<xsl:element name="{substring-before($name, $delimiter)}">
<xsl:value-of select="$token" />
</xsl:element>
<xsl:element name="{substring-after($name, $delimiter)}">
<xsl:variable name="token" select="substring-after($text, $delimiter)" />
<xsl:value-of select="$token" />
<!-- recursive call -->
<xsl:call-template name="nest">
<xsl:with-param name="name" select="substring-after($name, $delimiter)" />
<xsl:with-param name="text" select="substring-after($text, $delimiter)" />
</xsl:call-template>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>