问题
my xml input is-
<?xml version="1.0" encoding="UTF-8"?>
<foo> <bar>bar</bar>
<bar>bar</bar>
<foobar><baz>baz</baz></foobar>
<foobar>foobar</foobar>
<bar>bar</bar>
<bar>bar</bar>
</foo>
Output using xslt should be
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<s>
<s>
<bar>bar</bar>
<bar>bar</bar>
</s>
<s>
<foobar><baz>baz</baz></foobar>
<foobar>foobar></foobar>
</s>
<s>
<bar>bar</bar>
<bar>bar</bar>
</s>
</s>
</foo>
the output should have sequence-of elements inside a parent. Mixed sequence-of elements will be moved inside parents node “s”. the xslt file that is used is-
<?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" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="adjacentByName" match="*/*" use="generate-id(preceding-sibling::*[not(name()=name(current()))][1])" />
<xsl:template match="/*">
<foo><s>
<xsl:for-each select="*[generate-id()=generate-id(key('adjacentByName', generate-id(preceding-sibling::*[not(name()=name(current()))][1]))[1])]">
<s>
<xsl:for-each select="key('adjacentByName', generate-id(preceding-sibling::*[not(name()=name(current()))][1]))">
<xsl:copy-of select="."/>
</xsl:for-each>
</s>
</xsl:for-each>
</s></foo>
</xsl:template>
</xsl:stylesheet>
the problem is in output baz element (child element of first foobar element) is with both bar and foobar sequences.
<foo>
<s>
<s>
<bar>bar</bar>
<bar>bar</bar>
**<baz>baz</baz>**
</s>
<s>
<foobar>
<baz>baz</baz>
</foobar>
<foobar>foobar</foobar>
</s>
<s>
<bar>bar</bar>
<bar>bar</bar>
</s>
</s>
</foo>
How can i remove baz element from bar sequences. Many thanks.
回答1:
How about
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:key name="kGroupLeader" match="*" use="
generate-id(self::*[name() != name(preceding-sibling::*[1])])
" />
<xsl:template match="foo">
<xsl:copy>
<s>
<xsl:for-each select="*[key('kGroupLeader', generate-id())]">
<s>
<xsl:copy-of select=". | following-sibling::*[
name() = name(current())
and
generate-id(current()) = generate-id(
preceding-sibling::*[key('kGroupLeader', generate-id())][1]
)
]" />
</s>
</xsl:for-each>
</s>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With your input of
<foo>
<bar id="1">bar</bar>
<bar id="2">bar</bar>
<foobar id="3"><baz>baz</baz></foobar>
<foobar id="4">foobar</foobar>
<bar id="5">bar</bar>
<bar id="6">bar</bar>
</foo>
this produces
<foo>
<s>
<s>
<bar id="1">bar</bar>
<bar id="2">bar</bar>
</s>
<s>
<foobar id="3">
<baz>baz</baz>
</foobar>
<foobar id="4">foobar</foobar>
</s>
<s>
<bar id="5">bar</bar>
<bar id="6">bar</bar>
</s>
</s>
</foo>
来源:https://stackoverflow.com/questions/22429162/muenchian-method-for-nested-elements