如果元素属性具有Attention,我想省略addressLine1

时间:2015-03-02 18:25:08

标签: xml xslt

如果Address元素的type属性=“Standard With Attention”,我不想显示AddressLine1。相反,我想只显示AddressLine2,AddressLine3和AddressLine4。 我的输出

Attn:Michael,134 W Broadway ST NW,APT 3B,Washington,DC,12345

预期产量: 134 W Broadway ST NW,APT 3B,Washington,DC,12345

我的xml

<ProtectedAddresses>
    <Address InternalAddressID="1618613567" Type="Standard With Attention">
        <AddressLine1>Attn: Michael</AddressLine1>
        <AddressLine2>134 W Broadway ST NW</AddressLine2>
        <AddressLine3>APT 3B</AddressLine3>
        <AddressLine4>Washington, DC, 12345</AddressLine4>
        <Attention>James</Attention>
       </Address>
</ProtectedAddresses>

XSLT

<xsl:for-each select="Addresses/Address">
   <xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty/DCProtectionOrderPartyAdditional/ProtectedAddresses/Address[@InternalAddressID=current()/@InternalAddressID]">
     <xsl:for-each select="AddressLine1 | AddressLine2 | AddressLine3 | AddressLine4">
       <xsl:value-of select="."/>
       <xsl:if test="position()!=last()">, </xsl:if>
     </xsl:for-each>
     <xsl:text>; </xsl:text>
   </xsl:for-each>
</xsl:for-each>

2 个答案:

答案 0 :(得分:0)

如果将<xsl:for-each select="AddressLine1 | AddressLine2 | AddressLine3 | AddressLine4">替换为<xsl:for-each select="AddressLine1[not(../@Type = 'Standard With Attention')] | AddressLine2 | AddressLine3 | AddressLine4">,则在条件../@Type = 'Standard With Attention'为真的情况下构建仅包含三个元素的节点集。

答案 1 :(得分:0)

您的示例相当混乱,因为您只向我们展示了部分XML。因此,很难给出准确的答案,因为上下文不够清晰,并且无法根据您的输入测试给定的答案。

恕我直言,您应该使用而不是:

<xsl:for-each select="ancestor::ProtectionOrder/ProtectionOrderParties/ProtectionOrderParty/DCProtectionOrderPartyAdditional/ProtectedAddresses/Address[@InternalAddressID=current()/@InternalAddressID]">

现在,要解决您的问题(尽可能最好):要在某些情况下排除AddressLine1,您应该使用谓词附加它 - 例如:

<xsl:for-each select="AddressLine1[not(../@Type='Standard With Attention')] | AddressLine2 | AddressLine3 | AddressLine4">

如果 - 似乎 - 条件存在于实际AddressLine1的父元素中,这将起作用。

相关问题