中的唯一子字符串從重複元素中提取xml我想創建具有基於輸入xml的重複元素的xml結構。在輸入重複元素MDMTagTagProperties
只有2個字段Key
和Value
。基於Key字段,我們需要在輸出xml中創建一個元素,並將Value
字段的內容設置爲該字段。 Key
字段的最後3個數字字符對於一個子集是相同的。以下是我正在嘗試的輸入XML,xslt和期望輸出。基於值
輸入:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Properties xmlns:ns1="http://www.test.org/cmm/xsd/mdmtag_01">
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeteringDirection.2.8.1</ns1:Key>
<ns1:Value>TLV</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MultiplicationFactor.1.8.2</ns1:Key>
<ns1:Value>2.0</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeasureUnit.2.8.1</ns1:Key>
<ns1:Value>KWH</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.RegisterType.1.8.2</ns1:Key>
<ns1:Value>N</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeteringDirection.1.8.2</ns1:Key>
<ns1:Value>LVR</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MultiplicationFactor.2.8.1</ns1:Key>
<ns1:Value>5.0</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.NrOfDigits.1.8.2</ns1:Key>
<ns1:Value>5</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.MeasureUnit.1.8.2</ns1:Key>
<ns1:Value>KWH</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.RegisterType.2.8.1</ns1:Key>
<ns1:Value>P</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.NrOfDigits.2.8.1</ns1:Key>
<ns1:Value>4</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.Id.2.8.1</ns1:Key>
<ns1:Value>2.8.1</ns1:Value>
</ns1:MDMTagTagProperties>
<ns1:MDMTagTagProperties>
<ns1:Key>register.Id.1.8.2</ns1:Key>
<ns1:Value>1.8.2</ns1:Value>
</ns1:MDMTagTagProperties>
</ns1:Properties>
XSLT:
<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:template match="/ns1:Properties">
<ns5:Output>
<xsl:for-each select="ns1:MDMTagTagProperties[contains(ns1:Key,'register.Id')]">
<ns5:Register>
<ns5:RegisterId>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.Id')]" />
</ns5:RegisterId>
<ns5:RegisterType>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.RegisterType')]" />
</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.RegisterMultiplicationFactor')]" />
</ns5:RegisterMultiplicationFactor>
<ns5:RegisterMeteringDirection>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.RegisterMeteringDirection')]" />
</ns5:RegisterMeteringDirection>
<ns5:RegisterMeasureUnit>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.RegisterMeasureUnit')]" />
</ns5:RegisterMeasureUnit>
<ns5:RegisterNrOfDigits>
<xsl:value-of select="current()/ns1:Value[contains(ns1:Key,'register.RegisterNrOfDigits')]" />
</ns5:RegisterNrOfDigits>
</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 />
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor />
<ns5:RegisterMeteringDirection />
<ns5:RegisterMeasureUnit />
<ns5:RegisterNrOfDigits />
</ns5:Register>
<ns5:Register>
<ns5:RegisterId />
<ns5:RegisterType />
<ns5:RegisterMultiplicationFactor />
<ns5:RegisterMeteringDirection />
<ns5:RegisterMeasureUnit />
<ns5:RegisterNrOfDigits />
</ns5:Register>
</ns5:Output>
ExpectedOutput:
<?xml version="1.0" encoding="UTF-8"?>
<ns5:Output xmlns:ns5="http://www.test.org/cmm/xsd/outputmdmtag_01">
<ns5:Register>
<ns5:RegisterId>1.8.2</ns5:RegisterId>
<ns5:RegisterType>N</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>2.0</ns5:RegisterMultiplicationFactor>
<ns5:RegisterMeteringDirection>LVR</ns5:RegisterMeteringDirection>
<ns5:RegisterMeasureUnit>KHW</ns5:RegisterMeasureUnit>
<ns5:RegisterNrOfDigits>5</ns5:RegisterNrOfDigits>
</ns5:Register>
<ns5:Register>
<ns5:RegisterId>2.8.1</ns5:RegisterId>
<ns5:RegisterType>P</ns5:RegisterType>
<ns5:RegisterMultiplicationFactor>5.0</ns5:RegisterMultiplicationFactor>
<ns5:RegisterMeteringDirection>TLV</ns5:RegisterMeteringDirection>
<ns5:RegisterMeasureUnit>KWH</ns5:RegisterMeasureUnit>
<ns5:RegisterNrOfDigits>4</ns5:RegisterNrOfDigits>
</ns5:Register>
</ns5:Output>
任何幫助將是一個很大的忙。
完善。你做了我的一天。謝謝蒂姆! –