XML问题将过滤后的数据与新父节点中的节点一起复制

时间:2016-06-28 06:32:24

标签: xml xslt

下面是我想要输出的输入xml:

<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        <id>A</id>
        <name>jscvjab</name>
        <description>zcm,ab</description>
        <student>
            <id>1</id>
            <refObjectId>m9</refObjectId>
            <subject>
                <id>25</id>
                <name>science</name>
                <firstname>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>kullu</fragId>
                            <znjdvgsc>nSVCm</znjdvgsc>
                            <zmdcb>zmbvc</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>2</id>
            <refObjectId>m5</refObjectId>
            <subject>
                <id>92</id>
                <name>commerce</name>
                <firstname>
                    <textContent>
                        <Id>m9</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Contents</body>
                        </text>
                        <Key>Slide</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Jerry</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>zxZXVj</fragId>
                            <znjdvgsc>kjsdgfjkvnwsa</znjdvgsc>
                            <zmdcb>kadbvscjkna</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <students>
        <id>B</id>
        <name>jzvxcbhjnba</name>
        <description>mjZSVxcj</description>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>4</id>
            <refObjectId>m3</refObjectId>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>sdbnfn</fragId>
                            <znjdvgsc>masdjkljk</znjdvgsc>
                            <zmdcb>msjkldbhfjkn</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>
</class>

我正在使用的xslt如下:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="students">
<xsl:copy>
<xsl:apply-templates select="*" />
</xsl:copy>
<Employee>
<xsl:for-each select="student">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="subject" mode="Employee" />
</xsl:copy>
</xsl:for-each>
</Employee>
</xsl:template>
<xsl:template match="subject">
<xsl:copy>
<xsl:copy-of select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="subject" mode="Employee">
<xsl:copy>
<xsl:copy-of select="@*|node()[not(self::lastname)]" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

这样生成的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        <id>A</id>
        <name>jscvjab</name>
        <description>zcm,ab</description>
        <student>
            <id>1</id>
            <refObjectId>m9</refObjectId>
            <subject>
                <id>25</id>
                <name>science</name>
                <firstname>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>kullu</fragId>
                            <znjdvgsc>nSVCm</znjdvgsc>
                            <zmdcb>zmbvc</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>2</id>
            <refObjectId>m5</refObjectId>
            <subject>
                <id>92</id>
                <name>commerce</name>
                <firstname>
                    <textContent>
                        <Id>m9</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Contents</body>
                        </text>
                        <Key>Slide</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Jerry</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>zxZXVj</fragId>
                            <znjdvgsc>kjsdgfjkvnwsa</znjdvgsc>
                            <zmdcb>kadbvscjkna</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <Employee>
        <student>
            <subject>
                <id>25</id>
                <name>science</name>
                <firstname>
                    <isDisplay>true</isDisplay>
                </firstname>
            </subject>
        </student>
        <student>
            <subject>
                <id>92</id>
                <name>commerce</name>
                <firstname>
                    <textContent>
                        <Id>m9</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Contents</body>
                        </text>
                        <Key>Slide</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Jerry</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
            </subject>
        </student>
    </Employee>
    <students>
        <id>B</id>
        <name>jzvxcbhjnba</name>
        <description>mjZSVxcj</description>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>4</id>
            <refObjectId>m3</refObjectId>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>sdbnfn</fragId>
                            <znjdvgsc>masdjkljk</znjdvgsc>
                            <zmdcb>msjkldbhfjkn</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <Employee>
        <student>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
            </subject>
        </student>
        <student>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
            </subject>
        </student>
    </Employee>
    <faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>
    <Employee>
        <students>
            <id>A</id>
            <name>jscvjab</name>
            <description>zcm,ab</description>
            <student>
                <id>1</id>
                <refObjectId>m9</refObjectId>
                <subject>
                    <id>25</id>
                    <name>science</name>
                    <firstname>
                        <isDisplay>true</isDisplay>
                    </firstname>
                </subject>
            </student>
            <student>
                <id>2</id>
                <refObjectId>m5</refObjectId>
                <subject>
                    <id>92</id>
                    <name>commerce</name>
                    <firstname>
                        <textContent>
                            <Id>m9</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Contents</body>
                            </text>
                            <Key>Slide</Key>
                        </textContent>
                        <textContent>
                            <Id>m95</Id>
                            <title xfa:contentType="text/html">
                                <body>SubTitle</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Jerry</body>
                            </text>
                            <Key>SubTitle</Key>
                        </textContent>
                        <isDisplay>false</isDisplay>
                    </firstname>
                </subject>
            </student>
        </students>
        <Employee>
            <student>
                <subject>
                    <id>25</id>
                    <name>science</name>
                    <firstname>
                        <isDisplay>true</isDisplay>
                    </firstname>
                </subject>
            </student>
            <student>
                <subject>
                    <id>92</id>
                    <name>commerce</name>
                    <firstname>
                        <textContent>
                            <Id>m9</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Contents</body>
                            </text>
                            <Key>Slide</Key>
                        </textContent>
                        <textContent>
                            <Id>m95</Id>
                            <title xfa:contentType="text/html">
                                <body>SubTitle</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Jerry</body>
                            </text>
                            <Key>SubTitle</Key>
                        </textContent>
                        <isDisplay>false</isDisplay>
                    </firstname>
                </subject>
            </student>
        </Employee>
        <students>
            <id>B</id>
            <name>jzvxcbhjnba</name>
            <description>mjZSVxcj</description>
            <student>
                <id>3</id>
                <refObjectId>m8</refObjectId>
                <subject>
                    <id>91</id>
                    <name>humanities</name>
                    <firstname>
                        <textContent>
                            <Id>m954</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Current</body>
                            </text>
                            <Key>Title</Key>
                        </textContent>
                        <textContent>
                            <Id>m9542</Id>
                            <title xfa:contentType="text/html">
                                <body>SubTitle</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Sr</body>
                            </text>
                            <Key>SubTitle</Key>
                        </textContent>
                        <isDisplay>false</isDisplay>
                    </firstname>
                </subject>
            </student>
            <student>
                <id>4</id>
                <refObjectId>m3</refObjectId>
                <subject>
                    <id>75</id>
                    <name>PCM</name>
                    <firstname>
                        <textContent>
                            <Id>j4423</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>dfzxsdbjchA</body>
                            </text>
                            <Key>zdbgcfjkbna</Key>
                        </textContent>
                        <textContent>
                            <Id>m95</Id>
                            <title xfa:contentType="text/html">
                                <body>zdjbhfjkcda</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>sdjkhzjk</body>
                            </text>
                            <Key>mzsdjkbjk</Key>
                        </textContent>
                        <isDisplay>true</isDisplay>
                    </firstname>
                </subject>
            </student>
        </students>
        <Employee>
            <student>
                <subject>
                    <id>91</id>
                    <name>humanities</name>
                    <firstname>
                        <textContent>
                            <Id>m954</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Current</body>
                            </text>
                            <Key>Title</Key>
                        </textContent>
                        <textContent>
                            <Id>m9542</Id>
                            <title xfa:contentType="text/html">
                                <body>SubTitle</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>Sr</body>
                            </text>
                            <Key>SubTitle</Key>
                        </textContent>
                        <isDisplay>false</isDisplay>
                    </firstname>
                </subject>
            </student>
            <student>
                <subject>
                    <id>75</id>
                    <name>PCM</name>
                    <firstname>
                        <textContent>
                            <Id>j4423</Id>
                            <title xfa:contentType="text/html">
                                <body>Title</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>dfzxsdbjchA</body>
                            </text>
                            <Key>zdbgcfjkbna</Key>
                        </textContent>
                        <textContent>
                            <Id>m95</Id>
                            <title xfa:contentType="text/html">
                                <body>zdjbhfjkcda</body>
                            </title>
                            <text xfa:contentType="text/html">
                                <body>sdjkhzjk</body>
                            </text>
                            <Key>mzsdjkbjk</Key>
                        </textContent>
                        <isDisplay>true</isDisplay>
                    </firstname>
                </subject>
            </student>
        </Employee>
    </Employee>
</class>

所需的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
<students>
<id>A</id>
<name>jscvjab</name>
<description>zcm,ab</description>
<student>
<id>1</id>
<refObjectId>m9</refObjectId>
<subject>
<id>25</id>
<name>science</name>
<firstname>
<isDisplay>true</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>kullu</fragId>
<znjdvgsc>nSVCm</znjdvgsc>
<zmdcb>zmbvc</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
<student>
<id>2</id>
<refObjectId>m5</refObjectId>
<subject>
<id>92</id>
<name>commerce</name>
<firstname>
<textContent>
<Id>m9</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Contents</body>
</text>
<Key>Slide</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Jerry</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>zxZXVj</fragId>
<znjdvgsc>kjsdgfjkvnwsa</znjdvgsc>
<zmdcb>kadbvscjkna</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
</students>


<students>
<id>B</id>
<name>jzvxcbhjnba</name>
<description>mjZSVxcj</description>
<student>
<id>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>aksbgdk</fragId>
<znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
<zmdcb>slzdhfksjfml</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
<student>
<id>4</id>
<refObjectId>m3</refObjectId>
<subject>
<id>75</id>
<name>PCM</name>
<firstname>
<textContent>
<Id>j4423</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>dfzxsdbjchA</body>
</text>
<Key>zdbgcfjkbna</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>zdjbhfjkcda</body>
</title>
<text xfa:contentType="text/html">
<body>sdjkhzjk</body>
</text>
<Key>mzsdjkbjk</Key>
</textContent>
<isDisplay>true</isDisplay>
</firstname>
<lastname>
<Detail>
<lastNameDetail>
<fragId>sdbnfn</fragId>
<znjdvgsc>masdjkljk</znjdvgsc>
<zmdcb>msjkldbhfjkn</zmdcb>
</lastNameDetail>
</Detail>
</lastname>
</subject>
</student>
</students>


<faculty>
<configuration>
<Type>Output</Type>
<Key>Font</Key>
<Value>10pt</Value>
</configuration>
<configuration>
<Type>Out</Type>
<Key>Text</Key>
<Value>127,127,127</Value>
</configuration>
<configuration>
<Type>put</Type>
<Key>Slide</Key>
<Value>18pt</Value>
</configuration>
<configuration>
<Type>utp</Type>
<Key>Slide</Key>
<Value>127</Value>
</configuration>
</faculty>
<Info />
<Dean>
<name>zcdfjkaqbkd</name>
</Dean>



<Employee>

<students>
<id>A</id>
<name>jscvjab</name>
<description>zcm,ab</description>
<student>
<id>1</id>
<refObjectId>m9</refObjectId>
<subject>
<id>25</id>
<name>science</name>
<firstname>
<isDisplay>true</isDisplay>
</firstname>
</subject>
</student>
<student>
<id>2</id>
<refObjectId>m5</refObjectId>
<subject>
<id>92</id>
<name>commerce</name>
<firstname>
<textContent>
<Id>m9</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Contents</body>
</text>
<Key>Slide</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Jerry</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
</subject>
</student>
</students>


<students>
<id>B</id>
<name>jzvxcbhjnba</name>
<description>mjZSVxcj</description>
<student>
<id>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<firstname>
<textContent>
<Id>m954</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>Current</body>
</text>
<Key>Title</Key>
</textContent>
<textContent>
<Id>m9542</Id>
<title xfa:contentType="text/html">
<body>SubTitle</body>
</title>
<text xfa:contentType="text/html">
<body>Sr</body>
</text>
<Key>SubTitle</Key>
</textContent>
<isDisplay>false</isDisplay>
</firstname>
</subject>
</student>
<student>
<id>4</id>
<refObjectId>m3</refObjectId>
<subject>
<id>75</id>
<name>PCM</name>
<firstname>
<textContent>
<Id>j4423</Id>
<title xfa:contentType="text/html">
<body>Title</body>
</title>
<text xfa:contentType="text/html">
<body>dfzxsdbjchA</body>
</text>
<Key>zdbgcfjkbna</Key>
</textContent>
<textContent>
<Id>m95</Id>
<title xfa:contentType="text/html">
<body>zdjbhfjkcda</body>
</title>
<text xfa:contentType="text/html">
<body>sdjkhzjk</body>
</text>
<Key>mzsdjkbjk</Key>
</textContent>
<isDisplay>true</isDisplay>
</firstname>
</subject>
</student>
</students>
</Employee>
</class>

请帮助......我有这个问题并且无法解决它很长时间....我试过我的上一个问题,但我没有找到任何人可以解决它基本上我想在现有的xml中创建一个名为Employee的新节点,它应该包含所有数据,直到firstname节点,然后它应该关闭所有数据,然后所有节点应该自己关闭,但问题是我我面临的是我能够创建节点employe但是在每个学生节点之后我需要在xml的最后一个中进行,而我面临的另一个问题是......它不显示数据,对于{ {1}}和<students>节点,在新的<student>节点中如此生成休息所有正在按照我的要求获取..... pleasseeee helpppppp ....我真的需要一些帮助.....我一直在努力,但无法解决它......谢谢

1 个答案:

答案 0 :(得分:0)

从前一个问题中获取的XSLT的主要问题是,没有提到有多个students节点。 XSLT为每个Employee节点添加Students,而不只是一个。

如果你在一系列规则中表达了你的逻辑,那将会有所帮助

  1. 按原样复制现有项目
  2. Employee
  3. 的最后一个孩子之后附加Class
  4. Employee副本现有students
  5. 复制新lastname
  6. 下的节点时,请勿复制Employee

    要解决此问题,请使用与父class元素匹配的模板,然后选择要复制的所有现有子节点,然后添加Employee元素。

      <xsl:template match="class">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
          <Employee>
            <xsl:apply-templates select="students" mode="employee"/>
          </Employee>
        </xsl:copy>
      </xsl:template>
    

    此处的mode用于表示您以不同的方式匹配现有节点,以区别于将其与已复制的现有students进行复制。这意味着您需要一个单独的身份模板副本来保留模式

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

    然后,您可以使用此模式删除姓氏

    <xsl:template match="lastname" mode="employee" />
    

    试试这个XSLT,它确实产生与你想要的输出相同的(带缩进)

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" indent="yes"></xsl:output>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="@*|node()" mode="employee">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" mode="employee"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="class">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
          <Employee>
            <xsl:apply-templates select="students" mode="employee"/>
          </Employee>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="lastname" mode="employee" />
    </xsl:stylesheet>