Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT转换:将文件分组并拆分为多个部分_Xml_Xslt 2.0_Saxon - Fatal编程技术网

Xml XSLT转换:将文件分组并拆分为多个部分

Xml XSLT转换:将文件分组并拆分为多个部分,xml,xslt-2.0,saxon,Xml,Xslt 2.0,Saxon,我不熟悉XSLT转换,并且有非常基础的知识。 我在寻求你的帮助。我有一个XML文件,请参见下面的示例。我想将此文件拆分为多个较小的文件 我想根据cmfp:future和cmfp:Quotence的价值进行分组 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <GuiRoot xmlns:cmfp="mx.MarketParameters.Fixing.Commodities.Futures" xmlns:mp="m

我不熟悉XSLT转换,并且有非常基础的知识。 我在寻求你的帮助。我有一个XML文件,请参见下面的示例。我想将此文件拆分为多个较小的文件

我想根据cmfp:future和cmfp:Quotence的价值进行分组

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GuiRoot xmlns:cmfp="mx.MarketParameters.Fixing.Commodities.Futures" xmlns:mp="mx.MarketParameters" xmlns:fg="mx.MarketParameters.Fixing" xmlns:fgrt="mx.MarketParameters.Fixing.IRIndices" xmlns:xc="xmlCache" xmlns:fgfx="mx.MarketParameters.Fixing.FXIndices" xmlns:cmip="mx.MarketParameters.Commodities.Indices">
 <xc:XmlCache xc:action="Update">
  <xc:XmlCacheArea xc:value="MarketParameters">
   <mp:nickName xc:subset="Reference" xc:value="./BORATES">
    <mp:date xc:value="20161202">
     <fg:fixing>
      <cmfp:futurePrice>
       <cmfp:future xc:value="ONE">
        <cmfp:quotation xc:value="1.1">
         <cmfp:maturity xc:value="1.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="ONE">
        <cmfp:quotation xc:value="1.2">
         <cmfp:maturity xc:value="1.2.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
        <cmfp:quotation xc:value="1.2">
         <cmfp:maturity xc:value="1.2.2">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13406.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="TWO">
        <cmfp:quotation xc:value="2.1">
         <cmfp:maturity xc:value="2.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="THREE">
        <cmfp:quotation xc:value="3.1">
         <cmfp:maturity xc:value="3.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="FOUR">
        <cmfp:quotation xc:value="4.1">
         <cmfp:maturity xc:value="4.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="EIGHT">
        <cmfp:quotation xc:value="8.1">
         <cmfp:maturity xc:value="8.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
      </cmfp:futurePrice>
     </fg:fixing>
    </mp:date>
   </mp:nickName>
  </xc:XmlCacheArea>
 </xc:XmlCache>
</GuiRoot>

[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13406.00
[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13405.00
所需输出 File1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GuiRoot xmlns:cmfp="mx.MarketParameters.Fixing.Commodities.Futures" xmlns:mp="mx.MarketParameters" xmlns:fg="mx.MarketParameters.Fixing" xmlns:fgrt="mx.MarketParameters.Fixing.IRIndices" xmlns:xc="xmlCache" xmlns:fgfx="mx.MarketParameters.Fixing.FXIndices" xmlns:cmip="mx.MarketParameters.Commodities.Indices">
 <xc:XmlCache xc:action="Update">
  <xc:XmlCacheArea xc:value="MarketParameters">
   <mp:nickName xc:subset="Reference" xc:value="./BORATES">
    <mp:date xc:value="20161202">
     <fg:fixing>
      <cmfp:futurePrice>
       <cmfp:future xc:value="ONE">
        <cmfp:quotation xc:value="1.1">
         <cmfp:maturity xc:value="1.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
        <cmfp:quotation xc:value="1.2">
         <cmfp:maturity xc:value="1.2.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
         <cmfp:maturity xc:value="1.2.2">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13406.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
       <cmfp:future xc:value="FOUR">
        <cmfp:quotation xc:value="4.1">
         <cmfp:maturity xc:value="4.1.1">
          <cmfp:column xc:value="CLOSE" xc:type="Fields">
           <mp:HisValue xc:keyFormat="C">[startDate="19990101"][endDate="19990101"]13405.00</mp:HisValue>
          </cmfp:column>
         </cmfp:maturity>
        </cmfp:quotation>
       </cmfp:future>
      </cmfp:futurePrice>
     </fg:fixing>
    </mp:date>
   </mp:nickName>
  </xc:XmlCacheArea>
 </xc:XmlCache>
</GuiRoot>

[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13405.00
[startDate=“19990101”][endDate=“19990101”]13406.00
[startDate=“19990101”][endDate=“19990101”]13405.00
文件2 与文件1相似,但根据mod结果不同的节点

File3 与文件1相似,但根据mod结果不同的节点

感谢你的帮助。。。提前谢谢

我尝试了下面的XSLT代码,但它没有给我想要的输出….


我认为您首先需要分组,然后可以根据位置进行拆分,下面是XSLT 3.0的尝试(由Saxon 9.8(所有版本)或9.7 PE和EE支持):


要使用XSLT2.0处理器(如旧的Saxon 9版本)运行它,您必须使用例如

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>


您是否可以更详细地解释拆分的标准,而不是使用
xsl:mode

?为什么第一个结果文件有
cmfp:future xc:value=“ONE”和
cmfp:future xc:value=“FOUR”
,但是有两个不同的文件用于
2个
3个
值?谢谢Martin。我刚刚意识到我输入了错误的文件。基本上,我需要根据cmfp:future和cmfp:quote进行分组。我使用了基于节点位置的简单mod函数……在示例uniq cmfp:future中是一、二、三、四和八,即总共5个uniq值。如果我选择mod函数,一个和四个将在同一个文件中。两个和八个在同一个文件中,三个在不同的文件中。希望它能澄清。再次感谢你!非常感谢@Martin Honnen
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xpath-default-namespace="mx.MarketParameters.Fixing.Commodities.Futures"
    xmlns:xc="xmlCache"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:output indent="yes"/>

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

    <xsl:variable name="noOfSplits" select="3"/>

    <xsl:template match="/">
        <xsl:variable name="original-root-element" select="*"/>
        <xsl:variable name="groups">
            <xsl:for-each-group select="//future" group-by="@xc:value">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:for-each-group select="current-group()/quotation" group-by="@xc:value">
                        <xsl:copy>
                            <xsl:copy-of select="@*, current-group()/node()"/>
                        </xsl:copy>
                    </xsl:for-each-group>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:variable>
        <xsl:for-each-group select="$groups/future" group-by="(position() - 1) mod $noOfSplits">
          <xsl:result-document href="split{position()}.xml">
            <xsl:apply-templates select="$original-root-element">
                <xsl:with-param name="contents" select="current-group()" tunnel="yes"/>
            </xsl:apply-templates>
          </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="futurePrice">
        <xsl:param name="contents" tunnel="yes"/>
        <xsl:copy>
            <xsl:copy-of select="$contents"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>