Xml 某些数据在提取后丢失
当我根据这些信息为spiliting文件编写xslt时,我有一个xml文件要spilit,但是在spiliting文件之后,一些不是文件祖先的元素在转换中丢失了 提前谢谢 输入XMLXml 某些数据在提取后丢失,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,当我根据这些信息为spiliting文件编写xslt时,我有一个xml文件要spilit,但是在spiliting文件之后,一些不是文件祖先的元素在转换中丢失了 提前谢谢 输入XML <?xml version="1.0" encoding="UTF-8"?> <parent id="1"> <misc1>**Some DATA**</misc1> <misc2>**Some DATA**</misc2>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<misc1>**Some DATA**</misc1>
<misc2>**Some DATA**</misc2>
<child1 id="2">
<child1-misc1>**Some DATA**</child1-misc1>
<file name="A.xml"><a id="11">aaa</a></file>
<file name="B.xml"><b id="21">bbb</b></file>
</child1>
<child2 id="3">
<child2-misc1>**Some DATA**</child2-misc1>
<child2-misc2>**Some DATA**</child2-misc2>
<file name="C.xml"><c id="31">ccc</c></file>
<file name="D.xml"><d id="41">ddd</d></file>
</child2>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="file">
<xsl:variable name="name" select="@name"/>
<xsl:result-document method="xml" href="{$name}" indent="yes">
<xsl:call-template name="spilit">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="element" select="root()"/>
</xsl:call-template>
</xsl:result-document>
</xsl:template>
<xsl:template name="spilit">
<xsl:param name="name"/>
<xsl:param name="element"/>
<xsl:for-each select="$element[descendant-or-self::file[@name eq $name]]">
<xsl:copy>
<xsl:choose>
<xsl:when test="self::file">
<xsl:copy-of select="node()"></xsl:copy-of>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="@*"></xsl:copy-of>
<xsl:call-template name="spilit">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="element" select="child::*[descendant-or-self::file[@name eq $name]]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
**一些数据**
**一些数据**
**一些数据**
B.xml
bbb
C.xml
ccc
D.xml
ddd
预期产出
A.xml
**一些数据**
**一些数据**
**一些数据**
aaa
B.xml
**一些数据**
**一些数据**
**一些数据**
bbb
C.xml
**一些数据**
**一些数据**
**一些数据**
**一些数据**
ccc
D.xml
**一些数据**
**一些数据**
**一些数据**
**一些数据**
ddd
我认为您只需要额外的一行来复制当前节点中没有文件的子节点
<xsl:copy-of select="*[not(descendant-or-self::file)]" />
你能解释一下哪些规则决定哪些元素必须被复制,哪些元素不能被复制吗?这是否取决于元素的名称和/或位置?所有不包含文件元素的当前文件元素的祖先和前面的元素都必须出现在文件中。
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<child1 id="2">
<file><b id="21">bbb</b></file>
</child1>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<child2 id="3">
<file><c id="31">ccc</c></file>
</child2>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<child2 id="3">
<file><d id="41">ddd</d></file>
</child2>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<misc1>**Some DATA**</misc1>
<misc2>**Some DATA**</misc2>
<child1 id="2">
<child1-misc1>**Some DATA**</child1-misc1>
<file><a id="11">aaa</a></file>
</child1>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<misc1>**Some DATA**</misc1>
<misc2>**Some DATA**</misc2>
<child1 id="2">
<child1-misc1>**Some DATA**</child1-misc1>
<file><b id="21">bbb</b></file>
</child1>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<misc1>**Some DATA**</misc1>
<misc2>**Some DATA**</misc2>
<child2 id="3">
<child2-misc1>**Some DATA**</child2-misc1>
<child2-misc2>**Some DATA**</child2-misc2>
<file><c id="31">ccc</c></file>
</child2>
</parent>
<?xml version="1.0" encoding="UTF-8"?>
<parent id="1">
<misc1>**Some DATA**</misc1>
<misc2>**Some DATA**</misc2>
<child2 id="3">
<child2-misc1>**Some DATA**</child2-misc1>
<child2-misc2>**Some DATA**</child2-misc2>
<file><d id="41">ddd</d></file>
</child2>
</parent>
<xsl:copy-of select="*[not(descendant-or-self::file)]" />
<xsl:template name="spilit">
<xsl:param name="name"/>
<xsl:param name="element"/>
<xsl:for-each select="$element[descendant-or-self::file[@name eq $name]]">
<xsl:copy>
<xsl:choose>
<xsl:when test="self::file">
<xsl:copy-of select="node()"></xsl:copy-of>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="@*"></xsl:copy-of>
<xsl:copy-of select="*[not(descendant-or-self::file)]" />
<xsl:call-template name="spilit">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="element" select="child::*[descendant-or-self::file[@name eq $name]]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:for-each>
</xsl:template>