问题描述
我有一个这样的 xml 文档:
I have an xml document like this one:
<dataContainerList>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
<Container>
<LocalDataContainer>
<file>InputFile2</file>
</LocalDataContainer>
</Container>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
</dataContainerList>
并且我想用文件 name = InputFile3
意思:我想创建一个新的dataContainereList,其中只有第一个和第三个容器.
Means: I want to create a new dataContainereList with the only the first and the third container.
用一个 XPATH1.0
命令可以吗?不能使用 XSL
或其他东西,只是简单的 XPath
这应该适用于任意 dataContainerList.
Is that possible with one XPATH1.0
command? Cannot use XSL
or something, just plain XPath
and this should work with arbitrary dataContainerList.
推荐答案
从您的描述来看,与其过滤掉名为InputFile3"的Containers
,不如说您实际上想要选择 这样的容器.
From your description, it sounds like rather than filter out Containers
named "InputFile3", you actually want to select such containers.
您可以使用 XSLT 中标识转换的简单变体将所有内容复制到输出除了那些未命名为InputFile3"的Container
.
You can use a simple variation of the identity transformation in XSLT to copy everything to the output except those Containers
not named "InputFile3".
此输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Containers>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
<Container>
<LocalDataContainer>
<file>InputFile2</file>
</LocalDataContainer>
</Container>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
</Containers>
应用于此 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Container[LocalDataContainer/file != 'InputFile3']"/>
</xsl:stylesheet>
将产生此输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Containers>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
<Container>
<LocalDataContainer>
<file>InputFile3</file>
</LocalDataContainer>
</Container>
</Containers>
这篇关于XPATH 1.0 根据条件从原始文档中构造一个新的 xml 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!