我正在尝试访问像this one这样的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="' '"/>
</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以便考虑到这种情况?
答案 0 :(得分:1)
如果没有确切知道你想要实现的目标,并且没有良好的输入,这就是我对你要做的事情的猜测......
以下是样式表的修改版本。其中大部分没有变化;基本上我修改了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="' '"/>
</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
答案 1 :(得分:1)
看起来这会让你走得很远:
<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> </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>
。它的工作方式与for-each相似,但它更灵活。