如何显示节点的所有出生日期?

时间:2014-11-13 16:46:19

标签: xml xslt

我的xml文档包含3个出生日期,当前近似日期。我想显示3个日期作为输出。

我的xml代码

<Party ID="76" InternalPartyID="18">
    <Gender Word="F ">Female</Gender>
    <ApproximateDOB>03/4/1956</ApproximateDOB>
    <DateOfBirth Current="true">05/21/1956</DateOfBirth>
    <DateOfBirth>04/21/1956</DateOfBirth>
</Party>

我的XSLT代码

<!--Respondent -->
   <xsl:for-each select="RespondentPartyID">
   <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='RSP') ])]">
   <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
<xsl:call-template name="Respondent">
   <xsl:with-param name="pProtectionOrderID">
   <xsl:value-of select="$vProtectionOrderID"/>
   </xsl:with-param>
</xsl:call-template>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

<!--Respondent Template-->
<xsl:template name="Respondent">
    <xsl:param name="pProtectionOrderID"/>
<ext:Respondent>
<!--Guardian -->
    <xsl:for-each select="//CaseParty[(Connection[(@Word='GRD')])][1]">
        <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
            <xsl:call-template name="Guardian"/>
        </xsl:for-each>
    </xsl:for-each>
    <ext:PersonBirthDate>
        <xsl:choose>
            <xsl:when test="DateOfBirth[@Current='true']">
                <xsl:attribute name="ext:ApproximateDateIndicator">false</xsl:attribute>
                <xsl:attribute name="ext:CurrentIndicator">true</xsl:attribute>
                <xsl:value-of select="mscef:formatDate(string(DateOfBirth[@Current='true']))"/>
            </xsl:when>
            <xsl:when test="ApproximateDOB">
                <xsl:attribute name="ext:ApproximateDateIndicator">true</xsl:attribute>
                <xsl:attribute name="ext:CurrentIndicator">true</xsl:attribute>
                <xsl:value-of select="mscef:formatDate(string(ApproximateDOB))"/>
            </xsl:when>
        </xsl:choose>
    </ext:PersonBirthDate>
</ext:Respondent>

如何更改我的xslt代码,使输出看起来如下所示。现在我的xslt只返回当前的出生日期:

<ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">1956-05-21</ext:PersonBirthDate>
<ext:PersonBirthDate ext:approximateDateIndicator="true" ext:currentIndicator="false">1956-03-04</ext:PersonBirthDate>
<ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="false">1956-04-21</ext:PersonBirthDate>

1 个答案:

答案 0 :(得分:1)

考虑为每个可能的出生日期&#39;模板匹配。元件。像这样:

   <xsl:template match="DateOfBirth">
      <ext:PersonBirthDate>
         <xsl:attribute name="approximateDateIndicator">false</xsl:attribute>
         <xsl:attribute name="currentIndicator">false</xsl:attribute>
         <xsl:value-of select="."/>
      </ext:PersonBirthDate>
   </xsl:template>

   <xsl:template match="DateOfBirth[@Current='true']">
      <ext:PersonBirthDate>
         <xsl:attribute name="approximateDateIndicator">false</xsl:attribute>
         <xsl:attribute name="currentIndicator">true</xsl:attribute>
         <xsl:value-of select="."/>
      </ext:PersonBirthDate>
   </xsl:template>

   <xsl:template match="ApproximateDOB">
      <ext:PersonBirthDate>
         <xsl:attribute name="approximateDateIndicator">true</xsl:attribute>
         <xsl:attribute name="currentIndicator">false</xsl:attribute>
         <xsl:value-of select="."/>
      </ext:PersonBirthDate>
   </xsl:template>

然后,您可以将现有的XSLT代码更改为此代码,然后输出所有三个日期。

   <xsl:apply-templates select="ApproximateDOB|DateOfBirth" />

拥有如此多的模板显然非常重复,但将模板合并为一个模板应该没有问题。

为初学者尝试这个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="ext">
   <xsl:output method="xml" indent="yes" />

   <xsl:template match="Party">
       <ext:Respondent>
           <xsl:apply-templates select="ApproximateDOB|DateOfBirth" />
       </ext:Respondent>
   </xsl:template>

   <xsl:template match="ApproximateDOB|DateOfBirth">
      <ext:PersonBirthDate 
            approximateDateIndicator="{local-name() = 'ApproximateDOB'}" 
            currentIndicator="{@Current = 'true'}">
         <xsl:value-of select="."/>
      </ext:PersonBirthDate>
   </xsl:template>
</xsl:stylesheet>

请注意使用Attribute Value Templates来进一步简化代码。

当上述XSLT应用于以下XML

<Party ID="76" InternalPartyID="18">
    <Gender Word="F ">Female</Gender>
    <ApproximateDOB>03/4/1956</ApproximateDOB>
    <DateOfBirth Current="true">05/21/1956</DateOfBirth>
    <DateOfBirth>04/21/1956</DateOfBirth>
</Party>

以下是输出

<ext:Respondent xmlns:ext="ext">
    <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="false">03/4/1956</ext:PersonBirthDate>
    <ext:PersonBirthDate approximateDateIndicator="false" currentIndicator="true">05/21/1956</ext:PersonBirthDate>
    <ext:PersonBirthDate approximateDateIndicator="false" currentIndicator="false">04/21/1956</ext:PersonBirthDate>
</ext:Respondent>

(我遗漏了日期格式,因为我没有在本地测试的扩展功能)

编辑:要将其纳入您的XSLT代码,您可以这样做

<!--Respondent -->
<xsl:for-each select="RespondentPartyID">
   <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='RSP') ])]">
   <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
<xsl:call-template name="Respondent">
   <xsl:with-param name="pProtectionOrderID">
   <xsl:value-of select="$vProtectionOrderID"/>
   </xsl:with-param>
</xsl:call-template>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

<!--Respondent Template-->
<xsl:template name="Respondent">
   <xsl:param name="pProtectionOrderID"/>
   <ext:Respondent>
   <!--Guardian -->
    <xsl:for-each select="//CaseParty[(Connection[(@Word='GRD')])][1]">
        <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
            <xsl:call-template name="Guardian"/>
        </xsl:for-each>
    </xsl:for-each>
    <xsl:apply-templates select="ApproximateDOB|DateOfBirth" />
 </ext:Respondent>
</xsl:template>

<xsl:template match="ApproximateDOB|DateOfBirth">
      <ext:PersonBirthDate 
            approximateDateIndicator="{local-name() = 'ApproximateDOB'}" 
            currentIndicator="{@Current = 'true'}">
         <xsl:value-of select="mscef:formatDate(string(.))"/>
      </ext:PersonBirthDate>
   </xsl:template>