Reputation: 669
I am trying to create xml structure that have repeating element based on input xml repeating structure. Below are the input/output xml and xslt that i am trying. Input has fields key
and value
. Only need all the properties which have register.
keyword in the Key
field. Ignore all other properties.
Input:
<ns1:Properties xmlns:ns1="http://www.test.org/cmm/xsd/mdmtag_01">
<ns1:MDMTagTagProperties>
<ns1:Key>referenceId</ns1:Key>
<ns1:Value>1871691600000406318</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>dataSourceType</ns1:Key>
<ns1:Value>POWER</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>AllocationMethod</ns1:Key>
<ns1:Value>PRF</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>DossierCode</ns1:Key>
<ns1:Value>mdmconnectionpoint:MDMConnectionPointDossierCode</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>MaxConsumption</ns1:Key>
<ns1:Value>10000</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>Residential</ns1:Key>
<ns1:Value>J</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ContractedCapacity</ns1:Key>
<ns1:Value>10</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>CapacityGridChargeCode</ns1:Key>
<ns1:Value>8742090102111e</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>GridOperatorCompanyCode</ns1:Key>
<ns1:Value>8716916000004</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.Number</ns1:Key>
<ns1:Value>mdmconnectionpoint:Number</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>EACOffPeak</ns1:Key>
<ns1:Value>3453</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>MeterNumber</ns1:Key>
<ns1:Value>ENC-212233445566</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ReferenceDate</ns1:Key>
<ns1:Value>2001-12-31T12:00:00</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.Id1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterId1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.Id2</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterId2</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>EnergyMeterType</ns1:Key>
<ns1:Value>DUS</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.Street</ns1:Key>
<ns1:Value>mdmconnectionpoint:Street</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>EnergyFlowDirection</ns1:Key>
<ns1:Value>LVR</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ProcessTypeCode</ns1:Key>
<ns1:Value>mdmconnectionpoint:MDMConnectionPointProcessTypeCode</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>TemperatureCorrection</ns1:Key>
<ns1:Value>NTB</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>GridOperatorCompany</ns1:Key>
<ns1:Value>mdmconnectionpoint:MDMConnectionPointGridOperatorCompany</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.City</ns1:Key>
<ns1:Value>mdmconnectionpoint:City</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.NumberExtension</ns1:Key>
<ns1:Value>mdmconnectionpoint:NumberExtension</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>MarketSegmentCode</ns1:Key>
<ns1:Value>KVB</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>GridArea</ns1:Key>
<ns1:Value>871691600019188908</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ProductType</ns1:Key>
<ns1:Value>POWER</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.LocationDescription</ns1:Key>
<ns1:Value>mdmconnectionpoint:LocationDescription</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.NrOfDigits1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterNrOfDigits1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.NrOfDigits2</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterNrOfDigits2</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.RegisterType2</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterType2</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.RegisterType1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterType1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>AdministrativeStatusSmartMeter</ns1:Key>
<ns1:Value>AAN</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>PhysicalCapacityCode</ns1:Key>
<ns1:Value>1x35</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>SupplierCode</ns1:Key>
<ns1:Value>8716911900123</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>DeterminationComplex</ns1:Key>
<ns1:Value>N</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>EnergyDeliveryStatus</ns1:Key>
<ns1:Value>ACT</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>StandardLoadprofile</ns1:Key>
<ns1:Value>E1B</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.PostalCode</ns1:Key>
<ns1:Value>mdmconnectionpoint:PostalCode</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>MeteringResponsiblePartyCompanyCode</ns1:Key>
<ns1:Value>8712423029194</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>BalanceResponsiblePartyCompanyCode</ns1:Key>
<ns1:Value>8712423029194</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>EACPeak</ns1:Key>
<ns1:Value>4008</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>PhysicalStatus</ns1:Key>
<ns1:Value>IBD</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ExternalReference</ns1:Key>
<ns1:Value>mdmconnectionpoint:MDMConnectionPointExternalReference</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.Country</ns1:Key>
<ns1:Value>mdmconnectionpoint:Country</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>address.State</ns1:Key>
<ns1:Value>mdmconnectionpoint:State</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MultiplicationFactor1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterMultiplicationFactor1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MultiplicationFactor2</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterMultiplicationFactor2</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeasureUnit1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterMeasureUnit1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeasureUnit2</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterMeasureUnit2</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>MeteringMethod</ns1:Key>
<ns1:Value>JRL</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>NrOfRegisters</ns1:Key>
<ns1:Value>1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeteringDirection1</ns1:Key>
<ns1:Value>mdmconnectionpoint:RegisterMeteringDirection1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>InvoiceMonth</ns1:Key>
<ns1:Value>8</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>ConnectionpointCode</ns1:Key>
<ns1:Value>1871691600000406318</ns1:Value>
</ns1:MDMTagTagProperties>
</ns1:Properties>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.test.org/cmm/xsd/mdmtag_01" xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01" exclude-result-prefixes="ns1" version="1.0">
<xsl:template match="/ns1:Properties">
<ns5:Output>
<xsl:for-each select="ns1:MDMTagTagProperties">
<ns5:Register>
<ns5:RegisterId>
<xsl:if test="ns1:Key='register.Id'">
<xsl:value-of select="ns1:Value" />
</xsl:if>
</ns5:RegisterId>
<ns5:RegisterType>
<xsl:if test="ns1:Key='register.RegisterType'">
<xsl:value-of select="ns1:Value" />
</xsl:if>
</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>
<xsl:if test="ns1:Key='register.MultiplicationFactor'">
<xsl:value-of select="ns1:Value" />
</xsl:if>
</ns5:RegisterMultiplicationFactor>
</ns5:Register>
</xsl:for-each>
</ns5:Output>
</xsl:template>
</xsl:stylesheet>
CurrentOutput:
<?xml version="1.0" encoding="UTF-8"?>
<ns5:Output xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01">
<ns5:Register>
<ns5:RegisterId>123</ns5:RegisterId>
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor />
</ns5:Register>
<ns5:Register>
<ns5:RegisterId />
<ns5:RegisterType>RegisterType</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor />
</ns5:Register>
<ns5:Register>
<ns5:RegisterId />
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor>RegisterMultiplicationFactor</ns5:RegisterMultiplicationFactor>
</ns5:Register>
<ns5:Register>
<ns5:RegisterId>12345</ns5:RegisterId>
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor />
</ns5:Register>
<ns5:Register>
<ns5:RegisterId />
<ns5:RegisterType>RegisterType2</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor />
</ns5:Register>
<ns5:Register>
<ns5:RegisterId />
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor>RegisterMultiplicationFactor2</ns5:RegisterMultiplicationFactor>
</ns5:Register>
</ns5:Output>
Desired Output:
<ns5:Output xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01">
<ns5:Register xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01">
<ns5:RegisterId>register.Id1</ns5:RegisterId>
<ns5:RegisterType>register.RegisterType1</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>RegisterMultiplicationFactor</ns5:RegisterMultiplicationFactor>
</ns5:Register>
<ns5:Register xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01">
<ns5:RegisterId>register.Id2</ns5:RegisterId>
<ns5:RegisterType>RegisterType2</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>RegisterMultiplicationFactor2</ns5:RegisterMultiplicationFactor>
</ns5:Register>
.........
</ns5:Output>
Can anyone help me out?
Upvotes: 2
Views: 185
Reputation: 9627
And here a key bast (somehow generic) version.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.test.org/cmm/xsd/mdmtag_01" xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01" exclude-result-prefixes="ns1" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:key name="kRegisterId" match="ns1:MDMTagTagProperties[not(ns1:Key = 'register.Id') ]"
use="generate-id(preceding-sibling::ns1:MDMTagTagProperties[ ns1:Key = 'register.Id' ][1])"/>
<xsl:template match="/ns1:Properties">
<ns5:Output>
<xsl:for-each select="ns1:MDMTagTagProperties[ns1:Key = 'register.Id' ]">
<xsl:variable name="this" select="." />
<ns5:Register>
<ns5:RegisterId>
<xsl:if test="ns1:Key='register.Id'">
<xsl:value-of select="ns1:Value" />
</xsl:if>
</ns5:RegisterId>
<xsl:for-each select="key('kRegisterId', generate-id($this) ) ">
<xsl:element name="ns5:{substring-after(ns1:Key, 'register.')}" >
<xsl:value-of select="ns1:Value" />
</xsl:element>
</xsl:for-each>
</ns5:Register>
</xsl:for-each>
</ns5:Output>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 9627
Try this small changes:
<xsl:template match="/ns1:Properties">
<ns5:Output>
<xsl:for-each select="ns1:MDMTagTagProperties[ns1:Key = 'register.Id' ]">
<xsl:variable name="this" select="." />
<ns5:Register>
<ns5:RegisterId>
<xsl:value-of select="ns1:Value" />
</ns5:RegisterId>
<ns5:RegisterType>
<xsl:value-of select="following-sibling::*[ns1:Key='register.RegisterType'][1]/ns1:Value" />
</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>
<xsl:value-of select="following-sibling::*[ns1:Key='register.MultiplicationFactor'][1]/ns1:Value" />
</ns5:RegisterMultiplicationFactor>
</ns5:Register>
</xsl:for-each>
</ns5:Output>
</xsl:template>
This is a little bit quick an dirty and will only work if always all ns1:Key type available. If not a key based grouping could be use.
Update: Without keys you can check if the there is following sibling before the next regiserd.id like this:
<xsl:if test="following-sibling::*
[ns1:Key='register.RegisterType' or ns1:Key = 'register.Id']
[1]/ns1:Key = 'register.RegisterType'">
<xsl:value-of select="following-sibling::*[ns1:Key='register.RegisterType'][1]/ns1:Value" />
</xsl:if>
Upvotes: 2