次のフォームのネストされた (階層的) 構造を持つ大きな XML ファイルを変換する必要があります。
<Root>
Flat XML
Hierarchical XML (multiple blocks, some repetitive)
Flat XML
</Root>
繰り返しネストされたブロックごとに 1 つのブロックを持つ、よりフラットな (「細断された」) 形式に変換します。
データには多数の異なるタグと階層のバリエーション (特に、階層 XML の前後の断片化された XML のタグの数) があるため、理想的には、タグと属性の名前、または階層レベルについて仮定を行うべきではありません。
4 レベルのみの階層の最上位ビューは、次のようになります。
<Level 1>
...
<Level 2>
...
<Level 3>
...
<Level 4>A</Level 4>
<Level 4>B</Level 4>
...
</Level 3>
...
</Level 2>
...
</Level 1>
そして、望ましい出力は次のようになります
<Level 1>
...
<Level 2>
...
<Level 3>
...
<Level 4>A</Level 4>
...
</Level 3>
...
</Level 2>
...
</Level 1>
<Level 1>
...
<Level 2>
...
<Level 3>
...
<Level 4>B</Level 4>
...
</Level 3>
...
</Level 2>
...
</Level 1>
つまり、各レベルi
にLi
異なるコンポーネントがある場合、合計でProduct(Li)
異なるコンポーネントが生成されます (唯一の差別化要因はレベル 4 であるため、上の 2 つだけですL1*L2*L3*L4 = 2
)。
私が見た限りでは、XSLT が最適な方法かもしれませんが、他のソリューション (StAX や JDOM など) でも十分でしょう。
架空の情報を使用した、より詳細な例は次のようになります。
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="US">
<Comment>List of previous jobs in the US</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Senior Developer">
<StartDate>01/10/2001</StartDate>
<Months>38</Months>
</Job>
<Job title = "Senior Developer">
<StartDate>01/12/2004</StartDate>
<Months>6</Months>
</Job>
<Job title = "Senior Developer">
<StartDate>01/06/2005</StartDate>
<Months>10</Months>
</Job>
</JobDetails>
</Employment>
</EmploymentHistory>
<EmploymentHistory>
<Employment country="UK">
<Comment>List of previous jobs in the UK</Comment>
<Jobs>2</Jobs>
<JobDetails>
<Job title = "Junior Developer">
<StartDate>01/05/1999</StartDate>
<Months>25</Months>
</Job>
<Job title = "Junior Developer">
<StartDate>01/07/2001</StartDate>
<Months>3</Months>
</Job>
</JobDetails>
</Employment>
</EmploymentHistory>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employee>
上記のデータは、5 つのブロック (つまり、異なる<Job>
ブロックごとに 1 つ) に細分化する必要があります。各ブロックは、他のすべてのタグを同じままにし、要素を 1 つだけ持ち<Job>
ます。したがって、<Job>
上記の例で 5 つの異なるブロックがある場合、変換された (「断片化された」) XML は次のようになります。
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="US">
<Comment>List of previous jobs in the US</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Senior Developer">
<StartDate>01/10/2001</StartDate>
<Months>38</Months>
</Job>
</JobDetails>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employment>
</EmploymentHistory>
</Employee>
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="US">
<Comment>List of previous jobs in the US</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Senior Developer">
<StartDate>01/12/2004</StartDate>
<Months>6</Months>
</Job>
</JobDetails>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employment>
</EmploymentHistory>
</Employee>
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="US">
<Comment>List of previous jobs in the US</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Senior Developer">
<StartDate>01/06/2005</StartDate>
<Months>10</Months>
</Job>
</JobDetails>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employment>
</EmploymentHistory>
</Employee>
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="UK">
<Comment>List of previous jobs in the UK</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Junior Developer">
<StartDate>01/05/1999</StartDate>
<Months>25</Months>
</Job>
</JobDetails>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employment>
</EmploymentHistory>
</Employee>
<Employee name="A Name">
<Address>123 A Street</Address>
<Age>28</Age>
<EmploymentHistory>
<Employment country="UK">
<Comment>List of previous jobs in the UK</Comment>
<Jobs>3</Jobs>
<JobDetails>
<Job title = "Junior Developer">
<StartDate>01/07/2001</StartDate>
<Months>3</Months>
</Job>
</JobDetails>
<Available>true</Available>
<Experience unit="years">6</Experience>
</Employment>
</EmploymentHistory>
</Employee>