This is a short post to show how to leverage XSLT to convert a simple and generic XML file into NIEM-conformant XML as it pertains to the nc:ContactInformation block.
This is a very common situation where a "non-NIEM" data stream is received and needs to be converted to a conformant structure. Take the following sample non-NIEM XML instance:
<?xml version="1.0" encoding="UTF-8" ?>
<SomeBatchOfStuff>
<Person>
<Name>John Doe</Name>
<PhoneNumber>212-111-2222</PhoneNumber>
</Person>
<Person>
<Name>Sally Smith</Name>
<PhoneNumber>212-333-4444</PhoneNumber>
</Person>
</SomeBatchOfStuff>
If this very logical structure needed to be converted into nc:Person and nc:ContactInformatoin elements (with an nc:PersonContactInformationAssociation object to link the two together), the following XSLT could be used:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns0="SomeNonConformantDocumentNamespace" version="1.0" exclude-result-prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="var1_instance_InputSchema" select="."/>
<MyNIEMConformantDocument xmlns="MyNIEMDocumentNamespace" xmlns:i="http://niem.gov/niem/appinfo/2.0" xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:niem-xsd="http://niem.gov/niem/proxy/xsd/2.0" xmlns:s="http://niem.gov/niem/structures/2.0">
<!-- Loop through the Persons and create an NIEM Conformant Person -->
<xsl:for-each select="$var1_instance_InputSchema/SomeBatchOfStuff/Person">
<xsl:variable name="NonConformantPerson" select="."/>
<nc:Person>
<xsl:attribute name="s:id">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
<nc:PersonName>
<xsl:for-each select="$NonConformantPerson/Name">
<nc:PersonFullName>
<xsl:value-of select="string(.)"/>
</nc:PersonFullName>
</xsl:for-each>
</nc:PersonName>
</nc:Person>
</xsl:for-each>
<!-- Loop through the phone numbers and create a NIEM Conformant Contact Information -->
<xsl:for-each select="$var1_instance_InputSchema/SomeBatchOfStuff/Person/PhoneNumber">
<nc:ContactInformation>
<xsl:attribute name="s:id">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
<nc:ContactTelephoneNumber>
<nc:FullTelephoneNumber>
<nc:TelephoneNumberFullID>
<xsl:value-of select="string(.)"/>
</nc:TelephoneNumberFullID>
</nc:FullTelephoneNumber>
</nc:ContactTelephoneNumber>
</nc:ContactInformation>
</xsl:for-each>
<!-- Loop through the phone numbers and create a NIEM Conformant Contact Information Association -->
<xsl:for-each select="$var1_instance_InputSchema/SomeBatchOfStuff/Person">
<nc:PersonContactInformationAssociation>
<nc:PersonReference>
<xsl:attribute name="s:ref">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
</nc:PersonReference>
<nc:ContactInformationReference>
<xsl:attribute name="s:ref">
<xsl:value-of select="generate-id(./PhoneNumber)"/>
</xsl:attribute>
</nc:ContactInformationReference>
</nc:PersonContactInformationAssociation>
</xsl:for-each>
</MyNIEMConformantDocument>
</xsl:template>
</xsl:stylesheet>
The XSLT heavily leverages the XSLT generate-id() function in order to work its magic and result in the following NIEM-conformant XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<MyNIEMConformantDocument xmlns="MyNIEMDocumentNamespace" xmlns:i="http://niem.gov/niem/appinfo/2.0" xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:niem-xsd="http://niem.gov/niem/proxy/xsd/2.0" xmlns:s="http://niem.gov/niem/structures/2.0" xmlns:ns0="SomeNonConformantDocumentNamespace">
<nc:Person s:id="d0e3">
<nc:PersonName>
<nc:PersonFullName>John Doe</nc:PersonFullName>
</nc:PersonName>
</nc:Person>
<nc:Person s:id="d0e12">
<nc:PersonName>
<nc:PersonFullName>Sally Smith</nc:PersonFullName>
</nc:PersonName>
</nc:Person>
<nc:ContactInformation s:id="d0e8">
<nc:ContactTelephoneNumber>
<nc:FullTelephoneNumber>
<nc:TelephoneNumberFullID>212-111-2222</nc:TelephoneNumberFullID>
</nc:FullTelephoneNumber>
</nc:ContactTelephoneNumber>
</nc:ContactInformation>
<nc:ContactInformation s:id="d0e17">
<nc:ContactTelephoneNumber>
<nc:FullTelephoneNumber>
<nc:TelephoneNumberFullID>212-333-4444</nc:TelephoneNumberFullID>
</nc:FullTelephoneNumber>
</nc:ContactTelephoneNumber>
</nc:ContactInformation>
<nc:PersonContactInformationAssociation>
<nc:PersonReference s:ref="d0e3"/>
<nc:ContactInformationReference s:ref="d0e8"/>
</nc:PersonContactInformationAssociation>
<nc:PersonContactInformationAssociation>
<nc:PersonReference s:ref="d0e12"/>
<nc:ContactInformationReference s:ref="d0e17"/>
</nc:PersonContactInformationAssociation>
</MyNIEMConformantDocument>