我有以下输入xml:
<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
<students>
------
------
------
------
</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>
<friend>hercules</friend>
<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>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<friend>aladin</friend>
<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>3</id>
<refObjectId>m8</refObjectId>
<subject>
<id>91</id>
<name>humanities</name>
<friend>aladin</friend>
<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>
<friend>hercules</friend>
<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>
<students>
--------
-------
---------
--------
</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>
我想要做的是每个父节点的子节点“id”(可能存在或不存在于每个树中)应该成为其父节点的属性,并且应该永久地从xml中删除id节点。
我正在使用xslt 1.0,下面是我的xsl文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="1.0">
<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="id" />
<xsl:template match="subject">
<subject id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</subject>
</xsl:template>
<xsl:template match="students">
<students id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</students>
</xsl:template>
<xsl:template match="student">
<student id="{id}">
<xsl:copy>
<xsl:apply-templates select="child::node()" />
</xsl:copy>
</student>
</xsl:template>
</xsl:stylesheet>
我的xslt在做什么,它正在根据需要在父节点中创建属性,它正在根据需要删除id节点,但它正在创建父节点的新副本 我现在的输出附在此:
<?xml version="1.0" encoding="UTF-8"?><class xmlns:xfa="soommmeee-tteexxxxtttttt">
<students id="A">
<students>
-----
-----
------
</students>
</students>
<students id="B">
<students>
<name>jzvxcbhjnba</name>
<description>mjZSVxcj</description>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>hercules</friend>
<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>
</subject>
</student>
</student>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>aladin</friend>
<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>
</subject>
</student>
</student>
<student id="3">
<student>
<refObjectId>m8</refObjectId>
<subject id="91">
<subject>
<name>humanities</name>
<friend>aladin</friend>
<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>
</subject>
</student>
</student>
<student id="4">
<student>
<refObjectId>m3</refObjectId>
<subject id="75">
<subject>
<name>PCM</name>
<friend>hercules</friend>
<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>
</subject>
</student>
</student>
</students>
</students>
<students id="B">
<students>
----
-----
-----
----
</students>
</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>
请帮忙......我被困在这里!!!
答案 0 :(得分:2)
正在制作&#34;父母&#34;的副本。节点,因为您正在使用xsl:copy
以及手动创建新节点。 xsl:copy
将创建当前节点的副本,因此最终会有两个。您使用<student id="{id}">
手动创建的那个以及使用xsl:copy
复制的那个。
因此,您的模板应该看起来像这样:
<xsl:template match="subject">
<subject id="{id}">
<xsl:apply-templates />
</subject>
</xsl:template>
但是,您可以使您的XSLT更通用,以匹配具有子id
节点的任何元素。
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
version="1.0">
<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="id" />
<xsl:template match="*[id]">
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="id" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>