作为我工作流程的一部分,我收到 xml 文件,有时特定元素为空,这会导致系统出现问题,这是一个示例输入文件:
<?xml version="1.0" encoding="UTF-8"?>
<CompositionPlaylist xmlns="http://www.xml.com/20040511#">
<Id>urn:uuid:129de837-6eed-40d4-a635-db58380dfd9e</Id>
<AnnotationText>Title</AnnotationText>
<IssueDate>2014-09-22T19:18:56.001+01:00</IssueDate>
<Issuer>issuer</Issuer>
<Creator>Creator Name</Creator>
<ContentTitleText>Content Title</ContentTitleText>
<ContentKind>feature</ContentKind>
<ContentVersion>
<Id>urn:uuid:e52a4259-ecad-4630-be51-cf13f95d8b82</Id>
<LabelText>Title of the media</LabelText>
</ContentVersion>
<RatingList/>
<ReelList>
<Reel>
<Id>urn:uuid:1b840b2b-7526-4597-ae2d-81994f8e9867</Id>
<AnnotationText>FeG_r1_new</AnnotationText>
<AssetList>
<MainPicture>
<Id>urn:uuid:e6ac03bd-65a5-4dab-bb24-682c6b5bf61a</Id>
<EditRate>24 1</EditRate>
<IntrinsicDuration>25965</IntrinsicDuration>
<EntryPoint>0</EntryPoint>
<Duration>25965</Duration>
<Hash>3VTtxrGsVs1AsE4JXU12wdF/U0o=</Hash>
<FrameRate>24 1</FrameRate>
<ScreenAspectRatio>1.77</ScreenAspectRatio>
</MainPicture>
<MainSound>
<Id>urn:uuid:1202f14c-13ce-4585-8a3f-fd489b6c6bac</Id>
<EditRate>24 1</EditRate>
<IntrinsicDuration>25990</IntrinsicDuration>
<EntryPoint>0</EntryPoint>
<Duration>25965</Duration>
<Hash>cU4riXDJwfsQ9rL7tkd0c8nZgWw=</Hash>
<Language>und</Language>
</MainSound>
</AssetList>
</Reel>
有时元素“ContentVersion”是空的,如下所示:
<ContentVersion>
<LabelText/>
</ContentVersion
用“虚拟”元素替换该元素就可以了,就像这样
<ContentVersion>
<Id>urn:uuid:11111111-1111-1111-1111-111111111111</Id>
<LabelText>DUMMY</LabelText>
</ContentVersion>
采取的步骤 尝试过 XSLT 转换,但找不到用 cild 替换完整元素的方法
当您说
ContentVersion
不能为空时,我不确定您到底想要测试什么,但是例如,如果您的测试是它必须有一个非空 LabelText,那么您的 XSLT 转换就可以了
<xsl:template match="ContentVersion[not(LabelText) or LabelText = '']">
<ContentVersion>
<Id>urn:uuid:11111111-1111-1111-1111-111111111111</Id>
<LabelText>DUMMY</LabelText>
</ContentVersion>
</xsl:template>
如果您对“空”的定义不同,请根据您的口味调整谓词。