Xml XSL应用模板
我有XSL代码,它只给我一行输出。如何处理所有行?这是我的意见:Xml XSL应用模板,xml,xslt,Xml,Xslt,我有XSL代码,它只给我一行输出。如何处理所有行?这是我的意见: <?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"> <
<?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>
但正如你们所看到的,在我的输入中,我有两条结果线。我想我需要添加一些地方,但我不知道在哪里。多谢各位
名称模板:
<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>
皇家马德里/平局
皇家马德里/拜仁慕尼黑俱乐部
抽签/拜仁慕尼黑
更换线路
<selection price="{outcome/@price}" id="{outcome/@id}" name="{outcome/@name}" handicap="{$handicap}" />
然后我将结果元素的模板更改为
<xsl:template match="outcome">
<selection handicap="{substring-before(substring-after(@name, ' ('), ')')}">
<xsl:apply-templates select="@*"/>
</xsl:template>
谢谢!!!!!最后一个问题。我有一个模板,如果name=12,名称应该改为Draw。我已经有了这个模板,但是如何将它与我的模板结果连接起来呢?我们确实需要查看您的模板,如果它也是针对结果
元素的,那么请确保在上面的模板之后添加
。为了避免警告,您还可以设置优先级
。我添加了我的姓名模板
<xsl:template match="outcome">
<selection price="{@price}" id="{@id}" name="{@name}" handicap="{substring-before(substring-after(@name, ' ('), ')')}" />
</xsl:template>
<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>
<xsl:template match="outcome">
<selection handicap="{substring-before(substring-after(@name, ' ('), ')')}">
<xsl:apply-templates select="@*"/>
</xsl:template>