我正在尝试对包含数千个节点的大型XML文件进行排序,并且作为我想要排序的所有节点的共同基础,有一个名为“ID”的属性。
此属性前面的元素类型有所不同:它可以有多个名称,例如“Apt”,“CenterDrill”,“Reamer”,“Cylinder”,“Block”,“Model”,“Sweep”, “Cone”,“Probe”以及其他一些人。这使得我弄清楚一个算法可以进行正确的排序,无论属性前面的名称如何。
我不想弄乱外部元素的顺序,也就是说,我想要元素“Parametric Mills”,“Parametric Drills”,< strong>“工具架”,“探针触控笔”,“抛光机”保持相同的顺序。我想对内部元素进行排序,如下图所示。
原始XML内容:简短版本(287行)
<?xml version="1.0"?>
<Tools>
<Tool ID="Parametric Mills" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>Milling</Type>
<Cutter>
<Apt ID="10000" ParentID="" Type="FLAT END" >
<D>12.7</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>63.5</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
<Apt ID="2" ParentID="" Type="BALL END" >
<D>12.7</D>
<R>6.35</R>
<E>0</E>
<F>6.35</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
<Apt ID="33" ParentID="" Type="TAPERED BULL NOSE" >
<D>12.7</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>5</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<ShankDiameter>12.7</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="3000" ParentID="" Type="BULL NOSE" >
<D>12.7</D>
<R>2</R>
<E>4.35</E>
<F>2</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-114.3</Z>
</DrivenPoint>
</Tool>
<Tool ID="Parametric Drills" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>HoleMaking</Type>
<Cutter>
<Apt ID="5000" ParentID="" Type="DRILL" >
<D>9.525</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>31</A>
<B>0</B>
<H>101.6</H>
<StickoutLength>76.2</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>63.5</FluteLength>
<Alternate>off</Alternate>
</Apt>
<Reamer ID="111" ParentID="" DRILLMILL="FALSE" Type="Reamer" >
<ChamferLength>0.5</ChamferLength>
<D>12</D>
<H>100</H>
<ShankDiameter>12</ShankDiameter>
<StickoutLength>0</StickoutLength>
<SpindleDirection>CW</SpindleDirection>
<MAXIMUMMATERIALREMOVAL>0</MAXIMUMMATERIALREMOVAL>
<FluteLength>63.5</FluteLength>
<Alternate>off</Alternate>
</Reamer>
<CenterDrill ID="3" ParentID="" DRILLMILL="FALSE" Type="CenterDrill" >
<ChamferAngle>60</ChamferAngle>
<H>50</H>
<PilotAngle>31</PilotAngle>
<D>3</D>
<PilotLength>3</PilotLength>
<ShankDiameter>12</ShankDiameter>
<StickoutLength>0</StickoutLength>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>11.69552</FluteLength>
<Alternate>off</Alternate>
</CenterDrill>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-127</Z>
</DrivenPoint>
</Tool>
<Tool ID="Tool Holders" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>Turning</Type>
<Holder>
<Block ID="455" ParentID="">
<Width>25.4</Width>
<Length>25.4</Length>
<Height>101.6</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Block>
<Cylinder ID="1" ParentID="">
<Radius>50</Radius>
<Height>10</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Cylinder>
<Cone ID="50000" ParentID="">
<ButtomRadius>30</ButtomRadius>
<TopRadius>10</TopRadius>
<Height>50</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Cone>
</Holder>
</Tool>
<Tool ID="Probe Stylus" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Type>Probing</Type>
<Cutter>
<Probe ID="11500" ParentID="" Type="Standard">
<SphereDiameter>5.00126</SphereDiameter>
<Height>101.6</Height>
<StemDiameter>2.99974</StemDiameter>
<Length>101.6</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>76.2</StickoutLength>
<Alternate>off</Alternate>
</Probe>
<Probe ID="10455" ParentID="" Type="Cylinder">
<SphereDiameter>10</SphereDiameter>
<Height>10</Height>
<StemDiameter>3</StemDiameter>
<Length>50</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>0</StickoutLength>
<Alternate>off</Alternate>
</Probe>
<Probe ID="11111" ParentID="" Type="Disk">
<SphereDiameter>20</SphereDiameter>
<Height>3</Height>
<Height2>5</Height2>
<Diameter2>3</Diameter2>
<PinOffset>8</PinOffset>
<StemDiameter>6</StemDiameter>
<Length>50</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>0</StickoutLength>
<Alternate>off</Alternate>
</Probe>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-127</Z>
</DrivenPoint>
</Tool>
<Tool ID="Polishers" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Type>Polisher</Type>
<Cutter>
<Apt ID="50" ParentID="" Type="FLAT END" PolishType="Polisher">
<D>38.1</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>101.6</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="65" ParentID="" Type="BALL END" PolishType="Polisher">
<D>20</D>
<R>10</R>
<E>0</E>
<F>10</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="42" ParentID="" Type="BULL NOSE" PolishType="Polisher">
<D>10</D>
<R>2</R>
<E>3</E>
<F>2</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-114.3</Z>
</DrivenPoint>
</Tool>
</Tools>
这是排序后的预期XML内容:
<?xml version="1.0"?>
<Tools>
<Tool ID="Parametric Mills" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>Milling</Type>
<Cutter>
<Apt ID="2" ParentID="" Type="BALL END" >
<D>12.7</D>
<R>6.35</R>
<E>0</E>
<F>6.35</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
<Apt ID="33" ParentID="" Type="TAPERED BULL NOSE" >
<D>12.7</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>5</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<ShankDiameter>12.7</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="3000" ParentID="" Type="BULL NOSE" >
<D>12.7</D>
<R>2</R>
<E>4.35</E>
<F>2</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
<Apt ID="10000" ParentID="" Type="FLAT END" >
<D>12.7</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>0</B>
<H>88.9</H>
<StickoutLength>63.5</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>50.8</FluteLength>
<Alternate>off</Alternate>
</Apt>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-114.3</Z>
</DrivenPoint>
</Tool>
<Tool ID="Parametric Drills" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>HoleMaking</Type>
<Cutter>
<CenterDrill ID="3" ParentID="" DRILLMILL="FALSE" Type="CenterDrill" >
<ChamferAngle>60</ChamferAngle>
<H>50</H>
<PilotAngle>31</PilotAngle>
<D>3</D>
<PilotLength>3</PilotLength>
<ShankDiameter>12</ShankDiameter>
<StickoutLength>0</StickoutLength>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>11.69552</FluteLength>
<Alternate>off</Alternate>
</CenterDrill>
<Reamer ID="111" ParentID="" DRILLMILL="FALSE" Type="Reamer" >
<ChamferLength>0.5</ChamferLength>
<D>12</D>
<H>100</H>
<ShankDiameter>12</ShankDiameter>
<StickoutLength>0</StickoutLength>
<SpindleDirection>CW</SpindleDirection>
<MAXIMUMMATERIALREMOVAL>0</MAXIMUMMATERIALREMOVAL>
<FluteLength>63.5</FluteLength>
<Alternate>off</Alternate>
</Reamer>
<Apt ID="5000" ParentID="" Type="DRILL" >
<D>9.525</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>31</A>
<B>0</B>
<H>101.6</H>
<StickoutLength>76.2</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>63.5</FluteLength>
<Alternate>off</Alternate>
</Apt>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-127</Z>
</DrivenPoint>
</Tool>
<Tool ID="Tool Holders" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Teeth>2</Teeth>
<Type>Turning</Type>
<Holder>
<Cylinder ID="1" ParentID="">
<Radius>50</Radius>
<Height>10</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Cylinder>
<Block ID="455" ParentID="">
<Width>25.4</Width>
<Length>25.4</Length>
<Height>101.6</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Block>
<Cone ID="50000" ParentID="">
<ButtomRadius>30</ButtomRadius>
<TopRadius>10</TopRadius>
<Height>50</Height>
<NoSpin>0</NoSpin>
<Alternate>off</Alternate>
</Cone>
</Holder>
</Tool>
<Tool ID="Probe Stylus" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Type>Probing</Type>
<Cutter>
<Probe ID="10455" ParentID="" Type="Cylinder">
<SphereDiameter>10</SphereDiameter>
<Height>10</Height>
<StemDiameter>3</StemDiameter>
<Length>50</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>0</StickoutLength>
<Alternate>off</Alternate>
</Probe>
<Probe ID="11111" ParentID="" Type="Disk">
<SphereDiameter>20</SphereDiameter>
<Height>3</Height>
<Height2>5</Height2>
<Diameter2>3</Diameter2>
<PinOffset>8</PinOffset>
<StemDiameter>6</StemDiameter>
<Length>50</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>0</StickoutLength>
<Alternate>off</Alternate>
</Probe>
<Probe ID="11500" ParentID="" Type="Standard">
<SphereDiameter>5.00126</SphereDiameter>
<Height>101.6</Height>
<StemDiameter>2.99974</StemDiameter>
<Length>101.6</Length>
<MaxRPM>0</MaxRPM>
<StemRGB>-4144960</StemRGB>
<StickoutLength>76.2</StickoutLength>
<Alternate>off</Alternate>
</Probe>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-127</Z>
</DrivenPoint>
</Tool>
<Tool ID="Polishers" Units="Millimeter" Stack="FALSE">
<Force_data Input_lead="0" lead_dis="0.000000" helix_angle="0.000000" radial_rake="0.000000" rake_ball="0.000000" force_method="0" material="0" edge_type="0"
feed_limit="0.000000" chip_thickness="0.000000" force_limit="0.000000" power_limit="0.000000" plunge_feed="0" start_angle="0.000000" feed_drop="0.000000" cutoutput="0" force_resolution="0" force_resolution_distance="0.000000"
force_unit="1" force_cutchannel="0" force_ccstartangle="0.000000" force_ccfeedrop="0.000000" force_minfeedChange="0.000000" force_minfeedChange_def="1" force_mincutfeedrate="0.000000"
force_mincutfeedrate_def="1" force_aircutfeedrate="0.000000" force_aircutfeedrate_def="1" force_toolType="" force_edgeMaterial=""/>
<Type>Polisher</Type>
<Cutter>
<Apt ID="42" ParentID="" Type="BULL NOSE" PolishType="Polisher">
<D>10</D>
<R>2</R>
<E>3</E>
<F>2</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="50" ParentID="" Type="FLAT END" PolishType="Polisher">
<D>38.1</D>
<R>0</R>
<E>0</E>
<F>0</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>101.6</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
<Apt ID="65" ParentID="" Type="BALL END" PolishType="Polisher">
<D>20</D>
<R>10</R>
<E>0</E>
<F>10</F>
<A>0</A>
<B>0</B>
<H>127</H>
<StickoutLength>0</StickoutLength>
<R2>0</R2>
<E2>0</E2>
<F2>0</F2>
<SpindleDirection>CW</SpindleDirection>
<FluteLength>76.2</FluteLength>
<ShankDiameter>19.05</ShankDiameter>
<Alternate>off</Alternate>
</Apt>
</Cutter>
<DrivenPoint ID="1">
<Type>-1</Type>
<Radius>0</Radius>
<X>0</X>
<Y>0</Y>
<Z>-114.3</Z>
</DrivenPoint>
</Tool>
</Tools>
这是我正在使用的代码,但是输出文件是作为单行 XML文件返回的......我对如何对包含该节点的节点进行排序感到很遗憾标签“Apt”,“CenterDrill”,“Reamer”,“Cylinder”,“Block”,“Model”,“Sweep”,“Cone”,“Probe”之后的ID属性等等其他没有弄乱包含ID属性的节点,前面是“工具”或“DrivenPoint”标记/节点。
我已经研究并搜索了很多解决方案,但它们似乎都基于更简单的XML结构...我不介意为每个节点类型编写代码,在我的代码下面我尝试使用“探测”节点执行某些操作,但它也不起作用...我得到一个1kb输出文件,其中包含一行...
有什么建议吗?
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq
Public Class Form1
Private Function ReorderByMaterialThickness(ByRef inFile As String, ByRef outFile As String)
Try
Dim document As XElement = XElement.Load(inFile)
Dim sortedTables As XElement() = document.Elements("Probe").OrderBy(Function(t) CInt(t.Element("ID"))).ToArray()
document.ReplaceAll(sortedTables)
document.Save(outFile)
Catch ex As Exception
MsgBox("Something went wrong!")
End Try
Return Nothing
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ReorderByMaterialThickness("C:\Testes\Original.xml", "C:\Testes\Sorted.xml")
End Sub
End Class
答案 0 :(得分:2)
嗯,评论中有很多内容,但我没有注意到,直到我回来,但我相信这会做你想要的。它只对具有2个或更多祖先的元素进行排序,因此我认为它仅针对&#34;内部&#34;具有ID的元素并对它们进行排序。当我用原始XML尝试它时,我得到了你想要的输出。
XDocument doc = XDocument.Load("Original.xml");
foreach (XElement el in doc.Root.Descendants().Where(x => x.Elements().Any(y => y.Attribute("ID") != null) && x.Ancestors().Count() >= 2))
{
el.ReplaceNodes(el.Elements().OrderBy(x => int.Parse(x.Attribute("ID").Value)));
}
doc.Save("newFile.xml");
我对你的帖子感到有点困惑,但我认为这就是你要求的。
如果您不想使用XDocument:
XElement doc = XElement.Load("Original.xml");
foreach (XElement el in doc.Descendants().Where(x => x.Elements().Any(y => y.Attribute("ID") != null) && x.Ancestors().Count() >= 2))
{
el.ReplaceNodes(el.Elements().OrderBy(x => int.Parse(x.Attribute("ID").Value)));
}
doc.Save("newFile.xml");
答案 1 :(得分:2)
AFAICT,将以下XSL转换应用于XML文件将产生所需的结果:
XSLT 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="Cutter | Holder">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="@ID" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>