xml转换未获得所需的输出

时间:2017-03-08 11:00:09

标签: xml xslt xml-parsing xslt-2.0

我喜欢XML

     <ChildAges>
         <Age>4</Age>
        <Age>1</Age>
        <Age>5</Age>
        <Age>2</Age>
       <Age>0</Age>
   </ChildAges>

我想将年龄小于2的年龄替换为年龄2.Output应该像

   <ChildAges>
     <Age>4</Age>
    <Age>2</Age>
    <Age>5</Age>
   <Age>2</Age>
   <Age>2</Age>
 </ChildAges>

请建议xsl.I尝试如下。但是没有工作。

                      <xsl:element name="ChildAges">
                                <xsl:for-each select="*:Age">
                                    <xsl:choose>
                                        <xsl:when test="Age[node() &lt; 2]">
                                               <Age>2</Age>
                                         </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:copy-of select="*:Age"/>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </xsl:element>

3 个答案:

答案 0 :(得分:1)

请了解the identity transformation作为起点,然后您只需添加一个模板进行更改:

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

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

    <xsl:template match="Age[. &lt; 2]">
        <Age>2</Age>
    </xsl:template>

</xsl:transform>

http://xsltransform.net/pPJ8LWf

答案 1 :(得分:1)

public class des extends AppCompatActivity {

    ImageButton imageView15;
    InterstitialAd mInterstitialAd;
    private InterstitialAd interstitial;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_des);

        AdView mAdView = (AdView) findViewById(R.id.AdView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

                   // Prepare the Interstitial Ad
        interstitial = new InterstitialAd(des.this);
// Insert the Ad Unit ID
        interstitial.setAdUnitId(getString(R.string.intertitial_id));

        interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // Call displayInterstitial() function
                displayInterstitial();
            }
        });


        imageView15 = (ImageButton) findViewById(R.id.imageView15);
        imageView15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(des.this,vid.class);
                startActivity(intentLoadNewActivity);
            }
        });


    }

    public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }


}

答案 2 :(得分:0)

                            <xsl:element name="ChildAges">
                                <xsl:for-each select="*:ChildAges/*:Age">
                                    <xsl:choose>
                                        <xsl:when test="node() &lt; 2">
                                               <Age>2</Age>
                                         </xsl:when>
                                        <xsl:otherwise>
                                            <Age>
                                                <xsl:value-of  select="node()"/>
                                            </Age>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:for-each>
                            </xsl:element>
相关问题