gpt4 book ai didi

javascript - 按特定属性值循环 XML 元素

转载 作者:行者123 更新时间:2023-12-03 10:04:50 25 4
gpt4 key购买 nike

XML 代码:

<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="29.0000" PlanId="1" RoundDown="" TermMax="5" TierId="1" TierMultiplier=""/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="27.0000" PlanId="1" RoundDown="" TermMax="5" TierId="2" TierMultiplier=""/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="1" RoundDown="" TermMax="5" TierId="3" TierMultiplier=""/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.0000" PlanId="1" RoundDown="" TermMax="5" TierId="4" TierMultiplier=""/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="15.0000" PlanId="1" RoundDown="" TermMax="5" TierId="5" TierMultiplier=""/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="2" RoundDown="50" TermMax="5" TierId="6" TierMultiplier="60.61"/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.4000" PlanId="2" RoundDown="100" TermMax="5" TierId="7" TierMultiplier="71.52"/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="16.2000" PlanId="2" RoundDown="100" TermMax="5" TierId="8" TierMultiplier="92.61"/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="13.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="9" TierMultiplier="112.84"/>
<Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="10.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="10" TierMultiplier="143.30"/>
<Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="24.0000" PlanId="3" RoundDown="50" TermMax="6" TierId="11" TierMultiplier="60.61"/>
<Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="3" RoundDown="100" TermMax="7" TierId="12" TierMultiplier="71.52"/>
<Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="3" RoundDown="100" TermMax="9" TierId="13" TierMultiplier="92.61"/>
<Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="3" RoundDown="100" TermMax="11" TierId="14" TierMultiplier="112.84"/>
<Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="3" RoundDown="100" TermMax="14" TierId="15" TierMultiplier="143.30"/>
<Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="19.2000" PlanId="5" RoundDown="50" TermMax="6" TierId="17" TierMultiplier="68.06"/>
<Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="5" RoundDown="100" TermMax="7" TierId="18" TierMultiplier="71.94"/>
<Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="5" RoundDown="100" TermMax="9" TierId="19" TierMultiplier="92.61"/>
<Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="5" RoundDown="100" TermMax="11" TierId="20" TierMultiplier="112.84"/>
<Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="5" RoundDown="100" TermMax="14" TierId="21" TierMultiplier="143.30"/>

在我的 javascript 中,我尝试遍历仅具有 PlanId="5"属性的 XML 元素,然后将每个 Plan 元素的变量分配给其他属性,如下面的 for 循环代码中所示,但是我不能只得到上面的 XML 中的 PlanId="5"元素(最后 5 行)。任何帮助/指导将不胜感激。这是我的循环尝试:

planId = 5;

for (var i = 1; i <= planIdCount; i++) {

// Get Plan attributes for affordability calculations
tierID = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TierId']");
prov.log(0, "tierID: " + tierID);
tierMultiplier = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TierMultiplier']");
prov.log(0, "tierMultiplier: " + tierMultiplier);
config.Global_Variables.CalculatedAffordabilityLines[0].TierMultiplier.setValue(tierMultiplier);
monthlyRate = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='MonthlyRate']");
prov.log(0, "monthlyRate: " + monthlyRate);
config.Global_Variables.CalculatedAffordabilityLines[0].MonthlyRate.setValue(monthlyRate);
roundDown = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='RoundDown']");
config.Global_Variables.CalculatedAffordabilityLines[0].RoundDown.setValue(roundDown);
prov.log(0, "roundDown: " + roundDown);
maxDrawAmount = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='MaxDrawAmount']");
config.Global_Variables.CalculatedAffordabilityLines[0].MaxDrawAmount.setValue(maxDrawAmount);
prov.log(0, "maxDrawAmount: " + maxDrawAmount);
termMax = Provenir.getValue("/Data/Request/Plan[" +i+ "]/PlanId="+planId+"/@*[name()='TermMax']");
config.Global_Variables.CalculatedAffordabilityLines[0].setColumn("gbeb23b1191604f5eb919d11192d5caf6", termMax);
prov.log(0, "termMax: " + termMax);

// Affordability calculations
maxLoan = config.Business_Logic.Scripts.Calculations.CalculateMaxLoan(mdi, termMax, monthlyRate);
// ***** 23012015 - MS - Per Robert, round to 2 decimal places. *****
maxLoan = Number(Math.round(maxLoan + "e+2") + "e-2").toFixed(2);
prov.log(0, "maxLoan: " + maxLoan);
config.Global_Variables.CalculatedAffordabilityLines[0].MaxLoan.setValue(maxLoan);
affordabilityLine = Math.floor(maxLoan/roundDown)*roundDown;
prov.log(0, "affordabilityLine: " + affordabilityLine);
config.Global_Variables.CalculatedAffordabilityLines[0].AffordabilityLine.setValue(affordabilityLine);
}

最佳答案

var plans = '<Plans><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="29.0000" PlanId="1" RoundDown="" TermMax="5" TierId="1" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="27.0000" PlanId="1" RoundDown="" TermMax="5" TierId="2" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="1" RoundDown="" TermMax="5" TierId="3" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.0000" PlanId="1" RoundDown="" TermMax="5" TierId="4" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="15.0000" PlanId="1" RoundDown="" TermMax="5" TierId="5" TierMultiplier=""/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="24.0000" PlanId="2" RoundDown="50" TermMax="5" TierId="6" TierMultiplier="60.61"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="20.4000" PlanId="2" RoundDown="100" TermMax="5" TierId="7" TierMultiplier="71.52"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="16.2000" PlanId="2" RoundDown="100" TermMax="5" TierId="8" TierMultiplier="92.61"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="13.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="9" TierMultiplier="112.84"/><Plan InitialMin="100.00" MaxDrawAmount="1500.00" MonthlyRate="10.5000" PlanId="2" RoundDown="100" TermMax="5" TierId="10" TierMultiplier="143.30"/><Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="24.0000" PlanId="3" RoundDown="50" TermMax="6" TierId="11" TierMultiplier="60.61"/><Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="3" RoundDown="100" TermMax="7" TierId="12" TierMultiplier="71.52"/><Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="3" RoundDown="100" TermMax="9" TierId="13" TierMultiplier="92.61"/><Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="3" RoundDown="100" TermMax="11" TierId="14" TierMultiplier="112.84"/><Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="3" RoundDown="100" TermMax="14" TierId="15" TierMultiplier="143.30"/><Plan InitialMin="100.00" MaxDrawAmount="950.00" MonthlyRate="19.2000" PlanId="5" RoundDown="50" TermMax="6" TierId="17" TierMultiplier="68.06"/><Plan InitialMin="1000.00" MaxDrawAmount="1400.00" MonthlyRate="20.4000" PlanId="5" RoundDown="100" TermMax="7" TierId="18" TierMultiplier="71.94"/><Plan InitialMin="1500.00" MaxDrawAmount="1900.00" MonthlyRate="16.2000" PlanId="5" RoundDown="100" TermMax="9" TierId="19" TierMultiplier="92.61"/><Plan InitialMin="2000.00" MaxDrawAmount="2400.00" MonthlyRate="13.5000" PlanId="5" RoundDown="100" TermMax="11" TierId="20" TierMultiplier="112.84"/><Plan InitialMin="2500.00" MaxDrawAmount="2500.00" MonthlyRate="10.5000" PlanId="5" RoundDown="100" TermMax="14" TierId="21" TierMultiplier="143.30"/></Plans>';

// parse the XML into a DOM
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(plans, "text/xml");

// query for the items we care about.
var items = oDOM.querySelectorAll('[PlanId="5"]');

// Do whatever you need to do with those elements.
// Here (for the sake of example) we're emitting each item's MonthlyRate attribute.
var list = document.getElementById('list');
for(var i = 0; i < items.length; i++){
var el = document.createElement('li');
el.innerHTML = 'Monthly Rate: ' + items[i].getAttribute('MonthlyRate');
list.appendChild(el);
}
<ol id="list"></ol>

关于javascript - 按特定属性值循环 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30404841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com