Xml 用XSL包装某些节点
我想用Xml 用XSL包装某些节点,xml,xslt,Xml,Xslt,我想用包装的子节点,这些子节点不是或 注意:和将始终是的第一个子节点 将其转换为: <root> <foo> <bar>bar</bar> <baz>baz</baz> <qux>qux</qux> <grault>grault</grault> </foo> <foo>
包装
的子节点,这些子节点不是
或
注意:
和
将始终是
的第一个子节点
将其转换为:
<root>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<grault>grault</grault>
</foo>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<quux>quux</quux>
</foo>
</root>
酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
为此:
<root>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<corge>
<qux>qux</qux>
<grault>grault</grault>
</corge>
</foo>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<corge>
<qux>qux</qux>
<quux>quux</quux>
</corge>
</foo>
</root>
酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
使用XSL执行此操作的好方法是什么?此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo/*"/>
<xsl:template match="foo/bar|foo/baz">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="foo/*[not(self::bar or self::baz)][1]">
<corge>
<xsl:apply-templates select="../*[not(self::bar or self::baz)]"
mode="corge"/>
</corge>
</xsl:template>
<xsl:template match="foo/*" mode="corge">
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
我会这样做的 XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<grault>grault</grault>
</foo>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<quux>quux</quux>
</foo>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="foo">
<foo>
<xsl:apply-templates select="bar|baz"/>
<corge>
<xsl:apply-templates select="*[name() != 'bar' and name() != 'baz']"/>
</corge>
</foo>
</xsl:template>
</xsl:stylesheet>
酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯
XSL
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<grault>grault</grault>
</foo>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<qux>qux</qux>
<quux>quux</quux>
</foo>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.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="foo">
<foo>
<xsl:apply-templates select="bar|baz"/>
<corge>
<xsl:apply-templates select="*[name() != 'bar' and name() != 'baz']"/>
</corge>
</foo>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<corge>
<qux>qux</qux>
<grault>grault</grault>
</corge>
</foo>
<foo>
<bar>bar</bar>
<baz>baz</baz>
<corge>
<qux>qux</qux>
<quux>quux</quux>
</corge>
</foo>
</root>
酒吧
巴兹
库克斯
格劳特
酒吧
巴兹
库克斯
库克斯