使用xslt将同级节点设置为xml中的父节点



我需要使用xslt将以下输入xml转换为所需的输出格式,如下所示,我如何设置<BTAT_MEASUREMENTSet>使用xslt?作为所需输出的父对象?,它总是在根之后的消息的第二级,因此来自根的相对位置是恒定的

**Input:**
<?xml version="1.0" encoding="UTF-8"?>
<SyncBTAT_MEASUREMENT
xmlns="http://www.llll.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2021-03-24T15:39:55+00:00" transLanguage="EN" baseLanguage="EN" messageID="176416166003958143562">
<BTAT_MEASUREMENTSet></BTAT_MEASUREMENTSet>
<MEASUREPOINT action="AddChange">
<BTATMBO>MEASUREMENT</BTATMBO>
<ORGID>TAL3-DE</ORGID>
<POINTNUM>TEMP</POINTNUM>
<SITEID>TAL3</SITEID>
<MEASUREMENT>
<ASSETNUM>2403871PPC</ASSETNUM>
<MEASUREDATE>2020-06-16T06:47:07+00:00</MEASUREDATE>
<MEASUREMENTID/>
<MEASUREMENTVALUE>20.0</MEASUREMENTVALUE>
<METERNAME>TIME-MIN-001</METERNAME>
</MEASUREMENT>
</MEASUREPOINT>
</SyncBTAT_MEASUREMENT>
**Needed Output:**
<?xml version="1.0" encoding="UTF-8"?>
<SyncBTAT_MEASUREMENT
xmlns="http://www.llll.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" creationDateTime="2021-03-24T15:39:55+00:00" transLanguage="EN" baseLanguage="EN" messageID="176416166003958143562">
<BTAT_MEASUREMENTSet>
<MEASUREPOINT action="AddChange">
<BTATMBO>MEASUREMENT</BTATMBO>
<ORGID>TAL3-DE</ORGID>
<POINTNUM>TEMP</POINTNUM>
<SITEID>TAL3</SITEID>
<MEASUREMENT>
<ASSETNUM>2403871PPC</ASSETNUM>
<MEASUREDATE>2020-06-16T06:47:07+00:00</MEASUREDATE>
<MEASUREMENTID/>
<MEASUREMENTVALUE>20.0</MEASUREMENTVALUE>
<METERNAME>TIME-MIN-001</METERNAME>
</MEASUREMENT>
</MEASUREPOINT>
</BTAT_MEASUREMENTSet>
</SyncBTAT_MEASUREMENT>

如果您知道要作为父元素的元素名称以BTAT_开头,并且它没有名称也以BTAT_开头的同级元素,那么您可以执行:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="*[starts-with(local-name(), 'BTAT')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="../* except self::*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

这是假设您不知道输入XML中任何元素的名称,包括根元素,否则可能会更简单。

相关内容

  • 没有找到相关文章

最新更新