Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

xslt - XSL From XML input

I have a XML input and also XML output. I need to generate XSL. I have a problem with my XSL file. Can you help me please? Here is my XML input file:

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

And this is my XSL file:

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

This code is working by I need some changes in my OUTPUT. Here is my 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>

But this OUPUT must be:

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

What i must use, to change in output instead inRunning="false" to inRunning="0"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you just want to change an attribute to have another value, simply add a template to match that attribute (with the old value) and create a new attribute with the new value.

Try adding this template to your XSLT

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

Note that the check is case-sensitive, so this particular example wouldn't match "FALSE" for example.

Also note you have to replace <xsl:copy-of select="@inRunning" /> with <xsl:apply-templates select="@inRunning" /> in your existing code for this to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...