XSLT将元素根据ID

时间:2018-06-28 05:45:25

标签: xml xslt xsd

我有以下xml:

<Metro>
<MeasurementList>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1212</AcqSettingsRef>
            <SensSettingsRef>#33</SensSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1213</AcqSettingsRef>
            <SensSettingsRef>#34</SensSettingsRef>
        </MeasurementY>
    </Measurement>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1212</AcqSettingsRef>
            <SensSettingsRef>#33</SensSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1214</AcqSettingsRef>
            <SensSettingsRef>#35</SensSettingsRef>
        </MeasurementY>
    </Measurement>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1215</AcqSettingsRef>
            <SensSettingsRef>#36</SensSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1216</AcqSettingsRef>
        </MeasurementY>
    </Measurement>
</MeasurementList>
<References>
    <AcqSettings>
        <Id>1212</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
    <AcqSettings>
        <Id>1213</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
    <AcqSettings>
        <Id>1214</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
    <AcqSettings>
        <Id>1215</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
    <AcqSettings>
        <Id>1216</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
    <SensSettings>
        <Id>33</Id>
        <Prop3>bla33<Prop3>
    </SensSettings>
    <SensSettings>
        <Id>34</Id>
        <Prop3>bla34<Prop3>
    </SensSettings>
    <SensSettings>
        <Id>35</Id>
        <Prop3>bla35<Prop3>
    </SensSettings>
    <SensSettings>
        <Id>36</Id>
        <Prop3>bla36<Prop3>
    </SensSettings>
</References>

我必须基于“ MeasurementX或MeasurementY”具有的ID关系,将“ SensSettings”从其自身的元素移至“ AcqSettings”中。 SensSettings可能会缺少一些。

结果xml应该如下所示:

<Metro>
<MeasurementList>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1212</AcqSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1213</AcqSettingsRef>
        </MeasurementY>
    </Measurement>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1212</AcqSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1214</AcqSettingsRef>
        </MeasurementY>
    </Measurement>
    <Measurement>
        <MeasurementX>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1215</AcqSettingsRef>
        </MeasurementX>
        <MeasurementY>
            <Prop1>bla<Prop1>
            <AcqSettingsRef>#1216</AcqSettingsRef>
        </MeasurementY>
    </Measurement>
</MeasurementList>
<References>
    <AcqSettings>
        <Id>1212</Id>
        <Prop2>bla<Prop2>
        <Prop3>bla33<Prop3>
    </AcqSettings>
    <AcqSettings>
        <Id>1213</Id>
        <Prop2>bla<Prop2>
        <Prop3>bla34<Prop3>
    </AcqSettings>
    <AcqSettings>
        <Id>1214</Id>
        <Prop2>bla<Prop2>
        <Prop3>bla35<Prop3>
    </AcqSettings>
    <AcqSettings>
        <Id>1215</Id>
        <Prop2>bla<Prop2>
        <Prop3>bla36<Prop3>
    </AcqSettings>
    <AcqSettings>
        <Id>1216</Id>
        <Prop2>bla<Prop2>
    </AcqSettings>
</References>

这应该通过XSLT转换来完成。现在,我不确定如何根据测量中的ID将SensSettings链接到AcqSettings。

1 个答案:

答案 0 :(得分:1)

使用两个键,您可以跟随引用并复制引用的元素:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:key name="mes-ref" match="MeasurementX | MeasurementY" use="substring(AcqSettingsRef, 2)"/>
  <xsl:key name="setting" match="SensSettings" use="Id"/>

  <xsl:template match="AcqSettings">
      <xsl:copy>
          <xsl:apply-templates/>
          <xsl:copy-of select="key('setting', key('mes-ref', Id)/SensSettingsRef/substring(., 2))/Prop3"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="SensSettings"/>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFDb2Cg在线使用XSLT 3,但是可以通过阐明身份转换在XSLT 2中替换xsl:mode的使用。