我有一个带有节点名字的源模式(包含forename +''+ middlename),我需要将目标模式中的中间名分开,这样就会转到OtherNames。
我目前有以下 xslt模板:
<xsl:template name="StringSplit">
<xsl:param name="valFirstnames" />
<xsl:choose>
<xsl:when test="contains($valFirstnames, ' ')">
<xsl:call-template name="StringSplit">
<xsl:with-param name="valFirstnames" select="substring-after($valFirstnames, ' ')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<OtherFirstNames><xsl:value-of select="$valFirstnames" /></OtherFirstNames>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
此xslt模板的当前输出是将Middlename写出两次而不是一次:
<OtherFirstName>Middlename</OtherFirstName>
<OtherFirstName>Middlename</OtherFirstName>
预期:
<OtherFirstName>Middlename</OtherFirstName>
示例输入
<Data>
<SubjectName>
<forenames>first middle</forenames>
</SubjectName>
<SubjectPartner>
<forenames>first middle</forenames>
<Otherforenames>first middle</Otherforenames>
</SubjectPartner>
<etc./>
</Data>
示例输出
<Data>
<SubjectName>
<firstname>first</firstname>
<OtherFirstName>middle</OtherFirstname>
</SubjectName>
<SubjectPartner>
<firstname>first</firstname>
<OtherFirstName>middle</OtherFirstName>
<OtherFirstName>middle</OtherFirstName>
</SubjectPartner>
<etc./>
</Data>
我正在考虑更正当前的xslt并更新以合并具有相同子元素前缀名称的其他合作伙伴元素。
答案 0 :(得分:1)
你的例子对我来说没什么意义。我猜你想做点什么:
XSLT 1.0
<xsl:stylesheet version="1.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="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="forenames">
<firstname><xsl:value-of select="substring-before(concat(., ' '), ' ')"/></firstname>
<xsl:if test="contains(., ' ')">
<OtherFirstName><xsl:value-of select="substring-after(., ' ')"/></OtherFirstName>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
请注意,这假设一个人最多有两个名字。应用于以下测试输入时:
<Data>
<SubjectName>
<forenames>Alan Benjamin</forenames>
<surname>Adams</surname>
</SubjectName>
<SubjectPartner>
<forenames>Cecily Diana</forenames>
<surname>Crown</surname>
</SubjectPartner>
<Single>
<forenames>Eve</forenames>
<surname>Evans</surname>
</Single>
<Triple>
<forenames>Frank George Herbert</forenames>
<surname>Forrester</surname>
</Triple>
</Data>
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<SubjectName>
<firstname>Alan</firstname>
<OtherFirstName>Benjamin</OtherFirstName>
<surname>Adams</surname>
</SubjectName>
<SubjectPartner>
<firstname>Cecily</firstname>
<OtherFirstName>Diana</OtherFirstName>
<surname>Crown</surname>
</SubjectPartner>
<Single>
<firstname>Eve</firstname>
<surname>Evans</surname>
</Single>
<Triple>
<firstname>Frank</firstname>
<OtherFirstName>George Herbert</OtherFirstName>
<surname>Forrester</surname>
</Triple>
</Data>