如何使用XSLT根据条件更改XML的值

时间:2016-08-09 05:48:06

标签: xml xslt

我有一个XML:

<present>true</present>

我想使用XSL执行XML转换:

预期产量: 如果all> <one>Something 1</one> <two>something 2</two> <check> <present>YES</present> </check> <action> <perform>READ</perform> </action> </all>

<present>false</present>

其他如果:<all> <one>Something 1</one> <two>something 2</two> <check> <present>NO</present> </check> <action> <perform>INSERT</perform> </action> </all>

  <xsl:template match="perform">
  <xsl:copy>
    <xsl:choose>
      <xsl:when test="../check/present = 'true'">
        <xsl:text>READ</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:copy>
</xsl:template>

有可能吗? 我不知道XSL中的条件检查 我尝试移动元素但没有工作:

public class PlanetFragment extends ListFragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";
    ListView listView;
    ArrayList<Actors> actorList;
    ActorsAdapter adapter;
    private View view;
    public PlanetFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	if (view == null) {
    		view = inflater.inflate(R.layout.fragment_planet, container, false);
         }else {
        	 ViewGroup parent = (ViewGroup) view.getParent();
             parent.removeView(view);
		}
    	
        listView = (ListView) view.findViewById(android.R.id.list);
 
        actorList = new ArrayList<Actors>();
        adapter = new ActorsAdapter(getActivity(),R.layout.row, actorList);
        listView.setAdapter(adapter);
        new JSONAsyncTask().execute("/UpcomingEvents/GetEvents/1");
        return view;
    }
    public class JSONAsyncTask extends AsyncTask<String, Void, Boolean>{

		@Override
		protected Boolean doInBackground(String... params) {
			HttpGet httpGs[0]);
			HttpClient httpClient = new DefaultHttpClient();
			try {
				HttpResponse response = httpClient.execute(httpGet);
				int status = response.getStatusLine().getStatusCode();
				if(status == 200){
					HttpEntity entity = response.getEntity();
					String data = EntityUtils.toString(entity);
//					JSONObject jsonObject = new JSONObject(data);
					JSONArray jsonArray = new JSONArray(data);
					for(int i=0;i<jsonArray.length();i++){
						JSONObject object = jsonArray.getJSONObject(i);
						Actors actor = new Actors();
						actor.setName(object.getString("eventTitle"));
					
						actorList.add(actor);
						
					}
					return true;
				}
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return false;
		}
    	
    }
}

2 个答案:

答案 0 :(得分:1)

为什么不完全按照你的要求去做:

<xsl:template match="perform">
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="../../check/present='true'">READ</xsl:when>
            <xsl:when test="../../check/present='false'">INSERT</xsl:when>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

答案 1 :(得分:0)

试试这个:

<xsl:stylesheet version= "1.0"
xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "xml" version= "1.0" encoding= "UTF-8" indent= "yes" />
<xsl:strip-space elements= "*" />

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

<xsl:template match="action"/>

<xsl:template match="check/present">
     <xsl:choose>
          <xsl:when test=".='true'">
               <xsl:copy><xsl:text>YES</xsl:text></xsl:copy>
               <action>
                    <perform><xsl:text>READ</xsl:text></perform>
              </action>
          </xsl:when>
          <xsl:otherwise>
               <xsl:copy><xsl:text>NO</xsl:text></xsl:copy>
               <action>
                    <perform><xsl:text>INSERT</xsl:text></perform>
              </action>
          </xsl:otherwise>
     </xsl:choose>
</xsl:template>
</xsl:stylesheet>