XSL apply-templates

时间:2015-03-05 14:40:39

标签: xml xslt

我有XSL代码,只能输出1行。我怎么能用所有行做到这一点?这是我的INPUT:

<?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] Handicap" suspended="false" id="21905549" expiry="2014-04-23T18:45:00Z" inRunning="false">
<outcome name="Real Madrid (-3.5)" id="49954038" price="16"/>
<outcome name="FC Bayern München (+3.5)" id="49954039" price="0.98"/>
</market>
</event>
</competition>
</region>
</sport>
</odds>

这是我的XSLT:

<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="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="{outcome/@name}" handicap="{$handicap}" />
</market>
</xsl:template>

</xsl:stylesheet>

这是我的输出:

<?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 id="21905549-3.5" inRunning="0" expiry="2014-04-23T18:45:00Z" name="[Full Time] Handicap">
<selection handicap="-3.5" name="Real Madrid (-3.5)" id="49954038" price="16"/>
</market>
</event>
</competition>
</region>
</sport>
</odds>

但正如你所看到的,在我的输入中我有2条结果线。我想我需要添加一些地方,但我不知道在哪里。谢谢

名称模板:

<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>

1 个答案:

答案 0 :(得分:0)

替换

<selection price="{outcome/@price}" id="{outcome/@id}" name="{outcome/@name}" handicap="{$handicap}" />

<xsl:apply-templates select="outcome"/>

然后添加模板

<xsl:template match="outcome">
  <selection price="{@price}" id="{@id}" name="{@name}" handicap="{substring-before(substring-after(@name, ' ('), ')')}" />
</xsl:template>

假设样式表中还有name属性的三个模板,我会编辑它们并添加要转换为匹配模式的元素的名称,例如

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

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

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

然后我会将outcome元素的模板更改为

<xsl:template match="outcome">
  <selection handicap="{substring-before(substring-after(@name, ' ('), ')')}">
   <xsl:apply-templates select="@*"/>
</xsl:template>
相关问题