xslt - 如何在 XSLT 中正确实现 if-else 条件?
问题描述
我有 XML,其中有一个 ContactRecords 节点:
<Organisations>
<Organisation>
<Tag1>ValueElementTag1</Tag1>
<Tag2>ValueElementTag2</Tag2>
<Tag3>ValueElementTag3</Tag3>
<ContactRecords>
<item>
<ContactRecordType>AAAAA</ContactRecordType>
<ContactValue>ValueAAAAA</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>BBBBB</ContactRecordType>
<ContactValue>ValueBBBBB</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>CCCCC</ContactRecordType>
<ContactValue>ValueCCCCC</ContactValue>
</item>
</ContactRecords>
</Organisation>
<Organisation>
<Tag1>ValueElementTag1</Tag1>
<Tag2>ValueElementTag2</Tag2>
<Tag3>ValueElementTag3</Tag3>
<ContactRecords>
<item>
<ContactRecordType>AAAAA</ContactRecordType>
<ContactValue>ValueAAAAA</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>BBBBB</ContactRecordType>
<ContactValue>ValueBBBBB</ContactValue>
<Address xmlns="http://www.v8.1c.ru/ssl/contactinfo" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<AdrTag1 xsi:type="Adr">Example1</AdrTag1>
<AdrTag2>Example2</AdrTag2>
</Address>
</item>
<item>
<ContactRecordType>CCCCC</ContactRecordType>
<ContactValue>ValueCCCCC</ContactValue>
</item>
</ContactRecords>
</Organisation>
</Organisations>
我正在编写一个处理 ContactRecords 节点的 XSLT:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:element name="Organisations">
<xsl:for-each select="Organisations/Organisation">
<xsl:element name="{name(.)}">
<xsl:for-each select="*[not(name()='ContactRecords')]">
<xsl:copy select="*">
<xsl:value-of select="normalize-space(.)"/>
</xsl:copy>
</xsl:for-each>
<xsl:for-each select="ContactRecords/item">
<xsl:choose>
<xsl:when test="Address">
<h2>mooooooooooooo</h2>
</xsl:when>
<xsl:otherwise>
<h2>dooooooooooooo</h2>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:transform>
我现在得到以下结果:
<h2>dooooooooooooo</h2>
<h2>dooooooooooooo</h2>
<h2>dooooooooooooo</h2>
我希望收到:
<h2>mooooooooooooo</h2>
<h2>mooooooooooooo</h2>
<h2>dooooooooooooo</h2>
我究竟做错了什么?
如果我用文字解释算法,那么我需要以下内容:如果 item 元素中有 Address 元素,则执行逻辑 1。如果 item 元素中没有 Address 元素,则执行逻辑 2。
如果我们用伪代码描述算法,那么这就是:
if (item.includes(Address)) {
do logic #1
} else {
do logic #2
}
UPD1:更新了 XML 和 XSLT 代码 UPD2:在标签地址中添加命名空间(可能原因在其中)
解决方案
该Address
元素位于命名空间中,因此您的测试:
<xsl:when test="Address">
false
每次返回。尝试这种方式(最小化到当前问题):
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.v8.1c.ru/ssl/contactinfo"
exclude-result-prefixes="ns0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Organisations">
<Organisations>
<xsl:for-each select="Organisation">
<xsl:copy>
<!-- omitted -->
<xsl:for-each select="ContactRecords/item">
<h2>
<xsl:choose>
<xsl:when test="ns0:Address">mooooooooooooo</xsl:when>
<xsl:otherwise>dooooooooooooo</xsl:otherwise>
</xsl:choose>
</h2>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</Organisations>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- signal-processing - CWT 幅度是什么意思?
- json - 有条件地排除一个兄弟对象属性取决于 JSON Schema 中其他兄弟对象的属性
- hive - Apache Hive:重命名数组类型的列
> - pyspark - 您似乎正在尝试从广播变量、操作或转换中引用 SparkContext
- c++ - 命名空间中的 unordered_map 没有跨源文件“保存”其数据
- python - 如何在 cmd 中运行此代码以查看可视化输出?
- c - 在循环中调用 pthread_cancel() 显示增加 MEM 使用率
- spring - SpringBoot RestTemplate GET 返回空正文
- sql - 访问 SQL 查询以使用条件删除除最后一个最大日期之外的所有日期
- java - 保存 Runnable 实现的函数