如何删除父节点以进行输出并仅显示没有父节点的子节点?

时间:2014-11-14 15:26:57

标签: xml xslt

下面xslt的输出显示如下:

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

如您所见,父节点 ext:PersonBirthDate 有3个具有相同名称的子节点。 如何删除输出中的父级,只显示3个子节点。

我希望我的输出看起来像这样:

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

XML文档

<Party ID="14474176" InternalPartyID="1612366618">
    <Gender Word="F ">Female</Gender>
    <ApproximateDOB>3/4/1956</ApproximateDOB>
    <DateOfBirth>04/21/1956</DateOfBirth>
    <DateOfBirth Current="true">05/21/1956</DateOfBirth>
</Party>

这是我的xslt代码与原来的

略有不同
<!--Templates for DateOfBirth for the respondent-->
     <xsl:template match="DateOfBirth">
       <ext:PersonBirthDate>
          <xsl:attribute name="ext:approximateDateIndicator">false</xsl:attribute>
          <xsl:attribute name="ext:currentIndicator">false</xsl:attribute>
          <xsl:value-of select="mscef:formatDate(string(.))"/>
       </ext:PersonBirthDate>
     </xsl:template>

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

<!--Templates for ApproximateDOB for the respondent-->
      <xsl:template match="ApproximateDOB">
          <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{not(../DateOfBirth)}" >
          <xsl:value-of select="mscef:formatDate(string(.))"/>
        </ext:PersonBirthDate>
      </xsl:template>

<!--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:apply-templates select="ApproximateDOB|DateOfBirth"/>
        </xsl:for-each>
      </xsl:for-each>
   </ext:Respondent>
 </xsl:template>

1 个答案:

答案 0 :(得分:1)

或者很快:

<xsl:template match="ApproximateDOB">
    <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{count(../ApproximateDOB)=1}">
        <xsl:value-of select="mscef:formatDate(string(.))"/>
    </ext:PersonBirthDate>
</xsl:template>

注意:恕我直言,你最好将所有三个DOB模板合并为一个(我相信在你之前的问题中已经向你建议)。


编辑:

  

仅当存在的唯一元素是ApproximateDOB时才会出现。

阿。所以我们误解了这个要求。这可以通过改变:

来实现
<ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{count(../ApproximateDOB)=1}">

为:

<ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{count(../*)=1}">

但是,基于你的previous question,我怀疑你并不是字面意思,因为当Party只有一个ApproximateDOB时它也会返回false,但它也有一个性别。所以你可能想这样做:

<ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{not(../DateOfBirth)}">

相反,当没有兄弟DateOfBirth元素时,它将返回true。

相关问题