2014-12-29 30 views
-3

當我的xml代碼有聯繫人條件節點地址和派對元素時,在派對名稱後添加一個分號。 我不想在名稱後面輸入分號,因爲之後沒有任何其他內容。 這裏是輸出:如何在文本片段之間添加分號但不在文本片段的末尾?

答辯人在就業下列的地方(縣)以下方(IES)的訪問受到限制如下:布朗,萊恩**,** 1048區域RD,聖保羅,MN,55101

這裏我的輸出保持在條件文本元素的末尾顯示一個分號,即使只有一方的名字。 如何更改我的xslt,以便只有一個派對名稱時不會顯示此分號?

申請人被授予以下孩子的唯一身體和法定監護權:Brown,Rayne;

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

你有什麼建議?我應該刪除其中一個嗎? – user3781064

+2

您是否注意到您近期收到的許多問題只會降低評論的質量並造成很多混淆?有時間回顧一下XSLT的基礎知識_並且[在SO上提出問題的基本知識](http://stackoverflow.com/help/how-to-ask)_and_ at [如何創建一個好的代碼示例]( http://stackoverflow.com/help/mcve)。 –

回答

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但假設這可能是工作,因爲就像提到的,應當根據黨元素的含量:移動<xsl:if test="position() != last()">語句來<xsl:for-each select="Parties/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> 

應該寫後每個;Party如果它不是最後的Party

更新:有關評論中提到的問題的更新問題 - 我不知道我是否完全理解問題。關於更新的XSLT,應該如下:如果有多於一個Party,則將在每個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,在最後的PartyAddress元素的計數爲的情況下。

+0

我遇到的問題是,當我使用';'並在條件文本中用我的xml中的2方進行測試,我沒有在名稱之間得到分號。但是,當我在xml conditiontext中只有一方時,在名稱末尾添加了一個分號,這不是我想要的。當我刪除!並保留這樣的代碼';'當xml有2個派對時添加一個分號,但是當xml只有一個派對時它也添加了一個分號。 – user3781064

+0

@ user3781064剛剛更新了我的答案,如果這仍然行不通,那麼在問題中輸入更多xml會更好,因爲這是一種猜測。 –

+0

非常感謝你mathias_h。將測試位置聲明移動到派對循環之外工作!我再次感謝你。我現在將其標記爲已解決,並將您的答案標記爲解決我的問題的正確方法。 – user3781064

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>