1

こんにちは、固定数の要素になるためにxmlを完成させる方法を探しています。この場合、"Top" 要素の数を常に 4 にする必要があります。追加する要素は空にする必要があります。これは、入力 xml の例です。

<Root>
<Group>
 <Top>
    <x>test1</x>
    <y>test2</y>
 </Top>
 <Top>
    <x>test8</x>
    <y>test23</y>
 </Top>
 <Top>
    <x>test22</x>
    <y>test2</y>
 </Top>
 <Top>
    <x>test52</x>
    <y>test27</y>
 </Top>
</Group>
<Group>
 <Top>
    <x>test18</x>
    <y>test39</y>
 </Top>
 </Group>
<Group>
 <Top>
    <x>test68</x>
    <y>test99</y>
 </Top>
 <Top>
    <x>test88</x>
    <y>test100</y>
 </Top>
 </Group>
</Root>

必要な出力は次のようになります。

<Root>
<Group>
 <Top>
    <x>test1</x>
    <y>test2</y>
 </Top>
 <Top>
    <x>test8</x>
    <y>test23</y>
 </Top>
 <Top>
    <x>test22</x>
    <y>test2</y>
 </Top>
 <Top>
    <x>test52</x>
    <y>test27</y>
 </Top>
</Group>
<Group>
 <Top>
    <x>test18</x>
    <y>test39</y>
 </Top>
 <Top>
    <x></x>
    <y></y>
 </Top>
 <Top>
    <x></x>
    <y></y>
 </Top>
 <Top>
    <x></x>
    <y></y>
 </Top>
 </Group>
<Group>
 <Top>
    <x>test68</x>
    <y>test99</y>
 </Top>
 <Top>
    <x>test88</x>
    <y>test100</y>
 </Top>
 <Top>
    <x></x>
    <y></y>
 </Top>
 <Top>
    <x></x>
    <y></y>
 </Top>
 </Group>
</Root>
4

3 に答える 3

0

1.0 の例は次のとおりです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:param name="TopRequired" select="4"/>

  <xsl:template match="Root">
    <Root><xsl:apply-templates/></Root>
  </xsl:template>

  <xsl:template match="Group">
    <xsl:variable name="CountTop" select="count(Top)"/>
    <xsl:choose>
      <xsl:when test="$CountTop = number($TopRequired)">
        <Group>
          <xsl:copy-of select="Top"/>
        </Group>
      </xsl:when>
      <xsl:otherwise>
        <Group>
          <xsl:copy-of select="Top"/>
          <xsl:call-template name="createDummyTop">
            <xsl:with-param name="RequiredTop" select="number($TopRequired) - $CountTop"/>
          </xsl:call-template>
        </Group>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="createDummyTop">
    <xsl:param name="RequiredTop"/>
    <xsl:if test="$RequiredTop != 0">
      <Top>
        <x/>
        <y/>
      </Top>
      <xsl:call-template name="createDummyTop">
        <xsl:with-param name="RequiredTop" select="$RequiredTop - 1"/>
      </xsl:call-template>
    </xsl:if>

  </xsl:template>

</xsl:stylesheet>

出力:

<?xml version="1.0" encoding="utf-8"?>
<Root>
   <Group>
      <Top>
         <x>test1</x>
         <y>test2</y>
      </Top>
      <Top>
         <x>test8</x>
         <y>test23</y>
      </Top>
      <Top>
         <x>test22</x>
         <y>test2</y>
      </Top>
      <Top>
         <x>test52</x>
         <y>test27</y>
      </Top>
   </Group>
   <Group>
      <Top>
         <x>test18</x>
         <y>test39</y>
      </Top>
      <Top>
         <x/>
         <y/>
      </Top>
      <Top>
         <x/>
         <y/>
      </Top>
      <Top>
         <x/>
         <y/>
      </Top>
   </Group>
   <Group>
      <Top>
         <x>test68</x>
         <y>test99</y>
      </Top>
      <Top>
         <x>test88</x>
         <y>test100</y>
      </Top>
      <Top>
         <x/>
         <y/>
      </Top>
      <Top>
         <x/>
         <y/>
      </Top>
   </Group>
</Root>
于 2013-08-02T04:43:59.623 に答える