Add sub child of a child to a element

浪子不回头ぞ 提交于 2019-12-25 07:28:40

问题


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.


回答1:


This transform will do what you need. It is a basic identity transform with a special case for child elements, which are simply replaced by all their children.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="child">
        <xsl:copy-of select="*"/>
    </xsl:template>

</xsl:stylesheet>

output

<parent>
   <header>
      <left/>
      <right/>
   </header>
   <subchild/>
   <subchild/>
   <subchild/>
   <subchild/>
   <subchild/>
   <subchild/>
   <subchild/>
   <subchild/>
</parent>



回答2:


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>


来源:https://stackoverflow.com/questions/16432820/add-sub-child-of-a-child-to-a-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!