这是我关于如何在 .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() <= $size1]"/>
<xsl:apply-templates select="Line[position() > $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() < $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
作为Transform
方法的第二个参数)。