我有一个这样的xml;
<root>
<a>
<a1>value</a1>
<a2>value</a2>
<b>
<b1>value</b1>
<b2>value</b2>
</b>
</a>
</root>
我只是想将元素b从元素a中取出;这意味着我需要以下结果xml。
<root>
<a>
<a1>value</a1>
<a2>value</a2>
</a>
<b>
<b1>value</b1>
<b2>value</b2>
</b>
</root>
在XSLT中处理此问题的最佳方法是什么?
答案 0 :(得分:2)
您可以采用多种方法来实现此目标,具体取决于您的确切要求。下面的代码将找到任何元素module App (M : sig end) : sig
type t
val zero : t
end = struct
type t = int
let zero = 0
end
(* A () argument signifies a generative functor in OCaml. *)
module Gen (M : sig end) () : sig
type t
val zero : t
end = struct
type t = int
let zero = 0
end
module Empty = struct end
module A = App (Empty)
module B = App (Empty)
module C = App (struct end) (* The argument is syntactically different. *)
module D = Gen (Empty) ()
module E = Gen (Empty) ()
let _ = begin
(* A.t and B.t are compatible. *)
ignore (A.zero = B.zero); (* OK *)
(* A.t and C.t are not compatible because the functor arguments
* are not syntactically equal. *)
ignore (A.zero = C.zero); (* type error *)
(* D.t and C.t are not compatible because they are produced
* from generative functors. *)
ignore (D.zero = E.zero); (* type error *)
end
,并将其复制并删除其下面的所有直接子元素a
,然后将它们放在原始b
之后。
a
如果处理多个子元素,则可以像这样处理
<xsl:template match="//a">
<xsl:copy>
<xsl:apply-templates select="./*[not(local-name()='b')]"/>
</xsl:copy>
<xsl:apply-templates select="./b"/>
</xsl:template>
答案 1 :(得分:2)
获得期望结果的另一种方法是在b
下有一个排除模板来匹配a
并在期望的位置显式添加:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="root">
<xsl:element name="root">
<xsl:apply-templates />
<xsl:element name="b">
<xsl:apply-templates select="a/b/*" />
</xsl:element>
</xsl:element>
</xsl:template>
<xsl:template match="a/b" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>