Difference between 'where' and 'if' in an XSLT script
I am new to XML/XSLT, and I'm a bit confused about the difference between the <xsl:if test="x">
and adding a where="x"
at the end of a statement.
Below is some example data and two XSLT versions of code. I tried running it both ways here: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 but nothing appears, so I may be doing something wrong. Is anyone able to clarify this for me?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
Using where
my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
Code using the if
statemtent
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
if-statement xslt where
add a comment |
I am new to XML/XSLT, and I'm a bit confused about the difference between the <xsl:if test="x">
and adding a where="x"
at the end of a statement.
Below is some example data and two XSLT versions of code. I tried running it both ways here: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 but nothing appears, so I may be doing something wrong. Is anyone able to clarify this for me?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
Using where
my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
Code using the if
statemtent
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
if-statement xslt where
add a comment |
I am new to XML/XSLT, and I'm a bit confused about the difference between the <xsl:if test="x">
and adding a where="x"
at the end of a statement.
Below is some example data and two XSLT versions of code. I tried running it both ways here: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 but nothing appears, so I may be doing something wrong. Is anyone able to clarify this for me?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
Using where
my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
Code using the if
statemtent
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
if-statement xslt where
I am new to XML/XSLT, and I'm a bit confused about the difference between the <xsl:if test="x">
and adding a where="x"
at the end of a statement.
Below is some example data and two XSLT versions of code. I tried running it both ways here: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 but nothing appears, so I may be doing something wrong. Is anyone able to clarify this for me?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
Using where
my XSLT is:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
Code using the if
statemtent
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
if-statement xslt where
if-statement xslt where
asked Nov 20 '18 at 0:39
RABRAB
783116
783116
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The is no where
attribute for xsl:for-each
.
What you mean is called a predicate which is enclosed by Double brackets.
So change your xsl:for-each
from
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
to
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
That should do the trick.
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
You might like to note a minor difference between using a predicate in theselect
expression and usingxsl:if
within the body of thexsl:for-each
: in the first caseposition()
only counts the selected nodes, in the second case it counts them all.
– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?
– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384625%2fdifference-between-where-and-if-in-an-xslt-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The is no where
attribute for xsl:for-each
.
What you mean is called a predicate which is enclosed by Double brackets.
So change your xsl:for-each
from
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
to
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
That should do the trick.
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
You might like to note a minor difference between using a predicate in theselect
expression and usingxsl:if
within the body of thexsl:for-each
: in the first caseposition()
only counts the selected nodes, in the second case it counts them all.
– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?
– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
add a comment |
The is no where
attribute for xsl:for-each
.
What you mean is called a predicate which is enclosed by Double brackets.
So change your xsl:for-each
from
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
to
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
That should do the trick.
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
You might like to note a minor difference between using a predicate in theselect
expression and usingxsl:if
within the body of thexsl:for-each
: in the first caseposition()
only counts the selected nodes, in the second case it counts them all.
– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?
– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
add a comment |
The is no where
attribute for xsl:for-each
.
What you mean is called a predicate which is enclosed by Double brackets.
So change your xsl:for-each
from
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
to
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
That should do the trick.
The is no where
attribute for xsl:for-each
.
What you mean is called a predicate which is enclosed by Double brackets.
So change your xsl:for-each
from
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
to
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
That should do the trick.
answered Nov 20 '18 at 0:58
zx485zx485
13.6k122946
13.6k122946
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
You might like to note a minor difference between using a predicate in theselect
expression and usingxsl:if
within the body of thexsl:for-each
: in the first caseposition()
only counts the selected nodes, in the second case it counts them all.
– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?
– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
add a comment |
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
You might like to note a minor difference between using a predicate in theselect
expression and usingxsl:if
within the body of thexsl:for-each
: in the first caseposition()
only counts the selected nodes, in the second case it counts them all.
– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?
– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
Awesome, thanks! I just assumed because it turned red that the where clause was fine :P I'll definitely use this in future :)
– RAB
Nov 20 '18 at 1:12
1
1
You might like to note a minor difference between using a predicate in the
select
expression and using xsl:if
within the body of the xsl:for-each
: in the first case position()
only counts the selected nodes, in the second case it counts them all.– Michael Kay
Nov 20 '18 at 9:47
You might like to note a minor difference between using a predicate in the
select
expression and using xsl:if
within the body of the xsl:for-each
: in the first case position()
only counts the selected nodes, in the second case it counts them all.– Michael Kay
Nov 20 '18 at 9:47
@zx485 Can I have multiple bit in a for-each check? like:
ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?– RAB
Nov 21 '18 at 4:55
@zx485 Can I have multiple bit in a for-each check? like:
ODM/ODM/Study/MetaDataVersion[@MetaDataVersion=$MetaDataVersion]/CodeList[@OID=$varIEItem]/CodeListItem[@CodedValue=$varIEval]/Decode/TranslatedText
?– RAB
Nov 21 '18 at 4:55
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
@user10626943: Yes, you can. Why didn't you just try it out, for yourself? You should have seen that it's been working (correct syntax assumed).
– zx485
Nov 21 '18 at 18:13
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
It wasn't working for me, but I wasn't sure what the issue was and just wanted to eliminate this as a possibility :)
– RAB
Nov 21 '18 at 21:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384625%2fdifference-between-where-and-if-in-an-xslt-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown