tags:

views:

93

answers:

3

Hi there,

Say I have the following XML

<?xml version="1.0" encoding="utf-8"?>
<Person>
  <FirstName>Bjorn</FirstName>
  <LastName>Ellis-Gowland</LastName>
</Person>

That is 'governed' by the following XSD (XML Schema)

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="Person">
    <xs:complexType>
      <xs:all>
        <xs:element name="FirstName" type="xs:string" />
        <xs:element name="LastName" type="xs:string" />
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

I also have a XSD that is as follows

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="AnonymousPerson">
    <xs:complexType>
      <xs:all>
        <xs:element name="FirstNameInitial">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="1" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
        <xs:element name="LastNameInitial">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:length value="1" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>


My initial Person.xsd XML can be transformed into a state that is valid for my AnonymousPerson.xsd.

How do I define this transformation of valid Person.xsd XML data to AnonymousPerson.xsd XML data?

Thanks!!!!!

+1  A: 

Use an XML Transformation:

http://www.w3schools.com/xsl/

bethlakshmi
+1  A: 

xslt transformations

Sean
+3  A: 

the xslt you need is roughly:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

<xsl:template match="/" >
   <xsl:for-each select='//Person'>
      <AnonymousPerson>
         <FirstNameInitial>
            <xsl:value-of select="substring(FirstName, 1,1)"/>
         </FirstNameInitial>
         <LastNameInitial>
            <xsl:value-of select="substring(LastName, 1,1)"/>
         </LastNameInitial>
      </AnonymousPerson>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Haven't tried it out, but it shouldn't take much to get this into a working state!

EDIT: (Got around to testing, it works!)

Irfy