如何修复这两个xml文件的连接?
我想创建两个xml文件。 以下是input1.xml:如何修复这两个xml文件的连接?,xml,xslt,merge,concatenation,Xml,Xslt,Merge,Concatenation,我想创建两个xml文件。 以下是input1.xml: <schema> <sequence> <section id="xxx"> <nodeA id="a"> <fruit id="small"> <orange id="x" method="create">
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
</nodeB>
</section>
</sequence>
</schema>
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeA id="b"> <!-- I'm missing this node -->
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="c"> <!-- I'm missing this node -->
<dog id="small">
<terrier id="x" method="delete" />
</dog>
</nodeB>
</section>
</sequence>
</schema>
橙色
2000
黑色
这里是input2.xml
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete" />
</nodeA>
<nodeA id="b">
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="b">
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeB id="c">
<dog id="small">
<terrier id="x" method="delete" />
</dog>
</nodeB>
</section>
</sequence>
</schema>
绿色
黄色的
2001
白色
我的输出:
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
</section>
</sequence>
</schema>
橙色
2000
绿色
黑色
白色
而预期输出为:
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
</nodeB>
</section>
</sequence>
</schema>
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeA id="b"> <!-- I'm missing this node -->
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="c"> <!-- I'm missing this node -->
<dog id="small">
<terrier id="x" method="delete" />
</dog>
</nodeB>
</section>
</sequence>
</schema>
橙色
2000
绿色
黑色
白色
黄色的
2001
XSLT文件如下所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://a.com">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" method="xml" />
<xsl:param name="input2"/>
<xsl:variable name="to-merge" select="document($input2)" />
<xsl:function name="a:id">
<xsl:param name="ctx"/>
<xsl:value-of select="concat($ctx/local-name(), $ctx/@id)"/>
</xsl:function>
<xsl:key name="match" match="/schema/sequence/section/*" use="a:id(.)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[count(. | key('match', a:id(.))) = count(key('match', a:id(.)))]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:variable name="id" select="a:id(.)"/>
<xsl:for-each select="$to-merge">
<xsl:apply-templates select="key('match', $id)/*"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如何修改xslt文件以生成所需的输出?这里的关键是保持节点的顺序。如果文件1中存在节点,我们将其合并,如果不是,则根据它们的显示顺序将其放在底部
非常感谢
John既然您已经在使用XSLT 2.0,我就不必为键操心了,而是为每个组使用
xsl:for
,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="url2" select="'test2012051302.xml'"/>
<xsl:variable name="doc2" select="document($url2)"/>
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="sequence">
<xsl:copy>
<xsl:for-each-group select="section | $doc2/schema/sequence/section" group-by="@id">
<section id="{current-grouping-key()}">
<xsl:for-each-group select="current-group()/*" group-by="concat(local-name(), '|', @id)">
<xsl:copy>
<xsl:apply-templates select="@*, *, (current-group() except .)/*"/>
</xsl:copy>
</xsl:for-each-group>
</section>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当我使用Saxon 9.4他将上面的样式表应用到输入样本时,我得到了结果
<?xml version="1.0" encoding="UTF-8"?>
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeA id="b">
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="c">
<dog id="small">
<terrier id="x" method="delete"/>
</dog>
</nodeB>
</section>
</sequence>
</schema>
橙色
2000
绿色
黑色
白色
黄色的
2001
我想这正是你想要的。谢谢,它工作得很好!对于
如果有更多属性而不仅仅是id,我如何打印它们呢?您可以根据需要尝试..
或..
,而不是..
(例如,第一个建议允许您在需要时使用其他模板转换属性,第二个建议仅复制属性)。您在问题中使用的XML既没有意义,也基于错误的设计。我将始终忽略使用此类XML的问题。特别是,它太长,属性没有意义,特定元素的名称是任意的且不重复,有多个元素具有相同的id
。这使得它非常困难推断共同结构和/或关系。我希望你能改进未来问题的示例,以便