根据XSD中的属性值创建所需的XML元素

时间:2015-06-02 20:49:41

标签: xml xsd msxml

我的要求是有一个XSD文件,它根据属性值检查元素。我能够将XSD编写到可以限制'的属性值的位置。任何人都可以帮我完成XSD文件,我可以根据Application/@Type属性制作一些所需的元素吗?

我想制作

  • Application/@Type仅在PackageArg为“批量”
  • 时才需要
  • Application/@Type仅在Version为“服务”
  • 时才需要 仅当Application/@Type为“网络”或“服务”时才需要
  • Project

XML文件

Application/@Type

XSD档案

<Applications>        
    <Application Name="ConfigManagement" Type="Web">            
        <ProjectDirName>ConfigManagement</ProjectDirName>
        <Project>Web.ConfigManagement.csproj</Project>
        <OutputDirName>ConfigManagement</OutputDirName>            
    </Application>
    <Application Name="Util" Type="Web">            
        <ProjectDirName>Web</ProjectDirName>
        <Project>Web.csproj</Project>        
        <OutputDirName>Util</OutputDirName>    
    </Application>
    <Application Name="ConfigService" Type="Service">
        <ProjectDirName>WebServices\ConfigService</ProjectDirName>
        <Project>ConfigService.csproj</Project>    
        <Version>2015\04</Version>
        <OutputDirName>ConfigService</OutputDirName>
    </Application>
    <Application Name="DeliverEmail" Type="Batch">        
        <ProjectDirName>\Batch\DeliverEmail</ProjectDirName>
        <PackageArg>Release</PackageArg>        
        <OutputDirName>Tidal\DeliverEmail</OutputDirName>            
    </Application>
</Applications>

1 个答案:

答案 0 :(得分:2)

更新:OP已编辑问题以删除xs:assert的使用,并在评论中声明必须在C#中进行验证。 OP问题的答案现在变为:

您不能使用XSD 1.0强制执行基于属性值改变元素要求的约束,并且Microsoft不支持XSD 1.1,因此您必须放宽约束或在你的XSD。

原始XSD 1.1回答

(为未来读者的利益而保留)

你很亲密,但你的断言,

      <xs:assert test="count(./PackageArg[@type eq 'Batch']) eq 1"/>

@type PackageArg@Type上测试Application时的<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"> <xs:element name="Applications"> <xs:complexType> <xs:sequence> <xs:element name="Application" maxOccurs="unbounded" minOccurs="1"> <xs:complexType> <xs:all> <xs:element type="xs:string" name="ProjectDirName"/> <xs:element type="xs:string" name="Project" minOccurs="0"/> <xs:element type="xs:string" name="Version" minOccurs="0"/> <xs:element type="xs:string" name="PackageArg" minOccurs="0"/> <xs:element type="xs:string" name="OutputDirName"/> </xs:all> <xs:attribute type="xs:string" name="Name" use="optional"/> <xs:attribute name="Type" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Web"/> <xs:enumeration value="Batch"/> <xs:enumeration value="Service"/> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:assert test="PackageArg or @Type != 'Batch'"/> <xs:assert test="Version or @Type != 'Service'"/> <xs:assert test="Project or not(@Type = 'Web' or @Type = 'Service')"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

以下XSD将验证您的XML并强制执行与属性相关的要求:

XSD 1.1

package gaga.sharedpreferences;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    public final class setup extends MainActivity {
        public void setup () {
            //Nothing to see here!
        }

        // Define the File of Prefs; created if nonexistent
        public static final String PREFS_NAME = "MyPrefsFile";

        // Start up
        @Override
        public void onCreate(Bundle state) {
            super.onCreate(state);

            // Restore preferences on Startup
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            boolean Chocolate = settings.getBoolean("checkChocolate", false);
            boolean Luigi = settings.getBoolean("checkLuigi", false);
            // Function set to Whatever
            // setSilent(silent);
            /* Note:
            * CheckedTextView and CheckBox::isChecked()
            * CheckBox::setChecked()
            * */
            CheckBox checkHandleChocolate = (CheckBox) findViewById(R.id.checkChocolate);
            CheckBox checkHandleLuigi = (CheckBox) findViewById(R.id.checkLuigi);

            // What was the preference? On Start set it to the bool it left off in
            checkHandleChocolate.setChecked(Chocolate);
            checkHandleLuigi.setChecked(Luigi);

            // Change report text on Start
            TextView buttonHandleChocolate = (TextView) findViewById(R.id.chocolate);
            TextView buttonHandleLuigi = (TextView) findViewById(R.id.luigi);

            if(Chocolate)
                buttonHandleChocolate.setText("I do prefer Chocolate");
            else
                buttonHandleChocolate.setText("I do not prefer Chocolate");
            if(Luigi)
                buttonHandleLuigi.setText("I do prefer Luigi");
            else
                buttonHandleLuigi.setText("I do not prefer Luigi");

        }

        public void saveChocolate(Boolean c) {
            // All objects from android.context.Context
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("Chocolate", c);
            // Commit the edits
            editor.commit();
        }
        public void saveLuigi(Boolean l) {
            // All objects from android.context.Context
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("Chocolate", l);
            // Commit the edits
            editor.commit();
        }
    }


    @Override
    protected void onStop(){
        super.onStop();
        // Objects are from android.context.Context
        //Normally I'd put the edit commits here, but that's not true
    }

    // Clicks on Done
    public void userDone (View view) {
        // View is which widget
        boolean checked = ((CheckBox) view).isChecked();

        // Which checkbox was clicked
        switch(view.getId()) {
            case R.id.checkChocolate:
                setup instance1 = new setup();
                instance1.saveChocolate(checked);
                // No break; continue along
            case R.id.checkLuigi:
                setup instance2 = new setup();
                instance2.saveLuigi(checked);
                break;
        }
    }


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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

请注意,Microsoft不支持XSD 1.1。 (您已使用'msxml'标记了您的问题。)