проверка наличия тега в файле XML - PullRequest
0 голосов
/ 16 ноября 2011

Я пытаюсь получить доступ к событиям XML, таким как этот (выдержка следует):

<root ID="1">
  <SPORT NAME="Tennis" ID="100005">
    <EVENT NAME="ATP Tour" ID="366156">
      <SUBEVENT NAME="ATP World Tour Finals 2011" ID="996723" DATE="2011-11-20 12:00:00">
        <SUBEVENT1 NAME="Atp tour bla bla bla" ID="996723" DATE="2011-11-20 12:00:00">
         <SUBEVENT2 NAME="(Sun) R Federer v JW Tsonga (TV)" ID="997624" DATE="2011-11-20 14:00:00">
          <MARKET NAME="Match Betting" ID="2369945" ODDS_SYSTEM="BL">
            <LINK URL="http://www.betdaq.com?mktid=2369945" />
            <SELECTION NAME="R Federer" ID="12773216">
              <OUTCOME NAME="|BACK| R Federer">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="1.23">
                    <AMOUNT CURRENCY="USD" VALUE="2057" />
                    <AMOUNT CURRENCY="GBP" VALUE="1303" />
                    <AMOUNT CURRENCY="EUR" VALUE="1518" />
                  </PRICE>
                  <PRICE VALUE="1.18">
                    <AMOUNT CURRENCY="USD" VALUE="2720" />
                    <AMOUNT CURRENCY="GBP" VALUE="1723" />
                    <AMOUNT CURRENCY="EUR" VALUE="2008" />
                  </PRICE>
                  <PRICE VALUE="1.13">
                    <AMOUNT CURRENCY="USD" VALUE="3128" />
                    <AMOUNT CURRENCY="GBP" VALUE="1981" />
                    <AMOUNT CURRENCY="EUR" VALUE="2309" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| R Federer">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="1.31">
                    <AMOUNT CURRENCY="USD" VALUE="2" />
                    <AMOUNT CURRENCY="GBP" VALUE="1" />
                    <AMOUNT CURRENCY="EUR" VALUE="1" />
                  </PRICE>
                  <PRICE VALUE="1.38">
                    <AMOUNT CURRENCY="USD" VALUE="1246" />
                    <AMOUNT CURRENCY="GBP" VALUE="789" />
                    <AMOUNT CURRENCY="EUR" VALUE="919" />
                  </PRICE>
                  <PRICE VALUE="1.43">
                    <AMOUNT CURRENCY="USD" VALUE="1266" />
                    <AMOUNT CURRENCY="GBP" VALUE="802" />
                    <AMOUNT CURRENCY="EUR" VALUE="934" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <SELECTION NAME="JW Tsonga" ID="12773217">
              <OUTCOME NAME="|BACK| JW Tsonga">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="3.65">
                    <AMOUNT CURRENCY="USD" VALUE="178" />
                    <AMOUNT CURRENCY="GBP" VALUE="113" />
                    <AMOUNT CURRENCY="EUR" VALUE="131" />
                  </PRICE>
                  <PRICE VALUE="3.40">
                    <AMOUNT CURRENCY="USD" VALUE="226" />
                    <AMOUNT CURRENCY="GBP" VALUE="143" />
                    <AMOUNT CURRENCY="EUR" VALUE="167" />
                  </PRICE>
                  <PRICE VALUE="3.15">
                    <AMOUNT CURRENCY="USD" VALUE="291" />
                    <AMOUNT CURRENCY="GBP" VALUE="184" />
                    <AMOUNT CURRENCY="EUR" VALUE="214" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| JW Tsonga">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="5.10">
                    <AMOUNT CURRENCY="USD" VALUE="115" />
                    <AMOUNT CURRENCY="GBP" VALUE="73" />
                    <AMOUNT CURRENCY="EUR" VALUE="85" />
                  </PRICE>
                  <PRICE VALUE="5.60">
                    <AMOUNT CURRENCY="USD" VALUE="118" />
                    <AMOUNT CURRENCY="GBP" VALUE="74" />
                    <AMOUNT CURRENCY="EUR" VALUE="87" />
                  </PRICE>
                  <PRICE VALUE="6.20">
                    <AMOUNT CURRENCY="USD" VALUE="120" />
                    <AMOUNT CURRENCY="GBP" VALUE="76" />
                    <AMOUNT CURRENCY="EUR" VALUE="88" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
          </MARKET>
         </SUBEVENT2> 
         <!--- more <SUBEVENT2>s -->
        </SUBEVENT1>
        <SUBEVENT1 NAME="(Sun) R Nadal v M Fish (TV)" ID="997625" DATE="2011-11-20 20:00:00">
          <MARKET NAME="Match Betting" ID="2369947" ODDS_SYSTEM="BL">
            <LINK URL="http://www.betdaq.com?mktid=2369947" />
            <SELECTION NAME="R Nadal" ID="12773222">
              <OUTCOME NAME="|BACK| R Nadal">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="1.20">
                    <AMOUNT CURRENCY="USD" VALUE="13" />
                    <AMOUNT CURRENCY="GBP" VALUE="8" />
                    <AMOUNT CURRENCY="EUR" VALUE="9" />
                  </PRICE>
                  <PRICE VALUE="1.19">
                    <AMOUNT CURRENCY="USD" VALUE="2367" />
                    <AMOUNT CURRENCY="GBP" VALUE="1499" />
                    <AMOUNT CURRENCY="EUR" VALUE="1747" />
                  </PRICE>
                  <PRICE VALUE="1.14">
                    <AMOUNT CURRENCY="USD" VALUE="2722" />
                    <AMOUNT CURRENCY="GBP" VALUE="1724" />
                    <AMOUNT CURRENCY="EUR" VALUE="2009" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| R Nadal">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="1.30">
                    <AMOUNT CURRENCY="USD" VALUE="1578" />
                    <AMOUNT CURRENCY="GBP" VALUE="999" />
                    <AMOUNT CURRENCY="EUR" VALUE="1165" />
                  </PRICE>
                  <PRICE VALUE="1.35">
                    <AMOUNT CURRENCY="USD" VALUE="1555" />
                    <AMOUNT CURRENCY="GBP" VALUE="985" />
                    <AMOUNT CURRENCY="EUR" VALUE="1148" />
                  </PRICE>
                  <PRICE VALUE="1.40">
                    <AMOUNT CURRENCY="USD" VALUE="1565" />
                    <AMOUNT CURRENCY="GBP" VALUE="991" />
                    <AMOUNT CURRENCY="EUR" VALUE="1155" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <SELECTION NAME="M Fish" ID="12773223">
              <OUTCOME NAME="|BACK| M Fish">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="4.40">
                    <AMOUNT CURRENCY="USD" VALUE="139" />
                    <AMOUNT CURRENCY="GBP" VALUE="88" />
                    <AMOUNT CURRENCY="EUR" VALUE="102" />
                  </PRICE>
                  <PRICE VALUE="3.95">
                    <AMOUNT CURRENCY="USD" VALUE="184" />
                    <AMOUNT CURRENCY="GBP" VALUE="116" />
                    <AMOUNT CURRENCY="EUR" VALUE="136" />
                  </PRICE>
                  <PRICE VALUE="3.70">
                    <AMOUNT CURRENCY="USD" VALUE="231" />
                    <AMOUNT CURRENCY="GBP" VALUE="146" />
                    <AMOUNT CURRENCY="EUR" VALUE="171" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| M Fish">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="6.00">
                    <AMOUNT CURRENCY="USD" VALUE="2" />
                    <AMOUNT CURRENCY="GBP" VALUE="1" />
                    <AMOUNT CURRENCY="EUR" VALUE="2" />
                  </PRICE>
                  <PRICE VALUE="6.20">
                    <AMOUNT CURRENCY="USD" VALUE="91" />
                    <AMOUNT CURRENCY="GBP" VALUE="57" />
                    <AMOUNT CURRENCY="EUR" VALUE="67" />
                  </PRICE>
                  <PRICE VALUE="7.20">
                    <AMOUNT CURRENCY="USD" VALUE="87" />
                    <AMOUNT CURRENCY="GBP" VALUE="55" />
                    <AMOUNT CURRENCY="EUR" VALUE="64" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <!--- more <SELECTION>s -->
          </MARKET>
          <!--- more <MARKET>s -->
        </SUBEVENT1>
        <!--- more <SUBEVENT1>s -->
      </SUBEVENT>
      <!--- more <SUBEVENT>s -->
    </EVENT>
    <!--- more <EVENT>s -->
  </SPORT>
</root>

это мой XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <xsl:for-each select="root/SPORT[@NAME='Tennis']/EVENT">
      <xsl:for-each select="./SUBEVENT">
        <xsl:for-each select="./SUBEVENT1">
          <xsl:for-each select="./SUBEVENT2">
            <xsl:sort select="./MARKET/@DATE"/>
            <xsl:if test="./MARKET[@NAME='Match Betting']">
              <xsl:value-of select="./@DATE"/>
              <xsl:value-of select="','"/>
              <xsl:value-of select="./@NAME"/>
              <xsl:value-of select="','"/>
              <xsl:value-of select="' '"/>
              <xsl:choose>
                <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]">
                  <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'0.00'"/>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:value-of select="','"/>
              <xsl:choose>
                <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]">
                  <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'0.00'"/>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:value-of select="','"/>
              <xsl:choose>
                <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]">
                  <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'0.00'"/>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:value-of select="','"/>
              <xsl:choose>
                <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]">
                  <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'0.00'"/>
                </xsl:otherwise>
              </xsl:choose>
              <xsl:value-of select="'&#10;'"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Моя проблема в том, что иногда вложенные <SUBEVENT1> и <SUBEVENT2> отсутствуют, и событие представляется в основном вложенном событии.пример:

<root ID="1">
  <SPORT NAME="Tennis" ID="100005">
    <EVENT NAME="ATP Tour" ID="366156">
        <SUBEVENT NAME="(Sun) R Federer v JW Tsonga (TV)" ID="997624" DATE="2011-11-20 14:00:00">
          <MARKET NAME="Match Betting" ID="2369945" ODDS_SYSTEM="BL">
            <LINK URL="http://www.betdaq.com?mktid=2369945" />
            <SELECTION NAME="R Federer" ID="12773216">
              <OUTCOME NAME="|BACK| R Federer">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="1.23">
                    <AMOUNT CURRENCY="USD" VALUE="2057" />
                    <AMOUNT CURRENCY="GBP" VALUE="1303" />
                    <AMOUNT CURRENCY="EUR" VALUE="1518" />
                  </PRICE>
                  <PRICE VALUE="1.18">
                    <AMOUNT CURRENCY="USD" VALUE="2720" />
                    <AMOUNT CURRENCY="GBP" VALUE="1723" />
                    <AMOUNT CURRENCY="EUR" VALUE="2008" />
                  </PRICE>
                  <PRICE VALUE="1.13">
                    <AMOUNT CURRENCY="USD" VALUE="3128" />
                    <AMOUNT CURRENCY="GBP" VALUE="1981" />
                    <AMOUNT CURRENCY="EUR" VALUE="2309" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| R Federer">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="1.31">
                    <AMOUNT CURRENCY="USD" VALUE="2" />
                    <AMOUNT CURRENCY="GBP" VALUE="1" />
                    <AMOUNT CURRENCY="EUR" VALUE="1" />
                  </PRICE>
                  <PRICE VALUE="1.38">
                    <AMOUNT CURRENCY="USD" VALUE="1246" />
                    <AMOUNT CURRENCY="GBP" VALUE="789" />
                    <AMOUNT CURRENCY="EUR" VALUE="919" />
                  </PRICE>
                  <PRICE VALUE="1.43">
                    <AMOUNT CURRENCY="USD" VALUE="1266" />
                    <AMOUNT CURRENCY="GBP" VALUE="802" />
                    <AMOUNT CURRENCY="EUR" VALUE="934" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <SELECTION NAME="JW Tsonga" ID="12773217">
              <OUTCOME NAME="|BACK| JW Tsonga">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="3.65">
                    <AMOUNT CURRENCY="USD" VALUE="178" />
                    <AMOUNT CURRENCY="GBP" VALUE="113" />
                    <AMOUNT CURRENCY="EUR" VALUE="131" />
                  </PRICE>
                  <PRICE VALUE="3.40">
                    <AMOUNT CURRENCY="USD" VALUE="226" />
                    <AMOUNT CURRENCY="GBP" VALUE="143" />
                    <AMOUNT CURRENCY="EUR" VALUE="167" />
                  </PRICE>
                  <PRICE VALUE="3.15">
                    <AMOUNT CURRENCY="USD" VALUE="291" />
                    <AMOUNT CURRENCY="GBP" VALUE="184" />
                    <AMOUNT CURRENCY="EUR" VALUE="214" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| JW Tsonga">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="5.10">
                    <AMOUNT CURRENCY="USD" VALUE="115" />
                    <AMOUNT CURRENCY="GBP" VALUE="73" />
                    <AMOUNT CURRENCY="EUR" VALUE="85" />
                  </PRICE>
                  <PRICE VALUE="5.60">
                    <AMOUNT CURRENCY="USD" VALUE="118" />
                    <AMOUNT CURRENCY="GBP" VALUE="74" />
                    <AMOUNT CURRENCY="EUR" VALUE="87" />
                  </PRICE>
                  <PRICE VALUE="6.20">
                    <AMOUNT CURRENCY="USD" VALUE="120" />
                    <AMOUNT CURRENCY="GBP" VALUE="76" />
                    <AMOUNT CURRENCY="EUR" VALUE="88" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
          </MARKET>
        </SUBEVENT>
        <SUBEVENT NAME="(Sun) R Nadal v M Fish (TV)" ID="997625" DATE="2011-11-20 20:00:00">
          <MARKET NAME="Match Betting" ID="2369947" ODDS_SYSTEM="BL">
            <LINK URL="http://www.betdaq.com?mktid=2369947" />
            <SELECTION NAME="R Nadal" ID="12773222">
              <OUTCOME NAME="|BACK| R Nadal">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="1.20">
                    <AMOUNT CURRENCY="USD" VALUE="13" />
                    <AMOUNT CURRENCY="GBP" VALUE="8" />
                    <AMOUNT CURRENCY="EUR" VALUE="9" />
                  </PRICE>
                  <PRICE VALUE="1.19">
                    <AMOUNT CURRENCY="USD" VALUE="2367" />
                    <AMOUNT CURRENCY="GBP" VALUE="1499" />
                    <AMOUNT CURRENCY="EUR" VALUE="1747" />
                  </PRICE>
                  <PRICE VALUE="1.14">
                    <AMOUNT CURRENCY="USD" VALUE="2722" />
                    <AMOUNT CURRENCY="GBP" VALUE="1724" />
                    <AMOUNT CURRENCY="EUR" VALUE="2009" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| R Nadal">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="1.30">
                    <AMOUNT CURRENCY="USD" VALUE="1578" />
                    <AMOUNT CURRENCY="GBP" VALUE="999" />
                    <AMOUNT CURRENCY="EUR" VALUE="1165" />
                  </PRICE>
                  <PRICE VALUE="1.35">
                    <AMOUNT CURRENCY="USD" VALUE="1555" />
                    <AMOUNT CURRENCY="GBP" VALUE="985" />
                    <AMOUNT CURRENCY="EUR" VALUE="1148" />
                  </PRICE>
                  <PRICE VALUE="1.40">
                    <AMOUNT CURRENCY="USD" VALUE="1565" />
                    <AMOUNT CURRENCY="GBP" VALUE="991" />
                    <AMOUNT CURRENCY="EUR" VALUE="1155" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <SELECTION NAME="M Fish" ID="12773223">
              <OUTCOME NAME="|BACK| M Fish">
                <ODDS POLARITY="BACK">
                  <PRICE VALUE="4.40">
                    <AMOUNT CURRENCY="USD" VALUE="139" />
                    <AMOUNT CURRENCY="GBP" VALUE="88" />
                    <AMOUNT CURRENCY="EUR" VALUE="102" />
                  </PRICE>
                  <PRICE VALUE="3.95">
                    <AMOUNT CURRENCY="USD" VALUE="184" />
                    <AMOUNT CURRENCY="GBP" VALUE="116" />
                    <AMOUNT CURRENCY="EUR" VALUE="136" />
                  </PRICE>
                  <PRICE VALUE="3.70">
                    <AMOUNT CURRENCY="USD" VALUE="231" />
                    <AMOUNT CURRENCY="GBP" VALUE="146" />
                    <AMOUNT CURRENCY="EUR" VALUE="171" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
              <OUTCOME NAME="|LAY| M Fish">
                <ODDS POLARITY="LAY">
                  <PRICE VALUE="6.00">
                    <AMOUNT CURRENCY="USD" VALUE="2" />
                    <AMOUNT CURRENCY="GBP" VALUE="1" />
                    <AMOUNT CURRENCY="EUR" VALUE="2" />
                  </PRICE>
                  <PRICE VALUE="6.20">
                    <AMOUNT CURRENCY="USD" VALUE="91" />
                    <AMOUNT CURRENCY="GBP" VALUE="57" />
                    <AMOUNT CURRENCY="EUR" VALUE="67" />
                  </PRICE>
                  <PRICE VALUE="7.20">
                    <AMOUNT CURRENCY="USD" VALUE="87" />
                    <AMOUNT CURRENCY="GBP" VALUE="55" />
                    <AMOUNT CURRENCY="EUR" VALUE="64" />
                  </PRICE>
                </ODDS>
              </OUTCOME>
            </SELECTION>
            <!--- more <SELECTION>s -->
          </MARKET>
          <!--- more <MARKET>s -->
       </SUBEVENT>
      <!--- more <SUBEVENT>s -->
    </EVENT>
    <!--- more <EVENT>s -->
  </SPORT>
</root>

вывод в следующем виде:

2011-11-20 14:00:00,(Sun) R Federer v JW Tsonga (TV), 1.23,3.65,1.31,5.10
2011-11-20 20:00:00,(Sun) R Nadal v M Fish (TV), 1.20,4.40,1.30,6.00
2011-11-21 14:00:00,(Mon) A Murray v D Ferrer (TV), 1.13,5.40,1.20,8.20
2011-11-21 20:00:00,(Mon) N Djokovic v T Berdych (TV), 1.42,2.72,1.60,3.40

Таким образом, мой XSLT-скрипт не способен видеть события.Как мне изменить XSLT, чтобы в этом случае учесть?

Ответы [ 2 ]

1 голос
/ 18 ноября 2011

Похоже, это довольно далеко зашло бы:

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

  <xsl:template match="/">
    <xsl:apply-templates select="
      root/SPORT[@NAME='Tennis']/EVENT//MARKET[@NAME='Match Betting']
    " />
  </xsl:template>

  <xsl:template match="MARKET[@NAME='Match Betting']">
    <xsl:value-of select="../@DATE"/><xsl:text>,</xsl:text>
    <xsl:value-of select="../@NAME"/><xsl:text>, </xsl:text>
    <xsl:apply-templates select="SELECTION[1]/OUTCOME/ODDS[@POLARITY='BACK']" /><xsl:text>,</xsl:text>
    <xsl:apply-templates select="SELECTION[2]/OUTCOME/ODDS[@POLARITY='BACK']" /><xsl:text>,</xsl:text>
    <xsl:apply-templates select="SELECTION[1]/OUTCOME/ODDS[@POLARITY='LAY']" /><xsl:text>,</xsl:text>
    <xsl:apply-templates select="SELECTION[2]/OUTCOME/ODDS[@POLARITY='LAY']" /><xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="ODDS[PRICE/@VALUE]"><!-- odds with a price value -->
    <xsl:value-of select="PRICE/@VALUE"/>
  </xsl:template>

  <xsl:template match="ODDS"><!-- odds without a price value -->
    <xsl:text>0.00</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Для обоих примеров XML в вашем вопросе это дает следующий вывод:

2011-11-20 14:00:00,(Sun) R Federer v JW Tsonga (TV), 1.23,3.65,1.31,5.10
2011-11-20 20:00:00,(Sun) R Nadal v M Fish (TV), 1.20,4.40,1.30,6.00

Если вам все равно, есть ли <SUBEVENT1> / <SUBEVENT2> элементы или нет, просто игнорируйте их.

Кажется, вам интересны элементы <MARKET NAME="Match Betting">, поэтому выберите их напрямую:

root/SPORT[@NAME='Tennis']/EVENT//MARKET[@NAME='Match Betting']

Второй шаблон соответствует им и создает строку вывода.Третий и четвертый шаблоны обрабатывают оба типа элементов <ODDS>.

Вы можете видеть, что XSLT может быть коротким и эффективным, если все сделано правильно.Для начала стоит забыть о <xsl:for-each>.Звучит полезно, но это не так.

Взгляните на <xsl:apply-templates>.Он работает как для каждого, но гораздо гибче.

1 голос
/ 17 ноября 2011

Не зная точно, чего вы пытаетесь достичь, и без правильно сформированного вклада, я думаю, что вы пытаетесь сделать ...

Ниже приведена измененная версия вашей таблицы стилей. Основная часть этого неизменна; в основном я изменил match и один for-each.

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

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

  <xsl:template match="SUBEVENT2|SUBEVENT1[not(SUBEVENT2)]|SUBEVENT[not(SUBEVENT1)]">
    <xsl:for-each select=".">
      <xsl:sort select="./MARKET/@DATE"/>
      <xsl:if test="./MARKET[@NAME='Match Betting']">
        <xsl:value-of select="./@DATE"/>
        <xsl:value-of select="','"/>
        <xsl:value-of select="./@NAME"/>
        <xsl:value-of select="','"/>
        <xsl:value-of select="' '"/>
        <xsl:choose>
          <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]">
            <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'0.00'"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="','"/>
        <xsl:choose>
          <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]">
            <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='BACK']/PRICE/@VALUE[1]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'0.00'"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="','"/>
        <xsl:choose>
          <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]">
            <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[1]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'0.00'"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="','"/>
        <xsl:choose>
          <xsl:when test="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]">
            <xsl:value-of select="./MARKET[@NAME='Match Betting']/SELECTION[2]/OUTCOME/ODDS[@POLARITY='LAY']/PRICE/@VALUE[1]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'0.00'"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="'&#10;'"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Приведенная выше таблица стилей применяется к вашему второму примеру XML (изменен, чтобы быть правильно сформированным и надеясь, что это все еще то, что изначально предполагалось):

<root ID="1">
  <SPORT NAME="Tennis" ID="100005">
    <EVENT NAME="ATP Tour" ID="366156">
      <SUBEVENT NAME="(Sun) R Federer v JW Tsonga (TV)" ID="997624" DATE="2011-11-20 14:00:00">
        <MARKET NAME="Match Betting" ID="2369945" ODDS_SYSTEM="BL">
          <LINK URL="http://www.betdaq.com?mktid=2369945"/>
          <SELECTION NAME="R Federer" ID="12773216">
            <OUTCOME NAME="|BACK| R Federer">
              <ODDS POLARITY="BACK">
                <PRICE VALUE="1.23">
                  <AMOUNT CURRENCY="USD" VALUE="2057"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1303"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1518"/>
                </PRICE>
                <PRICE VALUE="1.18">
                  <AMOUNT CURRENCY="USD" VALUE="2720"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1723"/>
                  <AMOUNT CURRENCY="EUR" VALUE="2008"/>
                </PRICE>
                <PRICE VALUE="1.13">
                  <AMOUNT CURRENCY="USD" VALUE="3128"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1981"/>
                  <AMOUNT CURRENCY="EUR" VALUE="2309"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
            <OUTCOME NAME="|LAY| R Federer">
              <ODDS POLARITY="LAY">
                <PRICE VALUE="1.31">
                  <AMOUNT CURRENCY="USD" VALUE="2"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1"/>
                </PRICE>
                <PRICE VALUE="1.38">
                  <AMOUNT CURRENCY="USD" VALUE="1246"/>
                  <AMOUNT CURRENCY="GBP" VALUE="789"/>
                  <AMOUNT CURRENCY="EUR" VALUE="919"/>
                </PRICE>
                <PRICE VALUE="1.43">
                  <AMOUNT CURRENCY="USD" VALUE="1266"/>
                  <AMOUNT CURRENCY="GBP" VALUE="802"/>
                  <AMOUNT CURRENCY="EUR" VALUE="934"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
          </SELECTION>
          <SELECTION NAME="JW Tsonga" ID="12773217">
            <OUTCOME NAME="|BACK| JW Tsonga">
              <ODDS POLARITY="BACK">
                <PRICE VALUE="3.65">
                  <AMOUNT CURRENCY="USD" VALUE="178"/>
                  <AMOUNT CURRENCY="GBP" VALUE="113"/>
                  <AMOUNT CURRENCY="EUR" VALUE="131"/>
                </PRICE>
                <PRICE VALUE="3.40">
                  <AMOUNT CURRENCY="USD" VALUE="226"/>
                  <AMOUNT CURRENCY="GBP" VALUE="143"/>
                  <AMOUNT CURRENCY="EUR" VALUE="167"/>
                </PRICE>
                <PRICE VALUE="3.15">
                  <AMOUNT CURRENCY="USD" VALUE="291"/>
                  <AMOUNT CURRENCY="GBP" VALUE="184"/>
                  <AMOUNT CURRENCY="EUR" VALUE="214"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
            <OUTCOME NAME="|LAY| JW Tsonga">
              <ODDS POLARITY="LAY">
                <PRICE VALUE="5.10">
                  <AMOUNT CURRENCY="USD" VALUE="115"/>
                  <AMOUNT CURRENCY="GBP" VALUE="73"/>
                  <AMOUNT CURRENCY="EUR" VALUE="85"/>
                </PRICE>
                <PRICE VALUE="5.60">
                  <AMOUNT CURRENCY="USD" VALUE="118"/>
                  <AMOUNT CURRENCY="GBP" VALUE="74"/>
                  <AMOUNT CURRENCY="EUR" VALUE="87"/>
                </PRICE>
                <PRICE VALUE="6.20">
                  <AMOUNT CURRENCY="USD" VALUE="120"/>
                  <AMOUNT CURRENCY="GBP" VALUE="76"/>
                  <AMOUNT CURRENCY="EUR" VALUE="88"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
          </SELECTION>
        </MARKET>
      </SUBEVENT>
      <SUBEVENT1 NAME="(Sun) R Nadal v M Fish (TV)" ID="997625" DATE="2011-11-20 20:00:00">
        <MARKET NAME="Match Betting" ID="2369947" ODDS_SYSTEM="BL">
          <LINK URL="http://www.betdaq.com?mktid=2369947"/>
          <SELECTION NAME="R Nadal" ID="12773222">
            <OUTCOME NAME="|BACK| R Nadal">
              <ODDS POLARITY="BACK">
                <PRICE VALUE="1.20">
                  <AMOUNT CURRENCY="USD" VALUE="13"/>
                  <AMOUNT CURRENCY="GBP" VALUE="8"/>
                  <AMOUNT CURRENCY="EUR" VALUE="9"/>
                </PRICE>
                <PRICE VALUE="1.19">
                  <AMOUNT CURRENCY="USD" VALUE="2367"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1499"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1747"/>
                </PRICE>
                <PRICE VALUE="1.14">
                  <AMOUNT CURRENCY="USD" VALUE="2722"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1724"/>
                  <AMOUNT CURRENCY="EUR" VALUE="2009"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
            <OUTCOME NAME="|LAY| R Nadal">
              <ODDS POLARITY="LAY">
                <PRICE VALUE="1.30">
                  <AMOUNT CURRENCY="USD" VALUE="1578"/>
                  <AMOUNT CURRENCY="GBP" VALUE="999"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1165"/>
                </PRICE>
                <PRICE VALUE="1.35">
                  <AMOUNT CURRENCY="USD" VALUE="1555"/>
                  <AMOUNT CURRENCY="GBP" VALUE="985"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1148"/>
                </PRICE>
                <PRICE VALUE="1.40">
                  <AMOUNT CURRENCY="USD" VALUE="1565"/>
                  <AMOUNT CURRENCY="GBP" VALUE="991"/>
                  <AMOUNT CURRENCY="EUR" VALUE="1155"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
          </SELECTION>
          <SELECTION NAME="M Fish" ID="12773223">
            <OUTCOME NAME="|BACK| M Fish">
              <ODDS POLARITY="BACK">
                <PRICE VALUE="4.40">
                  <AMOUNT CURRENCY="USD" VALUE="139"/>
                  <AMOUNT CURRENCY="GBP" VALUE="88"/>
                  <AMOUNT CURRENCY="EUR" VALUE="102"/>
                </PRICE>
                <PRICE VALUE="3.95">
                  <AMOUNT CURRENCY="USD" VALUE="184"/>
                  <AMOUNT CURRENCY="GBP" VALUE="116"/>
                  <AMOUNT CURRENCY="EUR" VALUE="136"/>
                </PRICE>
                <PRICE VALUE="3.70">
                  <AMOUNT CURRENCY="USD" VALUE="231"/>
                  <AMOUNT CURRENCY="GBP" VALUE="146"/>
                  <AMOUNT CURRENCY="EUR" VALUE="171"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
            <OUTCOME NAME="|LAY| M Fish">
              <ODDS POLARITY="LAY">
                <PRICE VALUE="6.00">
                  <AMOUNT CURRENCY="USD" VALUE="2"/>
                  <AMOUNT CURRENCY="GBP" VALUE="1"/>
                  <AMOUNT CURRENCY="EUR" VALUE="2"/>
                </PRICE>
                <PRICE VALUE="6.20">
                  <AMOUNT CURRENCY="USD" VALUE="91"/>
                  <AMOUNT CURRENCY="GBP" VALUE="57"/>
                  <AMOUNT CURRENCY="EUR" VALUE="67"/>
                </PRICE>
                <PRICE VALUE="7.20">
                  <AMOUNT CURRENCY="USD" VALUE="87"/>
                  <AMOUNT CURRENCY="GBP" VALUE="55"/>
                  <AMOUNT CURRENCY="EUR" VALUE="64"/>
                </PRICE>
              </ODDS>
            </OUTCOME>
          </SELECTION>
          <!--- more <SELECTION>s -->
        </MARKET>
        <!--- more <MARKET>s -->
      </SUBEVENT1>
      <!--- more <SUBEVENT>s -->
    </EVENT>
    <!--- more <EVENT>s -->
  </SPORT>
</root>

Производит этот вывод:

2011-11-20 14:00:00,(Sun) R Federer v JW Tsonga (TV), 1.23,3.65,1.31,5.10
2011-11-20 20:00:00,(Sun) R Nadal v M Fish (TV), 1.20,4.40,1.30,6.00
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...