Как выполнить сортировку (в алфавитном порядке) в цикле for, как ожидается ниже в xslt или xsl-fo? - PullRequest
0 голосов
/ 07 января 2019

Я хочу создать таблицу, которая содержит два столбца как тип продукта и количество в XSL-Fo. Во входном XML-файле тип узла содержит 00, тогда в таблице продукт должен быть представлен как «Другой»

if Type element value is 
    00 then "Other" like wise
    51 = Business Loan – General
    05 = Personal Loan

Я хочу видеть все товары в алфавитном порядке возрастания.

Это мой входной XML

     <product>
                     <months>
                          <Type>00</Type>
                          <Number>2</Number>
                    </months>
                    <months>
                          <Type>51</Type>
                           <Number>2</Number>
                     </months>
                     <months>
                           <Type>05</Type>
                           <Number>1</Number>
                      </months>                    
            </product>

Я попробовал это здесь

     quantity
     <fo:table>
          <fo:table-body> 
             <fo:table-row>
                 <fo:table-cell>
                      <fo:block >Product Type</fo:block>
                 </fo:table-cell>
                 <fo:table-cell>
                      <fo:block>Quantity</fo:block>
                 </fo:table-cell>

     <xsl:for-each select=" Product/months">
     <xsl:variable name="Variable" select="Type"></xsl:variable>
      <fo:table-row>
             <fo:table-cell>
             <fo:block >      
                <xsl:choose>
                     <xsl:when test="Type[contains(text(),'05')]">
                        Personal Loan
                     </xsl:when>    
                     <xsl:when test="ProductType[contains(text(),'55')]">
                     Business Loan – General 
                     </xsl:when>
                     <xsl:when test="ProductType[contains(text(),'00')]">
                     Other
                     </xsl:when>
               </xsl:choose>
           </fo:block>
         </fo:table-cell>
        <fo:table-cell>
          <fo:block>
            <xsl:value-of select="Number"/>
          </fo:block>
        </fo:table-cell>
       </xsl:for-each>
     </fo:table-body>
    </fo:table> 

Я получил O / p, как показано ниже

                     Product Type            |            Quantity
   -----------------------------------------------------------
   Other                   |                2
   Business Loan – General |                2      
   Personal Loan           |                1

   but Actual Expected OUT/PUT is this

   Product Type            |            Quantity
   -----------------------------------------------------------
   Business Loan – General |                2
   Other                   |                2
   Personal Loan           |                1

Ответы [ 2 ]

0 голосов
/ 08 января 2019

Есть несколько способов посмотреть на это. Например, вы могли бы вообще избежать сортировки - особенно если у вас есть только 3 возможных типа продукта - применяя шаблоны к каждому типу по очереди.

Вот упрощенный пример:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/product">
    <table>
        <tr>
            <th>Product Type</th>
            <th>Quantity</th>
        </tr>
        <xsl:apply-templates select="months[Type='51']"/>
        <xsl:apply-templates select="months[Type='00']"/>
        <xsl:apply-templates select="months[Type='05']"/>
    </table>
</xsl:template>

<xsl:template match="months">
    <tr>
        <td>
            <xsl:choose>
                <xsl:when test="Type='51'">Business Loan – General</xsl:when>
                <xsl:when test="Type='05'">Personal Loan</xsl:when>
                <xsl:when test="Type='00'">Other</xsl:when>
            </xsl:choose>
        </td>
        <td>
            <xsl:value-of select="Number"/>
        </td>
    </tr>
</xsl:template>

</xsl:stylesheet>

Другой вариант - сортировка по индивидуальному заказу - снова, используя коды в их предполагаемом порядке:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/product">
    <table>
        <tr>
            <th>Product Type</th>
            <th>Quantity</th>
        </tr>
        <xsl:variable name="sort-order">|51|00|05|</xsl:variable>
        <xsl:for-each select="months">
            <xsl:sort select="string-length(substring-before($sort-order, concat('|', Type, '|')))" data-type="number"/>
            <tr>
                <td>
                    <xsl:choose>
                        <xsl:when test="Type='51'">Business Loan – General</xsl:when>
                        <xsl:when test="Type='05'">Personal Loan</xsl:when>
                        <xsl:when test="Type='00'">Other</xsl:when>
                    </xsl:choose>
                </td>
                <td>
                    <xsl:value-of select="Number"/>
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>
0 голосов
/ 08 января 2019

Если вы можете использовать XSLT 2.0, вы можете создать переменную для хранения элементов, которые представляют ваши отображения

<xsl:variable name="products">
  <map-entry key="05">Personal Loan</map-entry>
  <map-entry key="51">Business Loan – General</map-entry>
  <map-entry key="00">Other</map-entry>
</xsl:variable>

Затем вы можете использовать их для поиска по атрибуту key

 <xsl:key name="map" match="map-entry" use="@key" />

Затем вы используете этот ключ в некотором роде, например:

<xsl:for-each select="product/months">
  <xsl:sort select="key('map', Type, $products)" />

(Кроме того, убедитесь, что вы используете правильный регистр, как в вашем XSLT, который вы использовали Product, а не product)

Попробуйте этот XSLT, который вы можете увидеть в действии на http://xsltfiddle.liberty -development.net / pPzifoQ

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs"
    version="2.0">

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

  <xsl:variable name="products">
    <map-entry key="05">Personal Loan</map-entry>
    <map-entry key="51">Business Loan – General</map-entry>
    <map-entry key="00">Other</map-entry>
  </xsl:variable>

  <xsl:key name="map" match="map-entry" use="@key" />

  <xsl:template match="/">
    <fo:table>
      <fo:table-body> 
        <fo:table-row>
          <fo:table-cell>
            <fo:block>Product Type</fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>Quantity</fo:block>
          </fo:table-cell>
        </fo:table-row>
        <xsl:for-each select="product/months">
          <xsl:sort select="key('map', Type, $products)" />
          <fo:table-row>
            <fo:table-cell>
              <fo:block>
                <xsl:value-of select="key('map', Type, $products)" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block>
                <xsl:value-of select="Number"/>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </xsl:for-each>
      </fo:table-body>
    </fo:table> 
  </xsl:template>
</xsl:stylesheet>

Обратите внимание: если вы можете использовать XSLT 3.0, вы можете использовать специальную функцию "map", которая немного упрощает вещи

Попробуйте этот XSLT 3.0, который вы можете увидеть в действии на http://xsltfiddle.liberty -development.net / bnnZVx

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs"
    version="3.0">

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

  <xsl:variable name="products" as="map(xs:string, xs:string)">
    <xsl:map>
      <xsl:map-entry key="'05'" select="'Personal Loan'"/>
      <xsl:map-entry key="'51'" select="'Business Loan – General'"/>
      <xsl:map-entry key="'00'" select="'Other'"/>
    </xsl:map>
  </xsl:variable>

  <xsl:template match="/">
    <fo:table>
      <fo:table-body> 
        <fo:table-row>
          <fo:table-cell>
            <fo:block>Product Type</fo:block>
          </fo:table-cell>
          <fo:table-cell>
            <fo:block>Quantity</fo:block>
          </fo:table-cell>
        </fo:table-row>
        <xsl:for-each select="product/months">
          <xsl:sort select="$products(Type)" />
          <fo:table-row>
            <fo:table-cell>
              <fo:block>
                <xsl:value-of select="$products(Type)" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block>
                <xsl:value-of select="Number"/>
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </xsl:for-each>
      </fo:table-body>
    </fo:table> 
  </xsl:template>
</xsl:stylesheet>
...