我有一个输入文档,我只想提取前缀为p,T和C的元素

时间:2018-10-31 17:24:01

标签: xml xslt xslt-2.0 root sku

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

  • 在我的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>

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

非常感谢您的帮助。
谢谢,期待您的答复。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

如评论中所示,您似乎可以使用for-each-group group-by在XSLT 2或3中解决一个简单的分组问题:

<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