XSL来自XML输入

时间:2015-03-05 08:00:23

标签: xml xslt

我有XML输入和XML输出。我需要生成XSL。我的XSL文件有问题。你能帮我吗?这是我的XML输入文件:

<?xml version="1.0" encoding="utf-8"?>
<odds>
    <sport name="Soccer">
        <region name="Europe">
            <competition name="UEFA Champions League">
                <event name="Real Madrid - FC Bayern München">
                    <market name="[Full Time] Money Line" suspended="false" id="216649202" expiry="2014-04-23T18:45:00Z" inRunning="false">
                        <outcome name="Real Madrid" id="49234983" price="2.35"/>
                        <outcome name="Draw" id="49234984" price="3.6"/>
                        <outcome name="FC Bayern München" id="49234985" price="2.8"/>
                    </market>
                    <market name="[Next Round] To Qualify" suspended="true" id="21905536" expiry="2014-04-24T19:05:00Z" inRunning="false">
                        <selection name="Benfica" id="49951019" price="2.4"/>
                        <selection name="Juventus" id="49951020" price="1.5"/>
                    </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="market">
<market>
   <xsl:copy-of select="@name" />
    <xsl:copy-of select="@expiry" />
    <xsl:copy-of select="@inRunning" />
    <xsl:copy-of select="@id" />
    <xsl:apply-templates select="node()" />
</market>
 </xsl:template>

<xsl:template match="@inRunning[. = 'false']">
    <xsl:attribute name="inRunning">0</xsl:attribute>
</xsl:template>

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

</xsl:stylesheet>

此代码正在运行我需要在OUTPUT中进行一些更改。这是我的输出

    <?xml version="1.0" encoding="UTF-8"?>
<odds>
    <sport name="Soccer">
        <region name="Europe">
            <competition name="UEFA Champions League">
                <event name="Real Madrid - FC Bayern München">
                    <market name="[Full Time] Money Line" expiry="2014-04-23T18:45:00Z" inRunning="false" id="216649202">
                        <selection price="2.35" id="49234983" name="Real Madrid" handicap=""/>
                        <selection price="3.6" id="49234984" name="Draw" handicap=""/>
                        <selection price="2.8" id="49234985" name="FC Bayern München" handicap=""/>
                    </market>

                </event>
            </competition>
        </region>
    </sport>
</odds>

但是这个OUPUT必须是:

<?xml version='1.0' ?>
<odds>
  <group name="Soccer">
    <group name="Europe">
      <group name="UEFA Champions League">
        <group name="Real Madrid - FC Bayern München">
          <market name="[Full Time] Money Line" expiry="2014-04-23T18:45:00Z" inRunning="0" id="216649202">
            <selection price="2.35" id="49234983" name="Real Madrid" handicap=""/>
            <selection price="3.6" id="49234984" name="Draw" handicap=""/>
            <selection price="2.8" id="49234985" name="FC Bayern München" handicap=""/>
          </market>
          </group>
      </group>
    </group>
  </group>
</odds>

我必须使用什么,改变输出而不是运行=&#34;假&#34; to inRunning =&#34; 0&#34;?

1 个答案:

答案 0 :(得分:2)

如果您只想更改属性以获得其他值,只需添加模板以匹配该属性(使用旧值),然后使用新值创建新属性。

尝试将此模板添加到您的XSLT

<xsl:template match="@inRunning[. = 'false']">
    <xsl:attribute name="inRunning">0</xsl:attribute>
</xsl:template>

请注意,检查区分大小写,因此例如,此特定示例与“FALSE”不匹配。

另请注意,您必须在现有代码中将<xsl:copy-of select="@inRunning" />替换为<xsl:apply-templates select="@inRunning" />才能生效。