xmlxsltxslt-1.0xslt-2.0xmlconvert

How to modify created output DOM element?


I have xml:

<Root>
   <A>
     <B>X</B>
     <C>Y</C>
     <D>Z</D>
   </A>
</Root>

With use of xslt transform, I need to get this:

<Root>
  <Wrap type="B">
   <A>
     <B>X</B>
   </A>
  </Wrap>
  <Wrap type="C">
   <A>
     <B>Y</B>
   </A>
  </Wrap>
  <Wrap type="D">
   <A>
     <B>Z</B>
   </A>
  </Wrap>
</Root>

What kind of xsl::select should I use? And how to modify just created elements in xsl.

EDIT: correct typo, question updated. I add another one element to a list, now it is the list of A, that should be classified.

<Root>
   <A>
     <B>X</B>
     <C>Y</C>
     <D>Z</D>
   </A>
   <A>
     <B>X1</B>
     <C>Y1</C>
     <D>Z1</D>
   </A>
</Root>

And now I want to get

<Root>
  <Wrap type="B">
   <A>
     <B>X</B>
     <B>X1</B>
   </A>
  </Wrap>
  <Wrap type="C">
   <A>
     <B>Y</B>
     <B>Y1</B>
   </A>
  </Wrap>
  <Wrap type="D">
   <A>
     <B>Z</B>
     <B>Z1</B>
   </A>
  </Wrap>
</Root>

Solution

  • Use This

    <xsl:template match="Root">
        <xsl:copy>
        <xsl:for-each select="A[1]/*">
            <xsl:variable name="en" select="local-name(current())"/>
            <Wrap type="{local-name(current())}">
                <A>
                    <xsl:for-each select="//A/*[name() eq $en]">
                    <B>
                        <xsl:value-of select="."/>
                    </B>
                    </xsl:for-each>
                </A>
            </Wrap>
        </xsl:for-each>
        </xsl:copy>
    </xsl:template>