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



我是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>

我尝试了很多方法,但都无法实现,需要一些帮助/建议。

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

我能做什么?

如注释所示,这似乎是一个简单的分组问题,您可以在XSLT2或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