Question

I'm new to XSLT and I'm having some trouble convrting an xml file.

I need to know the XSLT code, I have a serialized xml (XML A) that I want to convert into XML B?

I'm un-sure how to merge the customer section into one section.

Thanks in advance

XML A

<Customers>
  <Customer>
    <Policy>
      <diaPolicyId>8B71E21FEFF546A680DFE9DA40EC0711</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
    </Policy>
    <PolicyAddtional>
      <diaPolicyId>8B71E21FEFF546A680DFE9DA40EC0711</diaPolicyId>
    </PolicyAddtional>
    <Customer>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <Title>003</Title>
    </Customer>
    <CustomerAddtional>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <IsBirthday>false</IsBirthday>
    </CustomerAddtional>
  </Customer>
  <Customer>
    <Policy>
      <diaPolicyId>E835E227EA8F44F1BDCF346CE63541F3</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
    </Policy>
    <PolicyAddtional>
      <diaPolicyId>E835E227EA8F44F1BDCF346CE63541F3</diaPolicyId>
    </PolicyAddtional>
    <Customer>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
    </Customer>
    <CustomerAddtional>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <IsBirthday>false</IsBirthday>
    </CustomerAddtional>
  </Customer>
</Customers>

XML B

<Customers>
  <Customer>
      <diaPolicyId>8B71E21FEFF546A680DFE9DA40EC0711</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <diaPolicyId>8B71E21FEFF546A680DFE9DA40EC0711</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <Title>003</Title>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <IsBirthday>false</IsBirthday>
  </Customer>
  <Customer>
      <diaPolicyId>E835E227EA8F44F1BDCF346CE63541F3</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <diaPolicyId>E835E227EA8F44F1BDCF346CE63541F3</diaPolicyId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <diaCustomerId>STE/WIL/19650420</diaCustomerId>
      <IsBirthday>false</IsBirthday>
  </Customer>
</Customers>

UPDATED: Added XSLT Done So Far:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="Customers">
    <combined>
      <xsl:copy-of select="@* | Customer/@*" />
    </combined>
  </xsl:template>
</xsl:stylesheet>

No correct solution

OTHER TIPS

You first need to learn about the XSLT identity transform

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

On its own it copies all the elements as-is, which means you only need to write templates for the nodes you actually wish to change.

It looks you wish to remove the child elements of the Customer elements (although keep their children of such child elements). In which case, you just need to add this template to handle this

<xsl:template match="Customers/Customer/*">
   <xsl:apply-templates />
</xsl:template>

So "Customers/Customer/*" matches the elements you want to remove, (or rather, not copy to the output), and <xsl:apply-templates /> will then carry on processing their children (which will be matched by the identity transform)

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

  <xsl:template match="Customers/Customer/*">
      <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top