XML XSLT使用auto gen PK和FK帮助父级子级

时间:2018-01-19 17:24:21

标签: xml xslt

我有一个XML结构,其中包含组织层次结构,但没有任何关系,或者'键'定义

这是我的xml



data.filter(x=>x.cat_type == _archid)




我需要一个能产生以下输出的XSLT



<?xml version="1.0" encoding="utf-8" ?>
<webreports>
<entity>
    <entity_name>Device Liquid Flow Daily</entity_name>
    <db_name>DeviceLiquidFlowDaily</db_name>
    <category>Scada</category>
    <datasource_id>2</datasource_id>
    <object_type>table</object_type>
    <schema>rpt</schema>
    <key>
      <col_name>DeviceKey</col_name>
    </key>
    <column_metadata>
      <col_name>CalendarKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>ClientCode</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>ContractHour</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>DeviceId</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>DeviceKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>FlowPeriodDateUTC</col_name>
      <col_type>datetime</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>FlowPeriodEndingUTC</col_name>
      <col_type>datetime</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>FlowPeriodStartingUTC</col_name>
      <col_type>datetime</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>OilVolume</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>OilVolumeUOM</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>RecordCreationDateUTC</col_name>
      <col_type>datetime</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>SystemCreationDateUTC</col_name>
      <col_type>datetime</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>WaterVolume</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>WaterVolumeUOM</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>FlowPeriodDate</col_name>
      <col_type>datetime</col_type>
      <col_alias>FlowPeriodDate</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({DeviceLiquidFlowDaily.FlowPeriodDateUTC})</col_value>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>FlowPeriodEnding</col_name>
      <col_type>datetime</col_type>
      <col_alias>FlowPeriodEnding</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({DeviceLiquidFlowDaily.FlowPeriodEndingUTC})</col_value>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>FlowPeriodStarting</col_name>
      <col_type>datetime</col_type>
      <col_alias>FlowPeriodStarting</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({DeviceLiquidFlowDaily.FlowPeriodStartingUTC})</col_value>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>RecordCreationDate</col_name>
      <col_type>datetime</col_type>
      <col_alias>RecordCreationDate</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({DeviceLiquidFlowDaily.RecordCreationDateUTC})</col_value>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>SystemCreationDate</col_name>
      <col_type>datetime</col_type>
      <col_alias>SystemCreationDate</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({DeviceLiquidFlowDaily.SystemCreationDateUTC})</col_value>
    </column_metadata>
  </entity>
  <entity>
    <entity_name>Tank Level</entity_name>
    <db_name>TankLevel</db_name>
    <category>Scada</category>
    <datasource_id>2</datasource_id>
    <object_type>table</object_type>
    <schema>rpt</schema>
    <key>
      <col_name>DeviceKey</col_name>
    </key>
    <column_metadata>
      <col_name>CalendarKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>ClientCode</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>DeviceId</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>DeviceKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>FluidLevelDateTimeUTC</col_name>
      <col_type>date</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>RecordCreationDateUTC</col_name>
      <col_type>date</col_type>
      <visible>false</visible>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>SourceModifiedDate</col_name>
      <col_type>date</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>TankId</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>TankKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>TankLevel1</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>TankLevel2</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>TankLevel3</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_name>Temperature</col_name>
      <col_type>float</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>FluidLevelDateTime</col_name>
      <col_type>datetime</col_type>
      <col_alias>FluidLevelDateTime</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({TankLevel.FluidLevelDateTimeUTC})</col_value>
    </column_metadata>
    <column_metadata>
      <col_source>ExagoFormula</col_source>
      <col_name>RecordCreationDate</col_name>
      <col_type>datetime</col_type>
      <col_alias>RecordCreationDate</col_alias>
      <filterable>None</filterable>
      <col_value>UTCConvertToLocal({TankLevel.RecordCreationDateUTC})</col_value>
    </column_metadata>
  </entity>
  </webreports>
&#13;
&#13;
&#13;

所以基本上我需要将每个子键和子column_metadata元素与父实体

相关联

任何和所有帮助表示赞赏!

我试过这个......

&#13;
&#13;
<?xml version="1.0" encoding="utf-8" ?>
<webreports>
<entity>
    <entity_name>Device Liquid Flow Daily</entity_name>
    <db_name>DeviceLiquidFlowDaily</db_name>
    <category>Scada</category>
    <datasource_id>2</datasource_id>
    <object_type>table</object_type>
    <schema>rpt</schema>
    <key>
      <col_name_entityKEY>Device Liquid Flow Daily</col_name_entityKEY>
      <col_name>DeviceKey</col_name>
    </key>
    <column_metadata>
      <col_metadata_entityKEY>Device Liquid Flow Daily</col_metadata_entityKEY>
      <col_name>CalendarKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_metadata_entityKEY>Device Liquid Flow Daily</col_metadata_entityKEY>
      <col_name>ClientCode</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_metadata_entityKEY>Device Liquid Flow Daily</col_metadata_entityKEY>
      <col_name>ContractHour</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    ...
  </entity>
  <entity>
    <entity_name>Tank Level</entity_name>
    <db_name>TankLevel</db_name>
    <category>Scada</category>
    <datasource_id>2</datasource_id>
    <object_type>table</object_type>
    <schema>rpt</schema>
    <key>
      <col_name_entityKEY>Tank Level</col_name_entityKEY>
      <col_name>DeviceKey</col_name>
    </key>
    <column_metadata>
      <col_metadata_entityKEY>Tank Level</col_metadata_entityKEY>
      <col_name>CalendarKey</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_metadata_entityKEY>Tank Level</col_metadata_entityKEY>
      <col_name>ClientCode</col_name>
      <col_type>string</col_type>
      <filterable>All</filterable>
    </column_metadata>
    <column_metadata>
      <col_metadata_entityKEY>Tank Level</col_metadata_entityKEY>
      <col_name>DeviceId</col_name>
      <col_type>int</col_type>
      <filterable>All</filterable>
    ...
 
  </webreports>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这两个模板会不会这样做......

<xsl:template match="column_metadata">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <col_metadata_entityKEY>
      <xsl:value-of select="ancestor::entity/entity_name" />
    </col_metadata_entityKEY>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<xsl:template match="key">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <col_name_entityKEY>
      <xsl:value-of select="ancestor::entity/entity_name" />
    </col_name_entityKEY>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>    

或许你可以将它们组合成一个......

<xsl:template match="key|column_metadata">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:element name="{if (local-name() = 'key') then 'col_name' else 'col_metadata'}_entityKEY">
      <xsl:value-of select="ancestor::entity/entity_name" />
    </xsl:element>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>