我正在尝试使用XSLT通过对数据进行分组来处理数据。
示例XML
<DELIVERYLEG>
<DELIVERYINSTRUCTION>
<MAP_REFERENCE_SYSTEM>EUREF-FIN</MAP_REFERENCE_SYSTEM>
<MAP_COORDINATE_TYPE>ETRS-TM35FIN</MAP_COORDINATE_TYPE>
<XY_TEXT>XY</XY_TEXT>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>3</MAP_POINT_ROW>
<MAP_POINT_NAME>695123</MAP_POINT_NAME>
<MAP_POINT_GROUP>3</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>UserWay</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>1</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>Turning</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>695123</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT>SALMI AINO</MAP_POINT_COMMENT>
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>4</MAP_POINT_ROW>
<MAP_POINT_NAME>6951337</MAP_POINT_NAME>
<MAP_POINT_GROUP>5</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>1</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951337</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>5</MAP_POINT_ROW>
<MAP_POINT_NAME>6951271</MAP_POINT_NAME>
<MAP_POINT_GROUP>5</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>2</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951271</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>6</MAP_POINT_ROW>
<MAP_POINT_NAME>6951479</MAP_POINT_NAME>
<MAP_POINT_GROUP>6</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>1</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951479</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>7</MAP_POINT_ROW>
<MAP_POINT_NAME>6951507</MAP_POINT_NAME>
<MAP_POINT_GROUP>6</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>2</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951507</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>8</MAP_POINT_ROW>
<MAP_POINT_NAME>6951598</MAP_POINT_NAME>
<MAP_POINT_GROUP>6</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>3</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951598</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
<maprows>
<MAP_POINT_TOTAL_ROWS>18</MAP_POINT_TOTAL_ROWS>
<MAP_POINT_ROW>9</MAP_POINT_ROW>
<MAP_POINT_NAME>6951734</MAP_POINT_NAME>
<MAP_POINT_GROUP>6</MAP_POINT_GROUP>
<MAP_POINT_DISPLAY_TYPE>Line</MAP_POINT_DISPLAY_TYPE>
<COORDINATE_DISPLAY_ORDER>4</COORDINATE_DISPLAY_ORDER>
<MAP_POINT_TYPE>ForestRoad</MAP_POINT_TYPE>
<MAP_POINT_COORDINATES>6951734</MAP_POINT_COORDINATES>
<MAP_POINT_COMMENT />
</maprows>
</DELIVERYINSTRUCTION>
</DELIVERYLEG>
它应该在MAP_POINT_GROUP上分组,所有坐标都应该在该组下。 以下是输出除外....
<MapPoint MapPointDisplayType="Turning" MapPointType="UserWay">
<MapPointName>695123</MapPointName>
<MapPointComment>Kom</MapPointComment>
<MapCoordinates>
<Coordinates Order="1" Type="XY">695123</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="Line" MapPointType="ForestRoad">
<MapPointName>6951271</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951337</Coordinates>
<Coordinates Order="2" Type="XY">6951271</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="Line" MapPointType="ForestRoad">
<MapPointName>6951479</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951479</Coordinates>
<Coordinates Order="2" Type="XY">6951507</Coordinates>
<Coordinates Order="3" Type="XY">6951598</Coordinates>
<Coordinates Order="4" Type="XY">6951734</Coordinates>
</MapCoordinates>
</MapPoint>
我该如何处理?
以下是现有的XSLT
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var"
exclude-result-prefixes="msxsl"
xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp"
xmlns:ReformatDate="http://functions/xslt">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0"/>
<xsl:template match="/">
<xsl:apply-templates select="/DeliveryLeg" />
<!--<xsl:apply-templates select="/DeliveryLeg" />-->
</xsl:template>
<xsl:template match="/DeliveryLeg">
<DeliveryInstruction>
<SupplyPoint>
<xsl:attribute name="LocationType">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/ORIGIN_VALUE/text()"/>
</xsl:attribute>
<SupplyPointCode>
<xsl:attribute name="Agency">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/SUPPLY_POINT_CODE/text()"/>
</xsl:attribute>
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/ERA_NRO/text()"/>
</SupplyPointCode>
<SupplyPointCode>
<xsl:attribute name="Agency">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/LOGISTICS_BUYER/text()"/>
</xsl:attribute>
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/ERA_NRO/text()"/>
</SupplyPointCode>
<SupplyPointDescription>
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/ROADSIDE_STOCK/text()"/>
</SupplyPointDescription>
<MapCoordinates>
<xsl:attribute name="MapReferenceSystem">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/MAP_REFERENCE_SYSTEM/text()"/>
</xsl:attribute>
<xsl:attribute name="MapCoordinateType">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/MAP_COORDINATE_TYPE/text()"/>
</xsl:attribute>
<Coordinates>
<xsl:attribute name="CoordinateFormatType">
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/XY_TEXT/text()"/>
</xsl:attribute>
<xsl:value-of select ="logforce..LF_HEADER_LAHTOVARASTO/logforce..LF_LAHTOVARASTO/ERA_KOORDINAATIT/text()"/>
</Coordinates>
</MapCoordinates>
<!--New Logic for MapPoint-->
<SupplyPointCharacteristics>
<NEW CODE SHOULD BE THERE>
</SupplyPointCharacteristics>
</SupplyPoint>
</DeliveryInstruction>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
我创建了一些脚本:
<强> XSLT:强>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="group" match="maprows" use="MAP_POINT_GROUP"/>
<xsl:template match="rows">
<row>
<xsl:apply-templates select="maprows[generate-id()=generate-id(key('group',MAP_POINT_GROUP))]"
/>
</row>
</xsl:template>
<xsl:template match="maprows">
<xsl:variable name="Group" select="MAP_POINT_GROUP"/>
<MapPoint MapPointDisplayType="{MAP_POINT_TYPE}" MapPointType="{MAP_POINT_TYPE}">
<MapPointName>
<xsl:value-of select="MAP_POINT_NAME"/>
</MapPointName>
<MapCoordinates>
<xsl:for-each select="parent::rows/maprows[MAP_POINT_GROUP=$Group]">
<Coordinates Order="{position()}" Type="XY">
<xsl:value-of select="MAP_POINT_NAME"/>
</Coordinates>
</xsl:for-each>
</MapCoordinates>
</MapPoint>
</xsl:template>
</xsl:stylesheet>
<强>输出:强>
<row>
<MapPoint MapPointDisplayType="Turning" MapPointType="Turning">
<MapPointName>695123</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">695123</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="ForestRoad" MapPointType="ForestRoad">
<MapPointName>6951337</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951337</Coordinates>
<Coordinates Order="2" Type="XY">6951271</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="ForestRoad" MapPointType="ForestRoad">
<MapPointName>6951479</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951479</Coordinates>
<Coordinates Order="2" Type="XY">6951507</Coordinates>
<Coordinates Order="3" Type="XY">6951598</Coordinates>
<Coordinates Order="4" Type="XY">6951734</Coordinates>
</MapCoordinates>
</MapPoint>
</row>
答案 1 :(得分:0)
这里的解决方案基于Muenchian grouping:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kmaprows" match="maprows" use="MAP_POINT_GROUP"/>
<xsl:template match="/">
<row>
<xsl:apply-templates select="//maprows[generate-id() = generate-id( key('kmaprows',MAP_POINT_GROUP)[1]) ]" />
</row>
</xsl:template>
<xsl:template match="maprows">
<MapPoint MapPointDisplayType="{MAP_POINT_TYPE}" MapPointType="{MAP_POINT_TYPE}">
<MapPointName>
<xsl:value-of select="MAP_POINT_NAME"/>
</MapPointName>
<MapCoordinates>
<xsl:for-each select="key('kmaprows',current()/MAP_POINT_GROUP)">
<Coordinates Order="{position()}" Type="{../XY_TEXT}">
<xsl:value-of select="MAP_POINT_NAME"/>
</Coordinates>
</xsl:for-each>
</MapCoordinates>
</MapPoint>
</xsl:template>
</xsl:stylesheet>
这将产生预期的输出:
<row>
<MapPoint MapPointDisplayType="Turning" MapPointType="Turning">
<MapPointName>695123</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">695123</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="ForestRoad" MapPointType="ForestRoad">
<MapPointName>6951337</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951337</Coordinates>
<Coordinates Order="2" Type="XY">6951271</Coordinates>
</MapCoordinates>
</MapPoint>
<MapPoint MapPointDisplayType="ForestRoad" MapPointType="ForestRoad">
<MapPointName>6951479</MapPointName>
<MapCoordinates>
<Coordinates Order="1" Type="XY">6951479</Coordinates>
<Coordinates Order="2" Type="XY">6951507</Coordinates>
<Coordinates Order="3" Type="XY">6951598</Coordinates>
<Coordinates Order="4" Type="XY">6951734</Coordinates>
</MapCoordinates>
</MapPoint>
</row>