Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform xml structure using XSLT-1.0

I need to transform this structure

<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>

into

<A>
<B>value1<B>
<B>value2<B>
</A>

What is best solution using XSLT-1.0? Thank you!

PS: I tried this code:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>   
<xsl:key name="group_a" match="//A" use="B"/> 
<xsl:template match="/Test"> <a-node> <xsl:for-each select="//A"> <b-node> 
<xsl:value-of select="//A/B"/> </b-node> </xsl:for-each> </a-node> 
</xsl:template> 
</xsl:stylesheet> 

but it returns only first value:

<?xml version="1.0" encoding="utf-8"?> <a-node mlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value1</b-node> </a-node> 

but I need:

<?xml version="1.0" encoding="utf-8"?> <a-node xmlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value2</b-node> </a-node>
like image 632
Alyaksandr Stzhalkouski Avatar asked May 22 '26 00:05

Alyaksandr Stzhalkouski


1 Answers

This style-sheet ...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
    <xsl:template match="/">
       <A>
        <xsl:apply-templates select="*/A/B"/>
       </A>
    </xsl:template>

    <xsl:template match="B">
      <B><xsl:value-of select="."/></B>
    </xsl:template>
</xsl:stylesheet>

... will transform ...

<root>
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
</root>

... into this ...

 <A><B>value1</B><B>value2</B></A>
like image 131
Sean B. Durkin Avatar answered May 23 '26 18:05

Sean B. Durkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!