首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从XML中提取XSL来拆分文件

从XML中提取XSL来拆分文件
EN

Stack Overflow用户
提问于 2022-06-04 01:37:56
回答 1查看 40关注 0票数 0

我有一个下面的xml示例文件,有大量的记录。我想根据头/测试簿id将其分成两个failedItems。生成的文件应该有价格表/价格表/@产品id和价格表/价格表/金额。

我面临的挑战是吐出文件并选择我需要的列。任何帮助都是很大的帮助。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<testbooks xmlns="http://www.demandware.com/xml/impex/testbook/2006-10-31">
  <testbook>
    <header testbook-id="ca-cad-list">
      <currency>CAD</currency>
      <online-flag>true</online-flag>
    </header>
    <price-tables>
      <price-table product-id="035:M:1:">
        <online-from>2022-06-02T00:00:00.000Z</online-from>
        <online-to>9999-12-31T23:59:59.000Z</online-to>
        <amount quantity="1">90.00 </amount>
      </price-table>
    </price-tables>
  </testbook>

  <testbook>
    <header testbook-id="ca-cad-sale">
      <currency>CAD</currency>
      <online-flag>true</online-flag>
    </header>
    <price-tables>
      <price-table product-id="035:M:1:">
        <online-from>2022-06-02T00:00:00.000Z</online-from>
        <online-to>9999-12-31T23:59:59.000Z</online-to>
        <amount quantity="1">85</amount>
      </price-table>
    </price-tables>
  </testbook>
  
</testbooks>
EN

回答 1

Stack Overflow用户

发布于 2022-06-04 10:59:41

在XSLT 3中,您可以使用分组,然后使用xsl:result-document,很明显,加上空的模板,以避免在输出中复制您不想要的数据。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xpath-default-namespace="http://www.demandware.com/xml/impex/testbook/2006-10-31"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:template match="testbooks">
    <xsl:for-each-group select="testbook" group-by="header/@testbook-id">
      <xsl:result-document href="{current-grouping-key()}.xml">
        <xsl:copy select="..">
          <xsl:apply-templates select="current-group()"/>
        </xsl:copy>
      </xsl:result-document>
    </xsl:for-each-group>
  </xsl:template>
  
  <xsl:template match="price-table/online-from | price-table/online-to"/>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:mode on-no-match="shallow-copy"/>
  
</xsl:stylesheet>

这假设您只想根据header/@testbook-id进行拆分,但保留testbook元素。当然,分组还允许您加入/合并属于一个组的元素。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72496506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档