将子元素的子元素添加到元素 [英] Add sub child of a child to a element
问题描述
header
和 child
元素是 parent
的子元素.我可以保留 header
及其子元素,而 child
的其余 subchild
节点将添加到新元素中吗?
<标题><左></左><右></右></标题><孩子><subchild></subchild><subchild></subchild><subchild></subchild></孩子><孩子><subchild></subchild><subchild></subchild></孩子><孩子><subchild></subchild><subchild></subchild><subchild></subchild></孩子></父母>
有什么办法可以生成以下输出吗?
<标题><左></左><右></右></标题><元素><subchild></subchild><subchild></subchild><subchild></subchild><subchild></subchild><subchild></subchild><subchild></subchild><subchild></subchild><subchild></subchild></元素></父母>
创建一个新元素并传递child的所有子子节点.
对于我们的 XML 文件,您可以使用这个 XSLT 模式来获得像 wnat 一样的结果(使用 HTML 在浏览器中看起来很漂亮)
<xsl:template match="/"><头><title></title>头部><身体><xsl:apply-templates/></html></xsl:模板><xsl:template match="parent"><div id="父"><标题><div id="left"><xsl:value-of select="header/left/text()"/></div><div id="right"><xsl:value-of select="header/right/text()"/></div></标题><ul id="元素"><xsl:for-each select="child/subchild"><li><xsl:value-of select="text()"/></li></xsl:for-each>
</xsl:模板></xsl:stylesheet>
结果如下:
左方块右块子子 1 - 子子子 1子子 1 - 子子子 2子 1 - 子 3子子 2 - 子子子 1子子 2 - 子子子 2孩子 3 - 子孩子 1孩子 3 - 子孩子 2子 3 - 子 3
对于您问题中的完整生成结果 - 使用此 XSLT 代码:
<xsl:template match="/"><xsl:apply-templates/></xsl:模板><xsl:template match="parent"><父母><xsl:value-of select="header/node()"/><元素><xsl:for-each select="child/subchild"><xsl:value-of select="node()"/></xsl:for-each></元素></父母></xsl:模板></xsl:stylesheet>
The header
and child
elements are the children of parent
. Can I keep header
and its children as it is and the remaining subchild
nodes of child
are to be added to a new element?
<parent>
<header>
<left></left>
<right></right>
</header>
<child>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
</child>
<child>
<subchild></subchild>
<subchild></subchild>
</child>
<child>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
</child>
</parent>
Is there any way where I can generate the below output?
<parent>
<header>
<left></left>
<right></right>
</header>
<element>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
<subchild></subchild>
</element>
</parent>
Make a new element and pass remaining all sub childs nodes of child.
For y our XML file you may use this XSLT schema for get result like you wnat (use HTML for pretty look in browser)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title></title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="parent">
<div id="parent">
<header>
<div id="left"><xsl:value-of select="header/left/text()"/></div>
<div id="right"><xsl:value-of select="header/right/text()"/></div>
</header>
<ul id="elements">
<xsl:for-each select="child/subchild">
<li><xsl:value-of select="text()"/></li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
</xsl:stylesheet>
Result looks like this:
Left block
Right Block
Child 1 - Subchild 1
Child 1 - Subchild 2
Child 1 - Subchild 3
Child 2 - Subchild 1
Child 2 - Subchild 2
Child 3 - Subchild 1
Child 3 - Subchild 2
Child 3 - Subchild 3
For complite make result as in you question - use this XSLT code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="parent">
<parent>
<xsl:value-of select="header/node()"/>
<elements>
<xsl:for-each select="child/subchild">
<xsl:value-of select="node()"/>
</xsl:for-each>
</elements>
</parent>
</xsl:template>
</xsl:stylesheet>
这篇关于将子元素的子元素添加到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!