xml - 如果包含字符串、为空或不存在,XSLT 2.0 替换元素值
问题描述
我正在尝试对如下所示的 XML 文档执行转换:
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>EX</SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
如果 SubModel 元素包含有用的数据,我想维护它。但是,如果元素包含'-',''。或不存在,我想用“NA”替换该值。
例如:
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>_</SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel></SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
</Vehicle>
</Vehicles>
</AutoInsurance>
都应该转变为
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>NA</SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
任何帮助是极大的赞赏!
解决方案
您可以使用应导致“NA”的字符串声明一个变量。
并像这样使用它:
<xsl:stylesheet version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="wrongStrings" as="xs:string*" select="'-','_','.'" />
<xsl:template match="Vehicle">
<xsl:apply-templates select="*[not(self::SubModel)]"/>
<xsl:variable name="subModelContent" as="xs:string" select="if(SubModel/text()) then SubModel/text() else '-'"/>
<SubModel>
<xsl:value-of select="if($subModelContent=$wrongStrings) then 'NA' else SubModel/text()"/>
</SubModel>
</xsl:template>
</xsl:stylesheet>
当你有这个 xml 时:
<?xml version='1.0' encoding='UTF-8'?>
<root>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>_</SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel></SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
</Vehicle>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Vehicle>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>ABC</SubModel>
</Vehicle>
</Vehicles>
</AutoInsurance>
</root>
结果将是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<AutoInsurance>
<Vehicles>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>NA</SubModel>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>NA</SubModel>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>NA</SubModel>
</Vehicles>
</AutoInsurance>
<AutoInsurance>
<Vehicles>
<Id>1</Id>
<Make>HONDA</Make>
<Model>ACCORD</Model>
<SubModel>ABC</SubModel>
</Vehicles>
</AutoInsurance>
</root>
推荐阅读
- r - 在闪亮服务器中使用 system() 运行系统命令
- python - 如何在 pybind11 中使用 Eigen::Ref?
- c - C,架构 x86_64 的未定义符号
- python - 将字符串转换为二进制时出现 Python 错误
- python - 是否可以使用 CNN 对同一对象的图像块进行分类,但区域不同?
- ruby-on-rails - 验证不在同一个表上的列组合
- c# - 处理大量类:我可以在文件夹中任意嵌套 C# 文件吗?
- python-3.x - Point 类和 Circle 类之间的继承
- html - 正文溢出:滚动未按预期工作
- asp.net-mvc - Asp.net C# DB2 连接字符串