XSL来自XML。 XSLT

时间:2015-03-05 11:11:08

标签: xml xslt

我需要从结果中得到一个数字 - "(0.5)"并将此号码设置为差点,并在市场ID的末尾添加此号码。像这样:219055430.5。你能给我一些建议吗? 这是XML输入:

<?xml version="1.0" encoding="utf-8"?>
<odds xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <sport name="Soccer">
  <region name="Europe">
   <competition name="UEFA Champions League">
    <event name="Real Madrid - FC Bayern München">
     <market name="[Full Time] Over/Under" suspended="false" id="21905543" expiry="2014-04-23T18:45:00Z" inRunning="false">
      <outcome name="Over (0.5)" id="49954102" price="1" handicap=""/>
     </market>
    </event>
   </competition>
  </region>
 </sport>
</odds> 

我的XSL:

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

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

<xsl:template match="@suspended"/>

<xsl:template match="market[@suspended='true']"/>

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

<xsl:template match="@name[. = '1X']">
<xsl:attribute name="name">Real Madrid/Draw</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = '12']">
<xsl:attribute name="name">Real Madrid/FC Bayern München</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'X2']">
<xsl:attribute name="name">Draw/FC Bayern München</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'HWin-HWin']">
<xsl:attribute name="name">Real Madrid/Real Madrid</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'HWin-Draw']">
<xsl:attribute name="name">Real Madrid/Draw</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'Draw-HWin']">
<xsl:attribute name="name">Draw/Real Madrid</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'AWin-AWin']">
<xsl:attribute name="name">FC Bayern München/FC Bayern München</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'AWin-Draw']">
<xsl:attribute name="name">FC Bayern München/Draw</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'Draw-AWin']">
<xsl:attribute name="name">Draw/FC Bayern München</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'AWin-HWin']">
<xsl:attribute name="name">FC Bayern München/Real Madrid</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'HWin-AWin']">
<xsl:attribute name="name">Real Madrid/FC Bayern München</xsl:attribute>
</xsl:template>

<xsl:template match="@name[. = 'Draw-Draw']">
<xsl:attribute name="name">Draw/Draw</xsl:attribute>
</xsl:template>


<xsl:template match="market">
<xsl:choose>
<xsl:when test="inRunning=false">
<market>
<xsl:copy-of select="@name" />
<xsl:copy-of select="@expiry" />
<xsl:attribute name="inRunning">1</xsl:attribute>
<xsl:copy-of select="@id" />
<xsl:apply-templates select="node()" />
</market>
</xsl:when>
<xsl:otherwise>
<market>
<xsl:copy-of select="@name" />
<xsl:copy-of select="@expiry" />
<xsl:attribute name="inRunning">0</xsl:attribute>
<xsl:copy-of select="@id" />
<xsl:apply-templates select="node()" />
</market>
</xsl:otherwise>
</xsl:choose>
</xsl:template>



<xsl:template match="outcome">
<selection >
<xsl:copy-of select="@price" />
<xsl:copy-of select="@id" />
<xsl:apply-templates select="@name" />
<xsl:attribute name="handicap"></xsl:attribute>
<xsl:apply-templates select="node()" />
</selection>
</xsl:template>

</xsl:stylesheet>

必须输出:

        <market name="[Full Time] Over/Under" expiry="2014-04-23T18:45:00Z" inRunning="0" id="219055430.5"> 
         <selection price="1" id="49954102" name="Over" handicap="0.5"/> 
        </market>

1 个答案:

答案 0 :(得分:1)

我不确定为什么你需要所有的XSLT代码。我甚至不太清楚为什么我们需要看到所有代码才能得到这个问题。如果我没有弄错,以下模板将自行提供所需的输出:

<xsl:template match="market">
    <xsl:variable name="handicap" select="substring-before(substring-after(outcome/@name, ' ('), ')')" />
    <market name="{@name}" expiry="{@expiry}" inRunning="{number(@inRunning='true')}" id="{concat(@id, $handicap)}"> 
         <selection price="{outcome/@price}" id="{outcome/@id}" name="{substring-before(outcome/@name, ' (')}" handicap="{$handicap}"/> 
    </market>
</xsl:template>

应用于示例输入

<?xml version="1.0" encoding="utf-8"?>
<odds xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <sport name="Soccer">
  <region name="Europe">
   <competition name="UEFA Champions League">
    <event name="Real Madrid - FC Bayern München">
     <market name="[Full Time] Over/Under" suspended="false" id="21905543" expiry="2014-04-23T18:45:00Z" inRunning="false">
      <outcome name="Over (0.5)" id="49954102" price="1" handicap=""/>
     </market>
    </event>
   </competition>
  </region>
 </sport>
</odds> 

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<market name="[Full Time] Over/Under" expiry="2014-04-23T18:45:00Z" inRunning="0" id="219055430.5">
   <selection price="1" id="49954102" name="Over" handicap="0.5"/>
</market>

如果我 错误,您需要编辑问题并为我们拼出规则。