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>

at

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

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

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 only returns the 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>
+3
source share
3 answers

Since it seems to you that you need to collapse all children under one node, then you do not need foreach on "A", you can go directly to children "B"

<?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:for-each select="//A/B">
                <B>
                    <xsl:value-of select="./text()"/>
                </B>
            </xsl:for-each>
        </A>
    </xsl:template>
</xsl:stylesheet>

EDIT . According to @Sean's comment, note that // should never be used in real life. Replace // with the path from your real Element.

+1
source

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>

... converts ...

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

... in it...

 <A><B>value1</B><B>value2</B></A>
+2

XSLT - . , , , :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" 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[1]">
  <A>
    <xsl:apply-templates select="node()|following-sibling::A/node()"/>
  </A>
 </xsl:template>
 <xsl:template match="A"/>
</xsl:stylesheet>

When this transformation is applied to the following XML document (obtained by transferring the provided XML fragment to one top element) to make it a well-formed XML document):

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

the desired, correct result is output:

<t>
   <A>
      <B>value1</B>
      <B>value2</B>
   </A>
</t>
0
source

All Articles