1

次のような請求書データを使用しています (2 レコード):

<?xml version=1.0" encoding="Windows-1252"?>
<Line>
  <SupplierID>Waltons</SupplierID>
  <InvoiceID>MOD280229SI</InvoiceID>
  <InvoiceDate>20120709</InvoiceDate>
  <ItemID>847308</ItemID>
  <ColorID>04</ColorID>
  <Description>BANTEX Quotation Folder A4 3420</Description>
  <MainCategory>FILES FILING</MainCategory>
  <SubCategory>Quotation Folders</SubCategory>
  <LineNum>17.0000000</LineNum>
  <Qty>30.0000000</Qty>
  <UnitPriceExclTax>19.2500000</UnitPriceExclTax>
  <LineTax>80.8500000</LineTax>
  <LinePriceExclTax>577.5000000</LinePriceExclTax>
  <ColorName>Blue</ColorName>
  <UOM>Ea</UOM>
  <Backorder>0.0000000</Backorder>
  <INVENTTRANSID>MOD2923560_060</INVENTTRANSID>
</Line>
<Line>
  <SupplierID>Waltons</SupplierID>
  <InvoiceID>MOD280229SI</InvoiceID>
  <InvoiceDate>20120709</InvoiceDate>
  <ItemID>847308</ItemID>
  <ColorID>06</ColorID>
  <Description>BANTEX Quotation Folder A4 3420</Description>
  <MainCategory>FILES FILING</MainCategory>
  <SubCategory>Quotation Folders</SubCategory>
  <LineNum>18.0000000</LineNum>

上記の構造を念頭に置くと、注文は何百ものこれらの行で構成される可能性があります。

これらを請求書のページに印刷していますが、1 ページに収まるのは 18 行だけです。ページのオーバーフロー ルールはすでに機能しています。

私の唯一の問題は、最初のページに最初の 17 を印刷し、次に残高を繰り越すという言葉を印刷し、次のページで残高を繰り越すなどで開始する必要があることです。

XMLを次のように構造化したいと思います

最初の16行は変更なし 16行目以降 残高繰越のみで改行を挿入したい その後、合計17行の空行を挿入したい(最終行は意図的に空欄にしたい) 空行以降Balance Brought Forward を含む別の行を挿入します。この行の後、17 行目以降、さらに 15 行後、データの最後まで同じことを繰り返します。

このようなものを構築する上で、誰かが私を正しい方向に向けることができますか?

これは XSLT 1 または 2 で実行できます。または、VBS の JavaScript で実行することもできます。

私はXMLの構造を制御しています。最初にいくつかの小さな変更を加える必要があるため、サブ要素なしでフラットファイルとして出力しています。次に、これをXMLファイルの残りの部分とマージします。で終わります

行とはレコードを意味するので、上記のXMLは2行あります(2行目はコピーエラーのため不完全です)別の行を挿入することで、完全に新しいXXX要素を追加したいということです

40 行 (要素) で構成される XML から始めるとします。

<Line>
xxx
</Line>  x40

私が欲しいものは次のとおりです:

<Line> <!-- x16 lines. from data -->
xxx
</Line> 
<Line> - <!-- Inserted -->
<LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line> - <!-- Inserted -->
<Line></Line>
<Line> - <!-- Inserted -->
<LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line> <!-- x15 lines. From data -->
xxx
</Line>
<Line> <!-- Inserted -->
<LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line> <!-- Inserted -->
<Line></Line>
<Line> <!-- Inserted -->
<LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line> <!-- x9 lines. from data -->
xxx
</Line>
4

1 に答える 1

2

.NET フレームワークで XslCompiledTransform を使用してそれを解決する方法についての私の提案は次のとおりです。ConformanceLevel.Fragment を使用して XmlReader を使用して XML フラグメントを読み取ることができます。次に、次のような XSLT を使用して入力を処理できます。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:param name="size1" select="16"/>

  <xsl:param name="size2" select="15"/>

  <xsl:param name="to-be-inserted">
    <Line>
      <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
    </Line>
    <Line></Line>
    <Line>
      <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
    </Line>
  </xsl:param>

  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="/">
    <xsl:apply-templates select="Line[position() &lt;= $size1]"/>
    <xsl:apply-templates select="Line[position() &gt; $size1][position() mod $size2 = 1]" mode="group"/>
  </xsl:template>

  <xsl:template match="Line" mode="group">
    <xsl:copy-of select="$to-be-inserted"/>
    <xsl:apply-templates select=". | following-sibling::Line[position() &lt; $size2]"/>
  </xsl:template>
</xsl:stylesheet>

整形式の XML ドキュメントではない入力ファイルでそのスタイルシートを実行するには、次のようなコードが必要です。

        string sampleInput = @"input1.xml";


        string sampleResult = @"result1.xml";

        XslCompiledTransform proc = new XslCompiledTransform();
        proc.Load("sheet.xslt");

        using (XmlReader xr = XmlReader.Create(sampleInput, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment }))
        {
            using (FileStream fs = File.OpenWrite(sampleResult))
            {
                proc.Transform(xr, null, fs);
            }
        }

次のような入力ファイルがあると仮定します

<Line>
  <Id>1</Id>
</Line>
<Line>
  <Id>2</Id>
</Line>
<Line>
  <Id>3</Id>
</Line>
<Line>
  <Id>4</Id>
</Line>
<Line>
  <Id>5</Id>
</Line>
<Line>
  <Id>6</Id>
</Line>
<Line>
  <Id>7</Id>
</Line>
<Line>
  <Id>8</Id>
</Line>
<Line>
  <Id>9</Id>
</Line>
<Line>
  <Id>10</Id>
</Line>
<Line>
  <Id>11</Id>
</Line>
<Line>
  <Id>12</Id>
</Line>
<Line>
  <Id>13</Id>
</Line>
<Line>
  <Id>14</Id>
</Line>
<Line>
  <Id>15</Id>
</Line>
<Line>
  <Id>16</Id>
</Line>
<Line>
  <Id>17</Id>
</Line>
<Line>
  <Id>18</Id>
</Line>
<Line>
  <Id>19</Id>
</Line>
<Line>
  <Id>20</Id>
</Line>
<Line>
  <Id>21</Id>
</Line>
<Line>
  <Id>22</Id>
</Line>
<Line>
  <Id>23</Id>
</Line>
<Line>
  <Id>24</Id>
</Line>
<Line>
  <Id>25</Id>
</Line>
<Line>
  <Id>26</Id>
</Line>
<Line>
  <Id>27</Id>
</Line>
<Line>
  <Id>28</Id>
</Line>
<Line>
  <Id>29</Id>
</Line>
<Line>
  <Id>30</Id>
</Line>
<Line>
  <Id>31</Id>
</Line>
<Line>
  <Id>32</Id>
</Line>
<Line>
  <Id>33</Id>
</Line>
<Line>
  <Id>34</Id>
</Line>
<Line>
  <Id>35</Id>
</Line>
<Line>
  <Id>36</Id>
</Line>
<Line>
  <Id>37</Id>
</Line>
<Line>
  <Id>38</Id>
</Line>
<Line>
  <Id>39</Id>
</Line>
<Line>
  <Id>40</Id>
</Line>
<Line>
  <Id>41</Id>
</Line>
<Line>
  <Id>42</Id>
</Line>
<Line>
  <Id>43</Id>
</Line>
<Line>
  <Id>44</Id>
</Line>
<Line>
  <Id>45</Id>
</Line>
<Line>
  <Id>46</Id>
</Line>
<Line>
  <Id>47</Id>
</Line>
<Line>
  <Id>48</Id>
</Line>
<Line>
  <Id>49</Id>
</Line>
<Line>
  <Id>50</Id>
</Line>
<Line>
  <Id>51</Id>
</Line>
<Line>
  <Id>52</Id>
</Line>
<Line>
  <Id>53</Id>
</Line>
<Line>
  <Id>54</Id>
</Line>
<Line>
  <Id>55</Id>
</Line>
<Line>
  <Id>56</Id>
</Line>
<Line>
  <Id>57</Id>
</Line>
<Line>
  <Id>58</Id>
</Line>
<Line>
  <Id>59</Id>
</Line>
<Line>
  <Id>60</Id>
</Line>
<Line>
  <Id>61</Id>
</Line>
<Line>
  <Id>62</Id>
</Line>
<Line>
  <Id>63</Id>
</Line>
<Line>
  <Id>64</Id>
</Line>
<Line>
  <Id>65</Id>
</Line>
<Line>
  <Id>66</Id>
</Line>
<Line>
  <Id>67</Id>
</Line>
<Line>
  <Id>68</Id>
</Line>
<Line>
  <Id>69</Id>
</Line>
<Line>
  <Id>70</Id>
</Line>
<Line>
  <Id>71</Id>
</Line>
<Line>
  <Id>72</Id>
</Line>
<Line>
  <Id>73</Id>
</Line>
<Line>
  <Id>74</Id>
</Line>
<Line>
  <Id>75</Id>
</Line>
<Line>
  <Id>76</Id>
</Line>
<Line>
  <Id>77</Id>
</Line>
<Line>
  <Id>78</Id>
</Line>
<Line>
  <Id>79</Id>
</Line>
<Line>
  <Id>80</Id>
</Line>
<Line>
  <Id>81</Id>
</Line>
<Line>
  <Id>82</Id>
</Line>
<Line>
  <Id>83</Id>
</Line>
<Line>
  <Id>84</Id>
</Line>
<Line>
  <Id>85</Id>
</Line>
<Line>
  <Id>86</Id>
</Line>
<Line>
  <Id>87</Id>
</Line>
<Line>
  <Id>88</Id>
</Line>
<Line>
  <Id>89</Id>
</Line>
<Line>
  <Id>90</Id>
</Line>
<Line>
  <Id>91</Id>
</Line>
<Line>
  <Id>92</Id>
</Line>
<Line>
  <Id>93</Id>
</Line>
<Line>
  <Id>94</Id>
</Line>
<Line>
  <Id>95</Id>
</Line>
<Line>
  <Id>96</Id>
</Line>
<Line>
  <Id>97</Id>
</Line>
<Line>
  <Id>98</Id>
</Line>
<Line>
  <Id>99</Id>
</Line>
<Line>
  <Id>100</Id>
</Line>

次のような結果が得られます

<?xml version="1.0" encoding="utf-8"?>
<Line>
  <Id>1</Id>
</Line>
<Line>
  <Id>2</Id>
</Line>
<Line>
  <Id>3</Id>
</Line>
<Line>
  <Id>4</Id>
</Line>
<Line>
  <Id>5</Id>
</Line>
<Line>
  <Id>6</Id>
</Line>
<Line>
  <Id>7</Id>
</Line>
<Line>
  <Id>8</Id>
</Line>
<Line>
  <Id>9</Id>
</Line>
<Line>
  <Id>10</Id>
</Line>
<Line>
  <Id>11</Id>
</Line>
<Line>
  <Id>12</Id>
</Line>
<Line>
  <Id>13</Id>
</Line>
<Line>
  <Id>14</Id>
</Line>
<Line>
  <Id>15</Id>
</Line>
<Line>
  <Id>16</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>17</Id>
</Line>
<Line>
  <Id>18</Id>
</Line>
<Line>
  <Id>19</Id>
</Line>
<Line>
  <Id>20</Id>
</Line>
<Line>
  <Id>21</Id>
</Line>
<Line>
  <Id>22</Id>
</Line>
<Line>
  <Id>23</Id>
</Line>
<Line>
  <Id>24</Id>
</Line>
<Line>
  <Id>25</Id>
</Line>
<Line>
  <Id>26</Id>
</Line>
<Line>
  <Id>27</Id>
</Line>
<Line>
  <Id>28</Id>
</Line>
<Line>
  <Id>29</Id>
</Line>
<Line>
  <Id>30</Id>
</Line>
<Line>
  <Id>31</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>32</Id>
</Line>
<Line>
  <Id>33</Id>
</Line>
<Line>
  <Id>34</Id>
</Line>
<Line>
  <Id>35</Id>
</Line>
<Line>
  <Id>36</Id>
</Line>
<Line>
  <Id>37</Id>
</Line>
<Line>
  <Id>38</Id>
</Line>
<Line>
  <Id>39</Id>
</Line>
<Line>
  <Id>40</Id>
</Line>
<Line>
  <Id>41</Id>
</Line>
<Line>
  <Id>42</Id>
</Line>
<Line>
  <Id>43</Id>
</Line>
<Line>
  <Id>44</Id>
</Line>
<Line>
  <Id>45</Id>
</Line>
<Line>
  <Id>46</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>47</Id>
</Line>
<Line>
  <Id>48</Id>
</Line>
<Line>
  <Id>49</Id>
</Line>
<Line>
  <Id>50</Id>
</Line>
<Line>
  <Id>51</Id>
</Line>
<Line>
  <Id>52</Id>
</Line>
<Line>
  <Id>53</Id>
</Line>
<Line>
  <Id>54</Id>
</Line>
<Line>
  <Id>55</Id>
</Line>
<Line>
  <Id>56</Id>
</Line>
<Line>
  <Id>57</Id>
</Line>
<Line>
  <Id>58</Id>
</Line>
<Line>
  <Id>59</Id>
</Line>
<Line>
  <Id>60</Id>
</Line>
<Line>
  <Id>61</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>62</Id>
</Line>
<Line>
  <Id>63</Id>
</Line>
<Line>
  <Id>64</Id>
</Line>
<Line>
  <Id>65</Id>
</Line>
<Line>
  <Id>66</Id>
</Line>
<Line>
  <Id>67</Id>
</Line>
<Line>
  <Id>68</Id>
</Line>
<Line>
  <Id>69</Id>
</Line>
<Line>
  <Id>70</Id>
</Line>
<Line>
  <Id>71</Id>
</Line>
<Line>
  <Id>72</Id>
</Line>
<Line>
  <Id>73</Id>
</Line>
<Line>
  <Id>74</Id>
</Line>
<Line>
  <Id>75</Id>
</Line>
<Line>
  <Id>76</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>77</Id>
</Line>
<Line>
  <Id>78</Id>
</Line>
<Line>
  <Id>79</Id>
</Line>
<Line>
  <Id>80</Id>
</Line>
<Line>
  <Id>81</Id>
</Line>
<Line>
  <Id>82</Id>
</Line>
<Line>
  <Id>83</Id>
</Line>
<Line>
  <Id>84</Id>
</Line>
<Line>
  <Id>85</Id>
</Line>
<Line>
  <Id>86</Id>
</Line>
<Line>
  <Id>87</Id>
</Line>
<Line>
  <Id>88</Id>
</Line>
<Line>
  <Id>89</Id>
</Line>
<Line>
  <Id>90</Id>
</Line>
<Line>
  <Id>91</Id>
</Line>
<Line>
  <LinePriceExclTax>Balance Carried Forward</LinePriceExclTax>
</Line>
<Line />
<Line>
  <LinePriceExclTax>Balance Brought Forward</LinePriceExclTax>
</Line>
<Line>
  <Id>92</Id>
</Line>
<Line>
  <Id>93</Id>
</Line>
<Line>
  <Id>94</Id>
</Line>
<Line>
  <Id>95</Id>
</Line>
<Line>
  <Id>96</Id>
</Line>
<Line>
  <Id>97</Id>
</Line>
<Line>
  <Id>98</Id>
</Line>
<Line>
  <Id>99</Id>
</Line>
<Line>
  <Id>100</Id>
</Line>

Line新しい要素を挿入したい要素の数と挿入するコンテンツはスタイルシートのパラメーターであるため、スタイルシートを編集するか、コードで設定することもできます (メソッドXsltArgumentListの 2 番目の引数としてを渡しますTransform)。

于 2012-08-20T16:44:02.503 に答える