bar bar foobar f...">

Sequences in XSLT

my xml input -

<?xml version="1.0" encoding="UTF-8"?> 
<foo>  <bar>bar</bar> 
       <bar>bar</bar> 
       <foobar>foobar</foobar> 
       <foobar>foobar</foobar> 
       <foobar>foobar</foobar>
       <bar>bar</bar>
       <bar>bar</bar> 
</foo>

The output using xslt should be

 <?xml version="1.0" encoding="UTF-8"?>

 <foo>  
 <s> 
 <s> 
 <bar>bar</bar>  
 <bar>bar</bar>
 </s>
 <s> 
 <foobar>foobar></foobar>
 <foobar>foobar></foobar>
 <foobar>foobar></foobar>
 </s> 
 <s>      
 <bar>bar</bar>  
 <bar>bar</bar> 
 </s>
 </s>
</foo>

The output must have a sequence of elements inside the parent. A mixed sequence of elements will move inside the parents of node "s". Is there any xslt command that defines the sequence and processes the xml accordingly. Many thanks.

0
source share
1 answer

Same as here , use the identifier of the first previous brother, whose name is different for grouping records:

<?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="foo/*" use="generate-id(preceding-sibling::*[not(name()=name(current()))][1])" />

<xsl:template match="/">
<foo><s>
    <xsl:for-each select="foo/*[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>

Added:

But the conclusion that I get is not the same as my desired result ...

When applied to your (cleaned) input:

<?xml version="1.0" encoding="UTF-8"?> 
<foo>
    <bar>bar</bar> 
    <bar>bar</bar> 
    <foobar>foobar</foobar> 
    <foobar>foobar</foobar> 
    <foobar>foobar</foobar>
    <bar>bar</bar>
    <bar>bar</bar> 
</foo>

result:

<?xml version="1.0" encoding="utf-8"?>
<foo>
  <s>
    <s>
      <bar>bar</bar>
      <bar>bar</bar>
    </s>
    <s>
      <foobar>foobar</foobar>
      <foobar>foobar</foobar>
      <foobar>foobar</foobar>
    </s>
    <s>
      <bar>bar</bar>
      <bar>bar</bar>
    </s>
  </s>
</foo>

, :

<?xml version="1.0" encoding="UTF-8"?>

 <foo>  
 <s> 
 <s> 
 <bar>bar</bar>  
 <bar>bar</bar>
 </s>
 <s> 
 <foobar>foobar></foobar>
 <foobar>foobar></foobar>
 <foobar>foobar></foobar>
 </s> 
 <s>      
 <bar>bar</bar>  
 <bar>bar</bar> 
 </s>
 </s>
</foo>

> <foobar>, .

-

P.S. .

+1

All Articles