您能否建议我如何使用以下XML构建XSLT逻辑?我需要能够按BIOSReldate排序并为BIOSReldate和BIOSVersion组合创建两行。
我的最终输出应在.CSV中具有以下属性
Description, Class, HardwareID, Vendor, BIOSRelDate, BIOSVersion
<!---
有两个BIOSRelDate和BIOSVersion实例,需要将它们生成为多行
<Device Count="1" Description="xxxx System Management BIOS Driver"
Class="System" DeviceType="Smbios">
<HardwareIDs>
<ID Value="ROOT\mssmbios" />
</HardwareIDs>
<Properties>
<Vendor Value="American Megatrends Inc." />
<BIOSRelDate Value="11/02/2016" />
<BIOSVersion Value="C1043.BS.4A16.AH1" />
<BIOSRelDate Value="10/02/2017" />
<BIOSVersion Value="C1043.BS.4A25.AH1" />
</Properties>
</Device>
Below is what I tried -
<xsl:apply-templates select="Device/Properties">
<xsl:sort select="BIOSRelDate" data-type="number" order="ascending"/>
</xsl:apply-templates>
...
<xsl:template match="BIOSRelDate">
<!--Duplicate all Properties data for each BIOSRelDate-->
<Device>
<xsl:value-of select="../*[not(self::BIOSRelDate)] | ."/>
</Device>
</xsl:template>
答案 0 :(得分:0)
这样尝试吗?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/Device">
<!-- HEADER -->
<xsl:text>Description, Class, HardwareID, Vendor, BIOSRelDate, BIOSVersion </xsl:text>
<!-- COMMON DATA -->
<xsl:variable name="common">
<xsl:value-of select="@Description"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="@Class"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="HardwareIDs/ID/@Value"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="Properties/Vendor/@Value"/>
<xsl:text>,</xsl:text>
</xsl:variable>
<!-- GENERATE ROWS -->
<xsl:for-each select="Properties/BIOSRelDate">
<!-- SORT BY YEAR -->
<xsl:sort select="substring(@Value, 7, 4)" data-type="number"/>
<!-- SORT BY MONTH -->
<xsl:sort select="substring(@Value, 1, 2)" data-type="number"/>
<!-- SORT BY DAY -->
<xsl:sort select="substring(@Value, 4, 2)" data-type="number"/>
<!-- OUTPUT -->
<xsl:value-of select="$common"/>
<xsl:value-of select="@Value"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="following-sibling::BIOSVersion[1]/@Value"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,结果将是:
Description, Class, HardwareID, Vendor, BIOSRelDate, BIOSVersion
xxxx System Management BIOS Driver,System,ROOT\\mssmbios,American Megatrends Inc.,11/02/2016,C1043.BS.4A16.AH1
xxxx System Management BIOS Driver,System,ROOT\\mssmbios,American Megatrends Inc.,10/02/2017,C1043.BS.4A25.AH1
请注意,我假设您的日期格式为MM/DD/YYYY
。您的例子在这方面是模棱两可的。如果是DD/MM/YYYY
,请进行必要的调整。