结合 xml、xpath 或 xquery [英] combining xml, xpath or xquery
本文介绍了结合 xml、xpath 或 xquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<main>
<parent>
<parentId>parentId1</parentId>
<aParentField>aParentValue
</aParentField>
</parent>
<child>
<parentId>parentId1</parentId>
<aChildField>aChildValue
</aChildField>
</child>
</main>
我是 XML 的新手,并试图使用 ID 作为要组合的参数来组合 A 和 B,所以结果应该是这样的:
I am new to XML and was trying to combine A and B using the ID as a parameter to combine, so the result should be like:
<main>
<parent>
<parentId>parentId1</parentId>
<aParentField>aParentValue
</aParentField>
<child>
<aChildField>aChildValue
</aChildField>
</child>
</parent>
</main>
可以使用什么以及如何使用?
What can be used and how ?
推荐答案
这个例子有点含糊.假设您的输入可以有多个 parent
节点,每个节点链接到多个 child
节点,我建议您使用 key 解决交叉引用:
The example is a little ambiguous. Assuming your input can have multiple parent
nodes, each linked to multiple child
nodes, I would suggest you use a key to resolve the cross-references:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="child" match="child" use="parentId" />
<xsl:template match="/main">
<xsl:copy>
<xsl:apply-templates select="parent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="parent">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates select="key('child', parentId)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="child">
<xsl:copy>
<xsl:copy-of select="*[not(self::parentId)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这篇关于结合 xml、xpath 或 xquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文