1

I need to remap an array of objects like this:

    <Root>
      <ListOfObjs>
        <Obj>
          <Attr1>0000</Attr1>
          <Attr2>Hello!</Attr2>
        </Obj>
        <Obj>
          <Attr1>1111</Attr1>
          <Attr2>Hello1!</Attr2>
        </Obj>
      </ListOfObjs>
    </Root>

To an output like this:

        <Root>
            <Obj1_Attr1>0000</Obj1_Attr1>
            <Obj1_Attr2>Hello!</Obj1_Attr2>
            <Obj2_Attr1>1111</Obj2_Attr1>
            <Obj2_Attr2>Hello1!</Obj2_Attr2>
        </Root>

So in my XSD schema I have something like this:

Schema Input

                           <xs:element name="Root">
                            <xs:complexType>
                             <xs:sequence>
                              <xs:element name="ListOfObjs">
                               <xs:complexType>
                                <xs:sequence>
                                 <xs:element name="Obj">
                                  <xs:complexType>
                                   <xs:sequence>
                                    <xs:element name="Attr1">
                                     <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                   <xs:element name="Attr2">
                                    <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                   </xs:sequence>
                                  </xs:complexType>
                                 </xs:element>
                                </xs:sequence>
                               </xs:complexType>
                              </xs:element>

Schema output

                                 <xs:element name="Root">
                                  <xs:complexType>
                                   <xs:sequence>
                                    <xs:element name="Obj1_Attr1">
                                     <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                   <xs:element name="Obj1_Attr2">
                                    <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                    <xs:element name="Obj2_Attr1">
                                    <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                    <xs:element name="Obj2_Attr2">
                                    <xs:simpleType>
                                      <xs:restriction base="xs:string">
                                       <xs:minLength value="1"/>
                                       <xs:maxLength value="50"/>
                                      </xs:restriction>
                                     </xs:simpleType>
                                    </xs:element>
                                   </xs:sequence>
                                  </xs:complexType>
                                 </xs:element>

In addition I have to evaluate every single value because I found some conditions, like if value=0000 , where the output should be NULL.

What would be the best way to do this?

I'm thinking of developing a custom functoid but I'm not sure it would be the best way, probably it could be done using XSLT inline transforms, can you point me in the best direction?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Caio
  • 61
  • 5

2 Answers2

1

First of all, there are some errors on your schemas:

The correct XSD for Schema1 has to be:

<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ListOfObjs">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Obj" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Attr1">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:minLength value="1"/>
                          <xs:maxLength value="50"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                    <xs:element name="Attr2">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:minLength value="1"/>
                          <xs:maxLength value="50"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

I've come up with the following

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <xsl:template match="/">
    <Root>
      <xsl:for-each select="/Root/ListOfObjs/Obj">
        <xsl:variable name="objName" select="name(.)"/>
        <xsl:variable name="objPos" select="position()"/>

        <xsl:for-each select="*">
          <xsl:variable name="nodeName" select="name(.)"/>

          <xsl:variable name="name" select="concat($objName, $objPos, $nodeName)"/>

          <xsl:element name="{$name}">
            <xsl:value-of select="."/>
          </xsl:element>

        </xsl:for-each>

      </xsl:for-each>
    </Root>
  </xsl:template>

</xsl:stylesheet>

With the following input:

<Root>
  <ListOfObjs>
    <Obj>
      <Attr1>0000</Attr1>
      <Attr2>Hello!</Attr2>
    </Obj>
    <Obj>
      <Attr1>1111</Attr1>
      <Attr2>Hello1!</Attr2>
    </Obj>
  </ListOfObjs>
</Root>

This gives the following output:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <Obj1Attr1>0000</Obj1Attr1>
  <Obj1Attr2>Hello!</Obj1Attr2>
  <Obj2Attr1>1111</Obj2Attr1>
  <Obj2Attr2>Hello1!</Obj2Attr2>
</Root>
zurebe-pieter
  • 3,246
  • 21
  • 38
  • Thank you, you're great. But now, suppose that I use this solution to create this output that I want to have as output file of my Biztalk custom project. How can I map these new 4 fields to my xsd schema output? – Caio Nov 06 '14 at 13:56
  • I"m not following I'm afraid: what XSD schema output are you referring to? The question was about a specified input and output schema. Is a second mapping necessary? This was not part of your (original) question. Please elaborate. – zurebe-pieter Nov 06 '14 at 22:09
0

If the 1st schema is an "array" of objects. Why you are so sure there're only Obj1 and Obj2 in your target schema?

Zee
  • 830
  • 1
  • 9
  • 22