我的 XML 代码显示员工的所有地址及其使用类型。仅当地址具有特定使用类型时才必须显示。其他的必须删除。
下面是xml。
<peci:Person_Communication>
<peci:Address>
<peci:Usage_Type>HOME1</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME2</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME3</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-8</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-9</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME4</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-9</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME5</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME6</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Třebíč</peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-10</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-8</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
</peci:Person_Communication>
必须显示仅包含 COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1
、
COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16
和
COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6
的地址以及不包含Usage 元素的地址。其余部分已被删除。
下面是我在 XSLT 代码中编写的逻辑。
<xsl:template match="peci:Person_Communication/peci:Address">
<xsl:variable name="AddUsage1" select="peci:Usage/peci:Usage_Behavior_ID[.='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1']"/>
<xsl:variable name="AddUsage2" select="peci:Usage/peci:Usage_Behavior_ID[.='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-9' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-8' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-10' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-11' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-12' or .='COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-13']"/>
<xsl:choose>
<xsl:when test="$AddUsage1 and not($AddUsage2)">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:when test="$AddUsage1 and $AddUsage2">
<xsl:copy-of select="peci:Usage_Type|peci:Address_Line_1|peci:Address_Line_3|peci:Address_Line_4|peci:City|peci:Postal_Code|peci:Country|peci:Country_Region"/>
<peci:Usage>
<xsl:copy-of select="$AddUsage1"/>
</peci:Usage>
</xsl:when>
<xsl:when test="not($AddUsage1) and not($AddUsage2)">
<xsl:copy-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:template>`
下面是输出。
<peci:Person_Communication>
<peci:Address>
<peci:Usage_Type>HOME1</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Treb�/peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Address>
<peci:Usage_Type>HOME2</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Treb�/peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Usage>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Address>
<peci:Usage_Type>HOME4</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Treb�/peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage xmlns:ptdf="urn:com.workday/peci/tdf">
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
<peci:Address>
<peci:Usage_Type>HOME5</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Treb�/peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
</peci:Address>
<peci:Usage_Type>HOME6</peci:Usage_Type>
<peci:Address_Line_1>Gen. Sochora</peci:Address_Line_1>
<peci:Address_Line_3>31</peci:Address_Line_3>
<peci:Address_Line_4>957</peci:Address_Line_4>
<peci:City>Treb�/peci:City>
<peci:Postal_Code>674 01</peci:Postal_Code>
<peci:Country>CZ</peci:Country>
<peci:Usage xmlns:ptdf="urn:com.workday/peci/tdf">
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6</peci:Usage_Behavior_ID>
<peci:Usage_Behavior_ID>COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1</peci:Usage_Behavior_ID>
</peci:Usage>
</peci:Person_Communication>
对于变量 $AddUsage1 和 $AddUsage2 都存在的情况,元素 peci:Address/ 丢失,并且使用行为的所有值都放置在单个 peci:Usage 下,而不是单独的元素下。请帮我修复以下逻辑。
<xsl:when test="$AddUsage1 and $AddUsage2">
<xsl:copy-of select="peci:Usage_Type|peci:Address_Line_1|peci:Address_Line_3|peci:Address_Line_4|peci:City|peci:Postal_Code|peci:Country|peci:Country_Region"/>
<peci:Usage>
<xsl:copy-of select="$AddUsage1"/>
</peci:Usage>
</xsl:when>
首先,让我提醒您,获得及时回复的最佳方法是在提问时注意您已提供所有相关信息。
您需要包括:
peci
命名空间前缀的声明;在我的示例中,我已将 peci
绑定到占位符值 not-supplied
)参见 https://stackoverflow.com/help/minimal-reproducible-example
如果我理解正确的话,这是您问题的答案。
我知道您想要删除任何
peci:Address
,如果它包含一个 peci:Usage/peci:Usage_Behavior_ID
,其值不是这三个可接受值之一:
COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1
COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16
COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6
我的示例解决方案使用“身份模板”来复制文档,并定义另一个模板来匹配不可接受的
peci:Address
元素,该模板会覆盖这些元素的身份模板。 peci:Address
匹配模板为空,因此匹配的结果是忽略该元素,因此不会将其复制到输出中。
请参阅 XSLT 2.0 规范中有关“身份转换”的部分,其中给出了复制文档并忽略特定
note
元素的示例。 https://www.w3.org/TR/xslt20/#d5e17568
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:peci="not-supplied">
<xsl:output method="xml" indent="true"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--
Match, and ignore, any Address which contains
a Usage/Usage_Behaviour_ID for which
it's not the case that the ID appears in the list of acceptable IDs
-->
<xsl:template match="
peci:Address[
peci:Usage/peci:Usage_Behavior_ID[
not ( . =
(
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6'
)
)
]
]
"/>
</xsl:stylesheet>
顺便说一句,如果您想知道,布尔表达式
not ( . =
(
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6'
)
)
不等于
. != (
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-1',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-16',
'COMMUNICATION_USAGE_BEHAVIOR_TENANTED-3-6'
)
在第一个表达式中,如果序列中的
any项等于上下文项,则
=
运算符返回 true
。
在第二个表达式中,如果序列中的
any项与上下文项不同,则
!=
运算符返回 true
。
一般来说,
not($A = $B)
与$A != $B
不同。有关详细信息,请参阅 XPath 规范中有关 “常规比较” 的部分。