2017-01-05 11 views
0

これは以前に尋ねられたかもしれませんが、個別のファイルに分割する必要があるXMLファイルがあります。XMLファイルを複数に分割します

<?xml version="1.0" encoding="UTF-8"?> 
<invoices> 
    <invoice> 
     <data> 

     </data> 
    </invoice> 
    <invoice> 
     <data> 

     </data> 
    </invoice> 
</invoices> 

そして、私はこのように分割する私のコードがあります:

ファイルがある

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="xsl"> 
    <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/> 
    <xsl:template match="/"> 
     <tosplit> 
      <xsl:for-each-group select="//invoice" group-by="ceiling(position() div 1)"> 
       <xsl:apply-templates select="/" mode="copy"> 
        <xsl:with-param name="currentgroup" select="current-group()"/> 
       </xsl:apply-templates> 
      </xsl:for-each-group> 
     </tosplit> 
    </xsl:template> 
    <xsl:template match="@*|node()" mode="copy"> 
     <xsl:param name="currentgroup"/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" mode="copy"> 
       <xsl:with-param name="currentgroup" select="current-group()"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="invoices" mode="copy"> 
     <xsl:param name="currentgroup"/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*" mode="copy"/> 
      <xsl:apply-templates select="$currentgroup" mode="copy"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

これを、しかし、トリックを行いません。私は各ファイルを見たいと思っています

<?xml version="1.0" encoding="UTF-8"?> 
<tosplit> 
    <invoices> 
     <invoice> 
     <data> 

     </data> 
     </invoice> 
    </invoices> 
</tosplit> 

助けていただければ幸いです。

+0

['xsl:result-document'](https://www.w3.org/TR/xslt20/#creating-result-trees)について学んでください。 –

答えて

0

あなたはXMLSpyの2017年にサクソン9 PEまたはEE酸素中またはAltovaのラプターのようなXSLT 3.0プロセッサへのアクセス権を持っているなら、あなたが必要とするすべては、各invoice要素の1つの結果文書を作成するための

<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" 
    exclude-result-prefixes="xs math" 
    version="3.0"> 

    <xsl:mode on-no-match="shallow-skip" streamable="yes"/> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <xsl:template match="invoice"> 
     <xsl:result-document href="invoice-split{position()}.xml"> 
      <xsl:copy-of select="snapshot(.)/root()"/> 
     </xsl:result-document> 
    </xsl:template> 

</xsl:stylesheet> 

です。

0

てみましょう最初のコードが

<xsl:template match="/"> 
     <tosplit> 
      <xsl:for-each-group select="//invoice" group-by="ceiling(position() div 1)"> 
       <xsl:apply-templates select="/" mode="copy"> 
        <xsl:with-param name="currentgroup" select="current-group()"/> 
       </xsl:apply-templates> 
      </xsl:for-each-group> 
     </tosplit> 
    </xsl:template> 

機能しない理由を私は本当にあなたがそのグループごとの属性を念頭に置いていたのか分からない参照してください。 Xが整数の場合(position()が)の場合、ceiling(X div 1)はXをそのまま返します。したがって、group-by = "position()"と同じです。各項目の位置が異なるため、各項目は別々のグループに入れられます。

<xsl:template match="/"> 
    <tosplit> 
     <xsl:for-each select="//invoice" > 
      <xsl:apply-templates select="/" mode="copy"> 
       <xsl:with-param name="currentgroup" select="."/> 
      </xsl:apply-templates> 
     </xsl:for-each> 
    </tosplit> 
</xsl:template> 

基本的に

<xsl:template match="/"> 
    <tosplit> 
     <xsl:apply-templates select="//invoice" mode="copy"/> 
    </tosplit> 
</xsl:template> 

として、より自然に行うことができませんでした何かを達成しない順番に、あなたがやっていない:それはあなたのコードがやっての複雑な方法であることを意味しグループ化を行う必要はありません。したがって、このコードが何であるかを知ることは困難です。

同時に、請求書ごとに1つのファイルを作成したいが、コードで複数の出力ファイルを作成しようとしていないとします。

関連する問題