输入:
<a q='r'>
<b x='1' y='2' z='3'/>
<!-- other a content -->
</a>
Run Code Online (Sandbox Code Playgroud)
期望的输出:
<A q='r' x='1' y='2' z='3'>
<!-- things derived from other a content, no b -->
</A>
Run Code Online (Sandbox Code Playgroud)
有人可以给我一个食谱吗?
Tom*_*lak 25
简单.
<xsl:template match="a">
<A>
<xsl:copy-of select="@*|b/@*" />
<xsl:apply-templates /><!-- optional -->
</A>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
<xsl:apply-templates />
如果您没有其他孩子<a>
想要处理,则没有必要.
注意
<xsl:copy-of>
将源节点插入到输出中不变|
一次选择几个不相关的节点编辑:如果您需要缩小其属性复制,和你独自离开,用这个(或它的变化):
<xsl:copy-of select="(@*|b/@*)[
name() = 'q' or name() = 'x' or name() = 'y' or name() = 'z'
]" />
Run Code Online (Sandbox Code Playgroud)
甚至
<xsl:copy-of select="(@*|b/@*)[
contains('|q|x|y|z|', concat('|', name(), '|'))
]" />
Run Code Online (Sandbox Code Playgroud)
请注意括号如何使谓词应用于所有匹配的节点.
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="a">
<A>
<xsl:apply-templates select="@*|b/@*|node()"/>
</A>
</xsl:template>
<xsl:template match="b"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
产量
<A q="r" x="1" y="2" z="3"><!-- other a content --></A>
Run Code Online (Sandbox Code Playgroud)