如何在文本片段之间添加半冒号但不在文本片段的末尾添加?

时间:2014-12-29 21:06:39

标签: xml xslt

当我的xml代码没有条件节点地址和派对元素时,会在派对名称后面添加一个分号。 我不希望名字后面有半冒号,因为之后没有其他内容。 以下是输出:

    被访者在以下就业地点与下列各方的访问权限如下:Brown,Rayne **; ** 1048 Regions RD,St Paul,MN,55101     08

这里我的输出在条件文本元素的末尾显示一个半冒号,即使只有一个聚会名称。 如何更改我的xslt,以便在只有一个聚会名称时不显示此半冒号?

        申诉人获得以下子女的唯一身体和法律监护权:Brown,Rayne;         09     

我的xml代码:

        <Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="BCA PO Notification" MessageID="57832809" xmlns="">
        <ProtectionOrder Op="E" InternalProtectionOrderID="2563" xmlns:user="http://tylertechnologies.com">
            <ProtectionOrderParties>
                <ProtectionOrderParty InternalPartyID="1614450754">
                    <ProtectionOrderPartyNames>
                        <ProtectionOrderPartyName Current="true" InternalNameID="1615262152" FormattedName="Brown, Rayne"/>
                    </ProtectionOrderPartyNames>
                    <Added>12/29/2014</Added>
                    <ProtectionOrderConnection>
                        <Petitioner>true</Petitioner>
                        <FilingParty>false</FilingParty>
                        <ProtectedParty>true</ProtectedParty>
                        <Minor>false</Minor>
                    </ProtectionOrderConnection>
                    <MNProtectionOrderAdditional InternalID="2604" xmlns:fn="http://www.w3.org/2005/xpath-functions">
            <Conditions>
                <Condition>
                    <Code Word="03F1EXPC">03.F.1 Respondent must not call/enter place of employment</Code>
                    <Description>Respondent must not call or enter the place of employment of the following party(ies). This includes all land, parking lots, and buildings of the following addresses:</Description>
                    <Parties>
                        <Party InternalPartyID="1614450754"/>
                    </Parties>
                    <Addresses>
                        <Address InternalAddressID="1618211350"/>
                    </Addresses>
                </Condition>
                <Condition>
                    <Code Word="03F2EXPC">03.F.2 Respondent's access at place of employment</Code>
                    <Description>Respondent's access to the following party(ies) at the following place(s) of employment is limited as follows: </Description>
                    <Parties>
                        <Party InternalPartyID="1614450754"/>
                    </Parties>
                    <Addresses>
                        <Address InternalAddressID="1618211350"/>
                    </Addresses>
                </Condition>
            </Conditions>
        </MNProtectionOrderAdditional>
                </ProtectionOrderParty>
            </ProtectionOrderParties>
            <MNProtectionOrderAdditional InternalID="2559" xmlns:fn="http://www.w3.org/2005/xpath-functions">
                </xsl:if>           
</MNProtectionOrderAdditional>
        </ProtectionOrder>
</Integration>

我的xslt代码

<xsl:for-each select="MNProtectionOrderAdditional/Conditions/Condition">
            <ext:ProtectionOrderCondition>
                <ext:ConditionText>
                    <xsl:variable name="vCondition">
                        <xsl:value-of select="normalize-space(Description)"/>
                        <xsl:text> </xsl:text>
                        <xsl:for-each select="Parties/Party">
                            <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
                                <xsl:value-of select="ProtectionOrderPartyNames/ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
                                <!--<xsl:text>; </xsl:text>-->
                                <xsl:if test="position() = last()">
                                <xsl:text>; </xsl:text>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:for-each>
                        <xsl:text> </xsl:text>
                        <xsl:for-each select="Addresses/Address">
                            <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty/MNProtectionOrderPartyAdditional/ProtectedAddresses/Address[@InternalAddressID=current()/@InternalAddressID]">
                                <xsl:if test="AddressLine1">
                                    <xsl:value-of select="AddressLine1"/>
                                    <xsl:text>, </xsl:text>
                                </xsl:if>
                                <xsl:if test="AddressLine2">
                                    <xsl:value-of select="AddressLine2"/>
                                    <xsl:text>, </xsl:text>
                                </xsl:if>
                                <xsl:if test="AddressLine3">
                                    <xsl:value-of select="AddressLine3"/>
                                    <xsl:text>, </xsl:text>
                                </xsl:if>
                                <xsl:if test="AddressLine4">
                                    <xsl:value-of select="AddressLine4"/>
                                </xsl:if>
                                <xsl:text>; </xsl:text>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:variable>
                    <xsl:choose>
                        <xsl:when test="substring($vCondition,string-length($vCondition)-1,2)='; '">
                            <xsl:value-of select="substring($vCondition,1,string-length($vCondition)-2)"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$vCondition"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </ext:ConditionText>
                <ext:ConditionCode>
                    <xsl:value-of select="document(concat($gEnvPath,'\ConfigFiles\MNCISCodes\ProtectionOrderConditionCodeMapping.xml'))
/ProtectionOrderConditionCodeMapping/Mapping[MNCISCode=current()/Code/@Word]/BCACode"/>
                </ext:ConditionCode>
            </ext:ProtectionOrderCondition>
        </xsl:for-each>

更新了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">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template name="ProtectionOrder">
        <ext:ProtectionOrder>
<!--ext:ProtectionOrderCondition-->
            <xsl:for-each select="MNProtectionOrderAdditional/Conditions/Condition">
                <ext:ProtectionOrderCondition>
                    <ext:ConditionText>
                        <xsl:variable name="vCondition">
                            <xsl:value-of select="normalize-space(Description)"/>
                            <xsl:text> </xsl:text>
                            <xsl:for-each select="Parties/Party">
                                <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
                                    <xsl:value-of select="ProtectionOrderPartyNames/ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
                                    </xsl:for-each>
                                    <xsl:if test="position() != last()">
                                    <xsl:text>; </xsl:text>
                                    </xsl:if>
                            </xsl:for-each>
                            <xsl:text> </xsl:text>
                            <xsl:for-each select="Addresses/Address">
                                <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty/MNProtectionOrderPartyAdditional/ProtectedAddresses/Address[@InternalAddressID=current()/@InternalAddressID]">
                                    <xsl:if test="AddressLine1">
                                        <xsl:value-of select="AddressLine1"/>
                                        <xsl:text>, </xsl:text>
                                    </xsl:if>
                                    <xsl:if test="AddressLine2">
                                        <xsl:value-of select="AddressLine2"/>
                                        <xsl:text>, </xsl:text>
                                    </xsl:if>
                                    <xsl:if test="AddressLine3">
                                        <xsl:value-of select="AddressLine3"/>
                                        <xsl:text>, </xsl:text>
                                    </xsl:if>
                                    <xsl:if test="AddressLine4">
                                        <xsl:value-of select="AddressLine4"/>
                                    </xsl:if>
                                    <xsl:text>; </xsl:text>
                                </xsl:for-each>
                            </xsl:for-each>
                        </xsl:variable>
                        <xsl:choose>
                            <xsl:when test="substring($vCondition,string-length($vCondition)-1,2)='; '">
                                <xsl:value-of select="substring($vCondition,1,string-length($vCondition)-2)"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:value-of select="$vCondition"/>
                            </xsl:otherwise>
                        </xsl:choose>
                    </ext:ConditionText>
                    <ext:ConditionCode>
                        <xsl:value-of select="document(concat($gEnvPath,'\ConfigFiles\MNCISCodes\ProtectionOrderConditionCodeMapping.xml'))
    /ProtectionOrderConditionCodeMapping/Mapping[MNCISCode=current()/Code/@Word]/BCACode"/>
                    </ext:ConditionCode>
                </ext:ProtectionOrderCondition>
            </xsl:for-each>
        </ext:ProtectionOrder>
    </xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:0)

可能是一种误解,但如果这个

,它会不会像请求的那样起作用
<xsl:for-each select="Parties/Party">
  <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties
         /ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
    <xsl:value-of select="ProtectionOrderPartyNames/
         ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
      <xsl:if test="position() = last()">
        <xsl:text>; </xsl:text>
      </xsl:if>
  </xsl:for-each>
</xsl:for-each>

只会改为:

<xsl:for-each select="Parties/Party">
  <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties
         /ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
    <xsl:value-of select="ProtectionOrderPartyNames/
         ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
      <xsl:if test="position() != last()">
        <xsl:text>; </xsl:text>
      </xsl:if>
  </xsl:for-each>
</xsl:for-each>

目前为最后一个元素添加了;(当position() = last()时),但我想你只想在多方之间(如果有的话),所以它应该适用于你将其更改为position() != last()

更新:正如评论中所述,建议的方法无法按预期工作。不知道输入XML但假设这可能有效,因为,如上所述,它应该基于Party元素的数量:

<xsl:for-each select="Parties/Party">
  <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties
         /ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
    <xsl:value-of select="ProtectionOrderPartyNames/
         ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
  </xsl:for-each>
  <xsl:if test="position() != last()">
      <xsl:text>; </xsl:text>
  </xsl:if>
</xsl:for-each>

如果<xsl:if test="position() != last()">语句不是最后一个<xsl:for-each select="Parties/Party">,则将;语句移至Party而不是内部循环,应在每个Party之后写Party

更新:有关评论中提到的问题的更新问题 - 我不知道我是否完全理解了这个问题。关于更新的XSLT,它应该如下:如果有多个;,将使用Party在每个position() != last()之间添加;。据我了解,Party名称之后还应该有Address,以防;后跟Party。当前,无论是否只有一个或多个party元素,最后Address名称后面都不会有Party。我建议采用以下方法:计算last()个节点,并检查Address元素的位置是否为addressCount,以及是否有> 0。在这种情况下,变量&gt; 0<xsl:variable name="addressCount" select="count(Addresses/Address)"/> <xsl:for-each select="Parties/Party"> <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties /ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]"> <xsl:value-of select="ProtectionOrderPartyNames/ ProtectionOrderPartyName[@Current='true']/@FormattedName"/> </xsl:for-each> <xsl:if test="position() != last() or (position() = last() and $addressCount &gt; 0)"> <xsl:text>; </xsl:text> </xsl:if> </xsl:for-each> ,写为<xsl:if test="position() != last() or (position() = last() and $addressCount &gt; 0)"> <xsl:text>; </xsl:text> </xsl:if> (转义):

;

使用

Party

表示:如果不是最后一个;,请写Party;如果有多个Party,则在Party名称之间添加Address或者,如果它是最后一个> 0,{{1}}元素的数量是{{1}}。

答案 1 :(得分:0)

这是我按建议做的,我在每个语句中移动代码。

xslt代码

<xsl:for-each select="Parties/Party">
   <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty[@InternalPartyID=current()/@InternalPartyID]">
   <xsl:value-of select="ProtectionOrderPartyNames/ProtectionOrderPartyName[@Current='true']/@FormattedName"/>
    <xsl:if test="position() = last()">
    <xsl:text>; </xsl:text>
    </xsl:if>
    </xsl:for-each>
</xsl:for-each>