XSLT 1.0:将元素插入数组
XSLT 1.0: Insert elements to an Aarray
我有一个类似下面的XML,我想读取<LocationID>
并将相关ID与对应的姓名映射并以逗号分隔存储。
我使用 for-each
完成了相应的名称映射,但我无法将这两个值存储在数组中(稍后用逗号连接)或存储在变量中。我怎样才能实现最后一部分
<Data>
<Mapping>
<LocationID>001</LocationID>
<GeoX>1.00</GeoX>
<GeoY>2.00</GeoY>
</Mapping>
<Mapping>
<LocationID>002</LocationID>
<GeoX>56.00</GeoX>
<GeoY>42.00</GeoY>
<Mapping>
</Data>
预期输出:
<Location>
<Name>ABC,XYZ</Name>
<Cost>00</Cost>
</Location>
现有代码
<Location>
<xsl:for-each select="location/type">
<xsl:choose>
<xsl:when test="LocationID='001'">
<xsl:variable name="loc1" select="ABC"/>
</xsl:when>
<xsl:when test="LocationID='002'">
<xsl:variable name="loc2" select="XYZ"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="loc" select="NEW"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<Name>
<xsl:value-of select="concat($loc1,$loc2,$loc)" />
</Name>
<Cost>
<xsl:value-of select="cost" />
</Cost>
</Location>
考虑以下示例:
XML
<Data>
<Mapping>
<LocationID>001</LocationID>
<GeoX>1.00</GeoX>
<GeoY>2.00</GeoY>
</Mapping>
<Mapping>
<LocationID>002</LocationID>
<GeoX>56.00</GeoX>
<GeoY>42.00</GeoY>
</Mapping>
</Data>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Data">
<Location>
<Name>
<xsl:for-each select="Mapping">
<xsl:choose>
<xsl:when test="LocationID='001'">ABC</xsl:when>
<xsl:when test="LocationID='002'">XYZ</xsl:when>
<xsl:otherwise>NEW</xsl:otherwise>
</xsl:choose>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
</Name>
<Cost>
<xsl:value-of select="some-missing-node" />
</Cost>
</Location>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<Location>
<Name>ABC,XYZ</Name>
<Cost/>
</Location>
我有一个类似下面的XML,我想读取<LocationID>
并将相关ID与对应的姓名映射并以逗号分隔存储。
我使用 for-each
完成了相应的名称映射,但我无法将这两个值存储在数组中(稍后用逗号连接)或存储在变量中。我怎样才能实现最后一部分
<Data>
<Mapping>
<LocationID>001</LocationID>
<GeoX>1.00</GeoX>
<GeoY>2.00</GeoY>
</Mapping>
<Mapping>
<LocationID>002</LocationID>
<GeoX>56.00</GeoX>
<GeoY>42.00</GeoY>
<Mapping>
</Data>
预期输出:
<Location>
<Name>ABC,XYZ</Name>
<Cost>00</Cost>
</Location>
现有代码
<Location>
<xsl:for-each select="location/type">
<xsl:choose>
<xsl:when test="LocationID='001'">
<xsl:variable name="loc1" select="ABC"/>
</xsl:when>
<xsl:when test="LocationID='002'">
<xsl:variable name="loc2" select="XYZ"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="loc" select="NEW"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<Name>
<xsl:value-of select="concat($loc1,$loc2,$loc)" />
</Name>
<Cost>
<xsl:value-of select="cost" />
</Cost>
</Location>
考虑以下示例:
XML
<Data>
<Mapping>
<LocationID>001</LocationID>
<GeoX>1.00</GeoX>
<GeoY>2.00</GeoY>
</Mapping>
<Mapping>
<LocationID>002</LocationID>
<GeoX>56.00</GeoX>
<GeoY>42.00</GeoY>
</Mapping>
</Data>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Data">
<Location>
<Name>
<xsl:for-each select="Mapping">
<xsl:choose>
<xsl:when test="LocationID='001'">ABC</xsl:when>
<xsl:when test="LocationID='002'">XYZ</xsl:when>
<xsl:otherwise>NEW</xsl:otherwise>
</xsl:choose>
<xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
</Name>
<Cost>
<xsl:value-of select="some-missing-node" />
</Cost>
</Location>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<Location>
<Name>ABC,XYZ</Name>
<Cost/>
</Location>