我想只在xml文档中有多个请求时显示第一个请愿

时间:2014-12-10 22:20:40

标签: xml xslt

我有一个带有2个请愿者的xml,其中请愿者=真。我只想显示第一个,即使有多个也是如此。我该怎么做呢? 我正在使用我在下面添加的请愿者模板。

所需的输出只是这样的一个请愿

<ext:ProtectedParty>
        <ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">1969-08-06</ext:PersonBirthDate>
        <nc:PersonName>
            <nc:PersonGivenName>MARY</nc:PersonGivenName>
            <nc:PersonMiddleName/>
            <nc:PersonSurName>TESTER</nc:PersonSurName>
            <nc:PersonNameSuffixText/>
            <nc:PersonFullName>TESTER, MARY</nc:PersonFullName>
        </nc:PersonName>
        <ext:PersonRaceCode/>
        <nc:PersonSexCode>F </nc:PersonSexCode>
        <ext:PetitionerIndicator>true</ext:PetitionerIndicator>
    </ext:ProtectedParty>
    <ext:ProtectedParty>
        <ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">2012-08-16</ext:PersonBirthDate>
        <nc:PersonName>
            <nc:PersonGivenName>Baby</nc:PersonGivenName>
            <nc:PersonMiddleName/>
            <nc:PersonSurName>Tester</nc:PersonSurName>
            <nc:PersonNameSuffixText/>
            <nc:PersonFullName>Tester, Baby</nc:PersonFullName>
        </nc:PersonName>
        <ext:PersonRaceCode/>
        <nc:PersonSexCode>F </nc:PersonSexCode>
        <ext:PetitionerIndicator>false</ext:PetitionerIndicator>
    </ext:ProtectedParty>

我的xml文档

<ProtectionOrderParties>
<ProtectionOrderParty InternalPartyID="77">
    <ProtectionOrderPartyNames>
        <ProtectionOrderPartyName Current="true" InternalNameID="77" FormattedName="Marko, Keem"/>
    </ProtectionOrderPartyNames>
    <ProtectionOrderConnection>
        <Petitioner>true</Petitioner>
        <ProtectedParty>true</ProtectedParty>
    </ProtectionOrderConnection>
</ProtectionOrderParty>
<ProtectionOrderParty InternalPartyID="97">
    <ProtectionOrderPartyNames>
        <ProtectionOrderPartyName Current="true" InternalNameID="53" FormattedName="JoAnn, Nenni"/>
    </ProtectionOrderPartyNames>
    <ProtectionOrderConnection>
        <Petitioner>true</Petitioner>
        <ProtectedParty>true</ProtectedParty>
    </ProtectionOrderConnection>
</ProtectionOrderParty>

我的xslt代码

       <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://www.courts.state.mn.us/ProtectionOrderExtension/1.0" exclude-result-prefixes="mscef msxsl exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="ProtectionOrder">
    <ext:ProtectionOrder>
    <xsl:variable name="vProtectionOrderID">
    <xsl:value-of select="@InternalProtectionOrderID"/>
    </xsl:variable>
<!--Petitioner's Address-->
<xsl:for-each select="ProtectionOrderParties/ProtectionOrderParty[(ProtectionOrderConnection/Petitioner='true') and (ProtectionOrderConnection/ProtectedParty='false')][1]">
    <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='PET') and (not(RemovedReason))])][1]">
    <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]/Address">
    <xsl:call-template name="Location"/>
    </xsl:for-each>
    </xsl:for-each>
    </xsl:for-each>
<!--Petitioner-->
    <xsl:for-each select="ProtectionOrderParties/ProtectionOrderParty[(ProtectionOrderConnection/Petitioner='true') and (ProtectionOrderConnection/ProtectedParty='false')][1]">
    <xsl:for-each select="/Integration/Party[@InternalPartyID=current()/@InternalPartyID]">
    <xsl:call-template name="Petitioner"/>
    </xsl:for-each>
    </xsl:for-each>
<!--Protected Party-->
    <xsl:for-each select="ProtectionOrderParties/ProtectionOrderParty[ProtectionOrderConnection/ProtectedParty='true']">
    <xsl:variable name="vPetitionerIndicator">
    <xsl:value-of select="ProtectionOrderConnection/Petitioner"/>
    </xsl:variable>
    <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID)]">
    <xsl:for-each select="/Integration/Party[@InternalPartyID=current()/@InternalPartyID]">
    <xsl:call-template name="ProtectedParty">
    <xsl:with-param name="pPetionerIndicator">
    <xsl:value-of select="$vPetitionerIndicator"/>
    </xsl:with-param>
    </xsl:call-template>
    </xsl:for-each>
    </xsl:for-each>
    </xsl:for-each>
    </xsl:template>
<!--Petitioner Template-->
    <xsl:template name="Petitioner">
    <ext:Petitioner>
    <ext:AddressReference>
    <xsl:for-each select="Address[@PartyCurrent='true']">
    <xsl:attribute name="ext:currentIndicator"><xsl:value-of select="@PartyCurrent"/></xsl:attribute>
    <nc:LocationReference>
    <xsl:attribute name="s:ref"><xsl:text>INT</xsl:text><xsl:value-of select="@ID"/></xsl:attribute>
    </nc:LocationReference>
    </xsl:for-each>
    </ext:AddressReference>
    <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>
    <nc:PersonName>
    <nc:PersonGivenName>
    <xsl:value-of select="PartyName/NameFirst"/>
    </nc:PersonGivenName>
    <nc:PersonMiddleName>
    <xsl:value-of select="PartyName/NameMiddle"/>
    </nc:PersonMiddleName>
    <nc:PersonSurName>
    <xsl:value-of select="PartyName/NameLast"/>
    </nc:PersonSurName>
    <nc:PersonNameSuffixText>
    <xsl:value-of select="PartyName/NameSuffix"/>
    </nc:PersonNameSuffixText>
    <nc:PersonFullName>
    <xsl:value-of select="PartyName/FormattedName"/>
    </nc:PersonFullName>
    </nc:PersonName>
    <ext:PersonRaceCode>
    <!--<xsl:value-of select="CaseParty/ObservedRace/@Word"/>-->
    <xsl:value-of select="//Case/CaseParty[@InternalPartyID=current()/@InternalPartyID]/ObservedRace/@Word"/>
    </ext:PersonRaceCode>
    <nc:PersonSexCode>
    <xsl:value-of select="Party/Gender/@Word"/>
    </nc:PersonSexCode>
    </ext:Petitioner>
    </xsl:template>
<!--Protected Party Template-->
    <xsl:template name="ProtectedParty">
    <xsl:param name="pPetionerIndicator"/>
    <ext:ProtectedParty>
    <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>
    <xsl:for-each select="PartyName[@Current='true']">
    <nc:PersonName>
    <nc:PersonGivenName>
    <xsl:value-of select="NameFirst"/>
    </nc:PersonGivenName>
    <nc:PersonMiddleName>
    <xsl:value-of select="NameMiddle"/>
    </nc:PersonMiddleName>
    <nc:PersonSurName>
    <xsl:value-of select="NameLast"/>
    </nc:PersonSurName>
    <nc:PersonNameSuffixText>
    <xsl:value-of select="NameSuffix"/>
    </nc:PersonNameSuffixText>
    <nc:PersonFullName>
    <xsl:value-of select="FormattedName"/>
    </nc:PersonFullName>
</nc:PersonName>
</xsl:for-each>
<ext:PersonRaceCode>
<!--<xsl:value-of select="//CaseParty/ObservedRace/@Word"/>-->
<xsl:value-of select="//Case/CaseParty[@InternalPartyID=current()/@InternalPartyID]/ObservedRace/@Word"/>
</ext:PersonRaceCode>
<nc:PersonSexCode>
<xsl:value-of select="Gender/@Word"/>
</nc:PersonSexCode>
<ext:PetitionerIndicator>
<xsl:value-of select="$pPetionerIndicator"/>
</ext:PetitionerIndicator>
</ext:ProtectedParty>
    </xsl:template>
<!--Template 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>
<!--Template for DateOfBirth or the respondent where Current = true-->
    <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>
<!--Template for ApproximateDOB-->
    <xsl:template match="ApproximateDOB">
    <ext:PersonBirthDate ext:approximateDateIndicator="true" ext:currentIndicator="{not(../DateOfBirth)}">
    <xsl:value-of select="mscef:formatDate(string(.))"/>
    </ext:PersonBirthDate>
    </xsl:template>
</ext:Respondent>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

尝试以下内容:

  <xsl:template match="/*">
    <good>
      <xsl:call-template name="secondaryCode">
        <xsl:with-param name="data" select="current()/ProtectionOrderParty[position()=1]"/>
      </xsl:call-template>
    </good>
  </xsl:template>
  <xsl:template name="secondaryCode">
    <xsl:param name="data"/>
    <!--your code-->
  </xsl:template>

在secondaryCode模板中,您可以进行操作