0

我是 XML 和 XSLT 的新手,我有一个输入文档,我想在其中提取前缀为 p 、 T 和 C 的元素,并计算带有根 SKU 和列表的序列号。我尝试了很多方法,但无法让它工作,需要一些帮助/建议。

  • 在我的 XSLT 中,我将所有带有 safix ppp 的元素复制到输出文件中。
  • 在我的 XSLT 中,我将所有带有 safix c00 的元素复制到输出文件中。
  • 在我的 XSLT 中,我将所有带有 saefix t0 的元素复制到输出文件中。

很感谢任何形式的帮助。
谢谢您,期待您的回复。

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0001</serial>
    </row>
    <row>
        <product.name>iPadMini</product.name>
        <category>tablet</category>
        <serial>T0001</serial>
    </row>
    <row>
        <product.name>iPadMini</product.name>
        <category>tablet</category>
        <serial>T0002</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0002</serial>
    </row>
    <row>
        <product.name>iMacPro</product.name>
        <category>computer</category>
        <serial>C0001</serial>
    </row>
    <row>
        <product.name>iMacPro</product.name>
        <category>computer</category>
        <serial>C0002</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0003</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0004</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0005</serial>
    </row>

预期的输出格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<Inventory>
    <SKU>
        <category>phone</category>
        <product.name>iPhoneX</product.name>
        <product.count>5</product.count>
        <list>P0001</list>
        <list>P0002</list>
        <list>P0003</list>
        <list>P0004</list>
        <list>P0005</list>
    </SKU>
    <SKU>
        <category>tablet</category>
        <product.name>iPadMini</product.name>
        <product.count>2</product.count>
        <list>T0001</list>
        <list>T0002</list>
    </SKU>
    <SKU>
        <category>computer</category>
        <product.name>iMacPro</product.name>
        <product.count>2</product.count>
        <list>C0001</list>
        <list>C0002</list>
    </SKU>
</Inventory>

我尝试了很多方法,但无法让它工作,需要一些帮助/建议。

很感谢任何形式的帮助。
谢谢您,期待您的回复。

我能做些什么?

4

1 回答 1

1

正如评论中所指出的,这似乎是一个简单的分组问题,您可以在 XSLT 2 或 3 中使用for-each-group group-by

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:param name="search-list" as="xs:string*" select="'P', 'T', 'C'"/>

  <xsl:output indent="yes"/>

  <xsl:template match="root">
      <Inventory>
          <xsl:for-each-group select="row[substring(serial, 1, 1) = $search-list]" group-by="substring(serial, 1, 1)">
              <SKU>
                  <xsl:copy-of select="category, product.name"/>
                  <product.count>
                      <xsl:value-of select="count(current-group())"/>
                  </product.count>
                  <xsl:apply-templates select="current-group()/serial"/>
              </SKU>
          </xsl:for-each-group>
      </Inventory>
  </xsl:template>

  <xsl:template match="serial">
      <list>
          <xsl:apply-templates/>
      </list>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFDb2Dc

于 2018-11-01T10:46:32.780 回答