我正在尝试创建具有基于输入xml重复结构的重复元素的xml结构。下面是我正在尝试的输入/输出xml和xslt。输入包含字段key
和value
。只需要register.
字段中包含Key
个关键字的所有属性。忽略所有其他属性。
输入:
<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>
期望的输出:
<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>
任何人都可以帮助我吗?
答案 0 :(得分:2)
尝试这个小改动:
<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>
这有点快,只有在所有ns1:Key类型可用时才会起作用。如果不是,则可以使用基于密钥的分组。
更新: 如果没有密钥,您可以在下一个regiserd.id之前检查是否有以下兄弟:
<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>
答案 1 :(得分:0)
这里有一个关键的麻烦(某种程度上是通用的)版本。
<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>