【问题标题】:I want to display the first petition only when there is more than one in xml document我只想在 xml 文档中有多个时才显示第一个请愿书
【发布时间】:2014-12-10 22:20:39
【问题描述】:

我有一个包含 2 个请愿人的 xml,其中请愿人 = true。即使有多个,我也只想显示第一个。我该怎么做呢? 我正在使用我在下面添加的请愿人模板。

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

<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>

【问题讨论】:

  • 由于我无法实际尝试自己运行它,我只能猜测...您是否尝试过在名为 @ 的模板中更改第一个 xsl:for-each 中的 select 987654327@ 到 (ProtectionOrderParties/ProtectionOrderParty[ProtectionOrderConnection/Petitioner='true' and ProtectionOrderConnection/ProtectedParty='false'])[1]?可能还想看看stackoverflow.com/help/mcve
  • 我已删除 [1] 但仍显示两位请愿人。我只想显示第一个请愿者。
  • 不要删除[1];尝试将 [1] 之前的所有内容都用括号括起来(参见前面评论中的示例)。
  • 我按照建议做了,但我的输出中仍然有两位请愿者。我已经为我的 xml 编辑了帖子并包含了整个 xml 代码。这可能会有所帮助。
  • 真正有用的是我们可以运行一个实际的 XSLT 来重现您的问题。如果添加此模板(不更改任何其他内容)会发生什么情况:&lt;xsl:template match="ProtectionOrderParty" priority="100"/&gt;?

标签: xml xslt


【解决方案1】:

尝试以下方法:

  <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模板中您可以进行操作

【讨论】:

  • 我真的不明白你要我做什么。
  • 匹配“/*”的模板仅从输入 xml 中提取第一个节点并将其发送到另一个模板,您可以在该模板中对第一个“ProtectionOrderParty”节点执行任何操作
猜你喜欢
  • 2019-10-28
  • 2017-05-18
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 2016-10-13
  • 2020-05-19
相关资源
最近更新 更多