На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
Дорогие друзья! Поздравляем вас с днём Победы!
msm.ru
Модераторы: ANDLL
Страницы: (2) 1 [2]  все  ( Перейти к последнему сообщению )  
> Интересная задачка по XSL , хитро отформатировать xml-ку
    ExpandedWrap disabled
      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
        
        <xsl:template match="/">
           <xsl:apply-templates select="intermediate/type"/>
        </xsl:template>
       
        <xsl:template match="type">
           <xsl:apply-templates select="field[1]" mode="recursive-combine">
              <xsl:with-param name="wire-name" select="@wireName"/>
           </xsl:apply-templates>
        </xsl:template>
        
        <xsl:template match="field[@isSimple='yes']" mode="recursive-combine">
          <xsl:param name="accumulated" select="node()"/>
          <xsl:param name="wire-name"/>
          <xsl:param name="hidden" select="false()"/>  <!-- в первый раз никто не скрыт, то есть опциональный field будет выведен -->
       
          <xsl:variable name="is-last" select="not(following-sibling::field)"/>
          <xsl:variable name="is-simple" select="@isSimple='yes'"/>
          <xsl:variable name="new-accumulated" select="$accumulated | current()[not($hidden) and $is-simple]"/>
       
           <xsl:if test="not($is-last)">
              <xsl:apply-templates select="following-sibling::field[1]" mode="recursive-combine">
                 <xsl:with-param name="accumulated" select="$new-accumulated"/>
                 <xsl:with-param name="wire-name" select="$wire-name"/>
              </xsl:apply-templates>
           </xsl:if>
       
           <xsl:apply-templates select="current()[$is-last]" mode="output">
              <xsl:with-param name="accumulated" select="$new-accumulated"/>
              <xsl:with-param name="wire-name" select="$wire-name"/>
           </xsl:apply-templates>
       
          <xsl:apply-templates select="current()[@isOptional='yes'][not($hidden)][$is-simple]" mode="recursive-combine">
              <xsl:with-param name="hidden" select="true()"/>
              <xsl:with-param name="accumulated" select="$accumulated"/>
              <xsl:with-param name="wire-name" select="$wire-name"/>
          </xsl:apply-templates>
        </xsl:template>
       
        <xsl:template match="*" mode="recursive-combine"/>
       
        <xsl:template match="field" mode="output">
             <xsl:param name="accumulated"/>
             <xsl:param name="wire-name"/>
       
             <xsl:value-of select="concat('- ', $wire-name, ': [')"/>
             <xsl:for-each select="$accumulated">
                 <xsl:value-of select="concat( substring(', ', 1 div (not(position()=1)) ), @name )" />
             </xsl:for-each>
             <xsl:value-of select="concat(']', '&#xa;')" />
         </xsl:template>
      </xsl:stylesheet>

    1. нафиг чойз в выводе, если до вывода доходят только простые поля?
    2. переделал на ноде-сеты (смотри внимательно на парметр accumulated), потому нужно допилить под твой парсер - каждый по-своему с сетами работает.
      В строке 15:
      ExpandedWrap disabled
         <xsl:template match="field[@isSimple='yes']" mode="recursive-combine">

      убрал [@isSimple='yes'], чтобы были выведены пустые элементы. Т.е. для
      ExpandedWrap disabled
          <type wireName="box" name="Box" >
             <field name="rightCornerPoint" type="Point" isOptional="no" wireName="point" isSimple="no"/>
             <field name="leftCornerPoint"  type="Point" isOptional="no" wireName="point" isSimple="no"/>
          </type>
      был такой вывод:
      ExpandedWrap disabled
        - box: []

      Все остальное, вроде, правильно. Еще раз большое спасибо!!!
      0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
      0 пользователей:


      Рейтинг@Mail.ru
      [ Script execution time: 0,0243 ]   [ 15 queries used ]   [ Generated: 9.05.24, 11:56 GMT ]