给定 input1.xml 文件,创建一个 XSL 文件,将其转换为 output1.xml 的格式。
input1.xml 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Trips>
<tripID>75</tripID>
<tripID>79</tripID>
<tripID>85</tripID>
<tripID>88</tripID>
</Trips>
而 output1.xml 是这样的:
<?xml version="1.0" encoding="utf-8"?>
<TripsToPlan>
<ids>75,79,85,88</ids>
<numberofShifts>4</numberofShifts>
</TripsToPlan>
我该怎么做?
答案 0 :(得分:0)
我们开始......我正在使用 EXSLT 扩展来计数,不确定是否有其他方法。
此 XSLT 应用于您的源 XML:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
exclude-result-prefixes="exslt"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="Trips"/>
</xsl:template>
<xsl:template match="Trips">
<TripsToPlan>
<ids>
<xsl:apply-templates select="tripID"/>
</ids>
<numberofShifts>
<xsl:call-template name="countLines">
<xsl:with-param name="nodes" select="tripID"/>
</xsl:call-template>
</numberofShifts>
</TripsToPlan>
</xsl:template>
<xsl:template match="tripID">
<xsl:value-of select="."/>
<xsl:if test="not(position() = last())">
<xsl:value-of select="','"/>
</xsl:if>
</xsl:template>
<xsl:template name="countLines">
<xsl:param name="nodes"/>
<xsl:value-of select="count(exslt:node-set($nodes))"/>
</xsl:template>
</xsl:stylesheet>
产生这个输出:
<?xml version="1.0" encoding="utf-8"?>
<TripsToPlan>
<ids>75,79,85,88</ids>
<numberofShifts>4</numberofShifts>
</TripsToPlan>
通读一遍,试着理解会发生什么,改变它,看看会发生什么。玩得开心! 最好的问候,彼得