Question

This xml generates from Dynamics Ax which need transformation (XSLT): Need to create an XSLT in order to get intended outbound xml file

<?xml version="1.0" encoding="UTF-16"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
  <Header>
    <MessageId>{3BCFB0D6-EAB4-430E-9921-E365F189046D}</MessageId>
    <Action>http://schemas.microsoft.com/dynamics/2008/01/services/LedgerExchangeRateService/read</Action>
  </Header>
  <Body>
    <MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
      <LedgerExchangeRate xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerExchangeRate">
        <SenderId>Womar</SenderId>
        <CurrencyPair class="entity">
          <FromCurrencyCode>US</FromCurrencyCode>
          <ToCurrencyCode>DKK</ToCurrencyCode>
          <ExchangeRateType>Default</ExchangeRateType>
          <ExchangeRateDisplayFactor>Hundred</ExchangeRateDisplayFactor>
          <ExchangeRate class="entity">
            <ExchangeRate>500.000000000000</ExchangeRate>
            <ValidFrom>2013-05-17</ValidFrom>
          </ExchangeRate>
          <RateType class="entity">
            <Name>Default</Name>
          </RateType>
        </CurrencyPair>
      </LedgerExchangeRate>
    </MessageParts>
  </Body>
</Envelope>

After transformation, the file will be as:

<?xml version="1.0" encoding="utf-8"?>
<exchangeRateImport action="create" xmlns="http://schemas.v.com/2005/ImosOps" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <baseCurrency>US</baseCurrency>
  <currency>EU</currency>
  <rate>55.00000000</rate>
  <effectiveDate>2013-05-17</effectiveDate>
</exchangeRateImport>

I have created an XLRT as follows which results differently. Please help.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:axEnv="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
                xmlns:axExchRate="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerExchangeRate">
  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>
  <xsl:template match="/">
    <exchangeRateImport imosMsg:action="update" xmlns:imosMsg="http://schemas.veson.com/2005/ImosMsg" xmlns="http://schemas.veson.com/2005/ImosOps" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <baseCurrency>
        <xsl:value-of select="axEnv:Envelope/axEnv:body/axEnv:MessageParts/axExchRate:LedgerExchangeRate/axExchRate:CurrencyPair/axExchRate:FromCurrencyCode"/>
      </baseCurrency>
      <currency/>
      <rate/>
      <effectiveDate/>
    </exchangeRateImport>
  </xsl:template>
</xsl:stylesheet>

Result:

<exchangeRateImport imosMsg:action="update" xmlns="http://schemas.veson.com/2005/ImosOps" xmlns:imosMsg="http://schemas.veson.com/2005/ImosMsg" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:axEnv="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" xmlns:axExchRate="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerExchangeRate"><baseCurrency></baseCurrency><currency /><rate /><effectiveDate /></exchangeRateImport>
Was it helpful?

Solution

The main reason your transform isn't working is that the axEnv:body element isn't being found because it has a capital B in the source XML.

I don't fully understand what it is you need, as the XML you say you require barely corresponds to the source data at all. However here is my best guess. I hope it helps.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:axEnv="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
    xmlns:axExchRate="http://schemas.microsoft.com/dynamics/2008/01/documents/LedgerExchangeRate"
    exclude-result-prefixes="axEnv axExchRate">

  <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>

  <xsl:template match="/">
    <xsl:apply-templates select="axEnv:Envelope/axEnv:Body/axEnv:MessageParts/axExchRate:LedgerExchangeRate/axExchRate:CurrencyPair"/>
  </xsl:template>

  <xsl:template match="axExchRate:CurrencyPair">

    <exchangeRateImport
        imosMsg:action="update"
        xmlns="http://schemas.veson.com/2005/ImosOps"
        xmlns:imosMsg="http://schemas.veson.com/2005/ImosMsg"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <baseCurrency>
        <xsl:value-of select="axExchRate:FromCurrencyCode"/>
      </baseCurrency>
      <currency>
        <xsl:value-of select="axExchRate:ToCurrencyCode"/>
      </currency>
      <rate>
        <xsl:value-of select="axExchRate:ExchangeRate/axExchRate:ExchangeRate"/>
      </rate>
      <effectiveDate>
        <xsl:value-of select="axExchRate:ExchangeRate/axExchRate:ValidFrom"/>
      </effectiveDate>
    </exchangeRateImport>

  </xsl:template>

</xsl:stylesheet>

output

<?xml version="1.0" encoding="utf-8"?>
<exchangeRateImport
    xmlns="http://schemas.veson.com/2005/ImosOps" 
    xmlns:imosMsg="http://schemas.veson.com/2005/ImosMsg" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    imosMsg:action="update">

  <baseCurrency>US</baseCurrency>
  <currency>DKK</currency>
  <rate>500.000000000000</rate>
  <effectiveDate>2013-05-17</effectiveDate>
</exchangeRateImport>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top