【发布时间】:2021-07-13 09:30:27
【问题描述】:
我是一个新手和 xslt,我为我的 xml 到 XML 转换创建了一个 xslt,它对名为 catLineNum 的字段进行排序。
XSLT 代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="category">
<category><xsl:value-of select= "category"/></category>
<xsl:variable name="min" select="min(//catLineNum)" />
<catLineNum><xsl:value-of select="$min" /></catLineNum>
<xsl:copy>
<xsl:apply-templates select="categoryDescription">
<xsl:sort select="catLineNum" data-type="number" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
输入xml
<?xml version='1.0' encoding='UTF-8'?>
<root>
<model>
<models>
<key>18322022AMECHANICAL & PERFORMANCE</key>
<models>
<category>
<category>MECHANICAL & PERFORMANCE</category>
<categoryDescription>
<categoryDescription>with Valvematic Technology, CVT</categoryDescription>
<catLineNum>002</catLineNum>
</categoryDescription>
<categoryDescription>
<categoryDescription>-15-in Styled Steel Wheels</categoryDescription>
<catLineNum>003</catLineNum>
</categoryDescription>
<categoryDescription>
<categoryDescription>-1.8L 4-cyl Engine</categoryDescription>
<catLineNum>001</catLineNum>
</categoryDescription>
</category>
</models>
</models>
</model>
</root>
输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<model>
<models>
<key>18322022AMECHANICAL & PERFORMANCE</key>
<models>
<category>MECHANICAL & PERFORMANCE</category>
<catLineNum>1</catLineNum>
<category>
<categoryDescription>
<categoryDescription>-1.8L 4-cyl Engine</categoryDescription>
<catLineNum>001</catLineNum>
</categoryDescription>
<categoryDescription>
<categoryDescription>with Valvematic Technology, CVT</categoryDescription>
<catLineNum>002</catLineNum>
</categoryDescription>
<categoryDescription>
<categoryDescription>-15-in Styled Steel Wheels</categoryDescription>
<catLineNum>003</catLineNum>
</categoryDescription>
</category>
</models>
</models>
</model>
</root>
问题是如果我不包含这部分代码
输出中将缺少类别字段。我觉得还有另一种方法可以对值进行排序并包含该类别字段。 感谢您的帮助。
【问题讨论】: