I'm really new at coding, sorry if any of this sounds silly or stupid. We have a new project to come up with a new webpage. I have a multiple condition if statement and would like to call a xsl template if condition is met. Here's how I have it now and it doesn't work at all.
<script>
function getSelectedValue()
{
if("document.getElementById('type').value==1 and document.getElementById('cablegroup5').value==9"+
"document.getElementById('c开发者_运维百科ablegroup3').value==22 and document.getElementById('cablelength').value==11")
{
<xsl:call-template name="PN">
<xsl:with-param name="Cable">ABC111-06</xsl:with-param>
</xsl:call-template>
}
}
</script>
I know the first part works, I've tested it with an alert message and that works just fine. These are all activated by a button(onclick) next to multiple drop down menus. Is there a way to get this to work? Any help would be really appreciated. Thanks.
You're confused about the processing model. Is the script element generated by XSLT? If so, the call-template will probably be called at the time the script is generated. It won't be called at the time the script is executed. Javascript code isn't going to magically execute XSLT instructions.
There are a couple of issue within the script that would prevent the if
statement executing correctly.
- The boolean and operator in JavaScript is
&&
notand
. Note the if you use&
this would be a bitwise and. The tests should not be a string. Due to JavaScript type coersion it a string will be converted to a boolean. A
null
or empty string''
will evaluatefalse
, all other strings will evaluatetrue
. Currently you haveif("test1 and test2")
This should be
if(test1 && test2)
So far your updated script would be
<script>
function getSelectedValue()
{
if(document.getElementById('type').value==1 &&
document.getElementById('cablegroup5').value==9 &&
document.getElementById('cablegroup3').value==22 &&
document.getElementById('cablelength').value == 11)
{
// Process Xml
}
}
</script>
You need to use the browser xml parser to handle your xml. I will assume that you have an xml string, if you have a document object then you will have to change the following slightly, this is from w3schools.
var xmlString = "<Products>" +
"<Product partnumber='foo'>This is product 1</Product>" +
"<Product partnumber='bar'>This is product 2</Product>" +
"</Products>";
// Load into an XML document
var myDoc;
if (window.DOMParser)
{
var parser=new DOMParser();
myDoc=parser.parseFromString(xmlString,"text/xml");
}
else // Internet Explorer
{
myDoc=new ActiveXObject("Microsoft.XMLDOM");
myDoc.async="false";
myDoc.loadXML(xmlString);
}
// Get all product nodes
var products = myDoc.getElementsByTagName('Product');
var i, targetProduct, partNumber;
for(i = 0; i < products.length; i += 1){
// Get the partnumber attribute
partnumber = products[i].attributes.getNamedItem('partnumber');
// Ensure that the partnumber exists and its value is what is wanted
if(partnumber && partnumber.value == 'foo'){
targetProduct = products[i];
// Exit for
break;
}
}
// If the product has been found alert its value.
if(targetProduct != null){
alert(targetProduct.textContent || targetProduct.text);
}
If you were selected a node by id then you could use xmlDoc.getElementById
instead of iterating through all nodes of a type and checking attributes.
To select the text value of an xml node most browsers use the property textContent
although Internet Explorer uses text
. The line
targetProduct.textContent || targetProduct.text
returns textContent
if it is present and not null, or the value of text
.
精彩评论