用于为BIOSRelDate和BIOSVersion组合创建多行的逻辑

问题描述 投票:-1回答:1

您能否告诉我如何使用以下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>
xml xslt
1个回答
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&#10;</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>&#10;</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,请进行必要的调整。

© www.soinside.com 2019 - 2024. All rights reserved.