0

我有一个带有节点名字的源模式(包含名字+''+中间名),我需要在目标模式中分离出中间名,以便将其发送给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 模板的当前输出是两次而不是一次写出中间名:

<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 并更新以合并具有相同子元素名字的其他合作伙伴元素。

4

1 回答 1

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>
于 2014-06-04T15:06:00.333 回答