Unable to list relative elements XSLT 2.0
I am trying to learn XSLT 2.0. In the example below, I am trying to list languages and books available in each language. Can you please help me understand how the context works?
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Library.xsl"?>
<Library>
<Books>
<Book>
<Language code="English">English</Language>
<code>1</code>
<Title>History</Title>
</Book>
<Book>
<Language code="Spanish">Spanish</Language>
<code>2</code>
<Title>Math</Title>
</Book>
</Books>
<Languages>
<Language code="English">English</Language>
<Language code="Spanish">Spanish</Language>
</Languages>
</Library>
// Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" /><br /><br />
<xsl:variable name="key" select="@code" />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book">
<xsl:if test="Language[@code=string($key)]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" />
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
// Desired output:
Language: English
Title: English Book
Language: Spanish
Title: Spanish Book
xml xslt stylesheet
add a comment |
I am trying to learn XSLT 2.0. In the example below, I am trying to list languages and books available in each language. Can you please help me understand how the context works?
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Library.xsl"?>
<Library>
<Books>
<Book>
<Language code="English">English</Language>
<code>1</code>
<Title>History</Title>
</Book>
<Book>
<Language code="Spanish">Spanish</Language>
<code>2</code>
<Title>Math</Title>
</Book>
</Books>
<Languages>
<Language code="English">English</Language>
<Language code="Spanish">Spanish</Language>
</Languages>
</Library>
// Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" /><br /><br />
<xsl:variable name="key" select="@code" />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book">
<xsl:if test="Language[@code=string($key)]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" />
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
// Desired output:
Language: English
Title: English Book
Language: Spanish
Title: Spanish Book
xml xslt stylesheet
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared thexsl:variable
inside of atd
element, it will not be available outside of thetd
. You can simply use thecurrent()
function in a predicate of the innerxsl:for-each
:<xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.
– Martin Honnen
Nov 22 '18 at 19:59
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29
add a comment |
I am trying to learn XSLT 2.0. In the example below, I am trying to list languages and books available in each language. Can you please help me understand how the context works?
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Library.xsl"?>
<Library>
<Books>
<Book>
<Language code="English">English</Language>
<code>1</code>
<Title>History</Title>
</Book>
<Book>
<Language code="Spanish">Spanish</Language>
<code>2</code>
<Title>Math</Title>
</Book>
</Books>
<Languages>
<Language code="English">English</Language>
<Language code="Spanish">Spanish</Language>
</Languages>
</Library>
// Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" /><br /><br />
<xsl:variable name="key" select="@code" />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book">
<xsl:if test="Language[@code=string($key)]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" />
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
// Desired output:
Language: English
Title: English Book
Language: Spanish
Title: Spanish Book
xml xslt stylesheet
I am trying to learn XSLT 2.0. In the example below, I am trying to list languages and books available in each language. Can you please help me understand how the context works?
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Library.xsl"?>
<Library>
<Books>
<Book>
<Language code="English">English</Language>
<code>1</code>
<Title>History</Title>
</Book>
<Book>
<Language code="Spanish">Spanish</Language>
<code>2</code>
<Title>Math</Title>
</Book>
</Books>
<Languages>
<Language code="English">English</Language>
<Language code="Spanish">Spanish</Language>
</Languages>
</Library>
// Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" /><br /><br />
<xsl:variable name="key" select="@code" />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book">
<xsl:if test="Language[@code=string($key)]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="@code" />
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
// Desired output:
Language: English
Title: English Book
Language: Spanish
Title: Spanish Book
xml xslt stylesheet
xml xslt stylesheet
edited Nov 22 '18 at 20:58
zx485
14.8k133048
14.8k133048
asked Nov 22 '18 at 19:32
Surinder SinghSurinder Singh
32
32
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared thexsl:variable
inside of atd
element, it will not be available outside of thetd
. You can simply use thecurrent()
function in a predicate of the innerxsl:for-each
:<xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.
– Martin Honnen
Nov 22 '18 at 19:59
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29
add a comment |
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared thexsl:variable
inside of atd
element, it will not be available outside of thetd
. You can simply use thecurrent()
function in a predicate of the innerxsl:for-each
:<xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.
– Martin Honnen
Nov 22 '18 at 19:59
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared the
xsl:variable
inside of a td
element, it will not be available outside of the td
. You can simply use the current()
function in a predicate of the inner xsl:for-each
: <xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.– Martin Honnen
Nov 22 '18 at 19:59
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared the
xsl:variable
inside of a td
element, it will not be available outside of the td
. You can simply use the current()
function in a predicate of the inner xsl:for-each
: <xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.– Martin Honnen
Nov 22 '18 at 19:59
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29
add a comment |
1 Answer
1
active
oldest
votes
To get your desired output, you can change your XSLT-1.0 file to the following (XSLT-2.0 does not seem to be necessary). One crucial aspect was moving the <xsl:variable name="key" select="@code" />
out of the td
element up two layers, so that it can be accessed in the xsl:for-each
.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<xsl:variable name="key" select="@code" />
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Language: ', @code)" /><br /><br />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book[Language[@code=$key]]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Title: ', $key,' Book')" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
<html>
<body>
<table>
<tr>
<td style="border:1px solid red;">Language: English</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: English Book</td>
</tr>
<tr>
<td style="border:1px solid red;">Language: Spanish</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: Spanish Book</td>
</tr>
</table>
</body>
</html>
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
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%2f53437135%2funable-to-list-relative-elements-xslt-2-0%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
To get your desired output, you can change your XSLT-1.0 file to the following (XSLT-2.0 does not seem to be necessary). One crucial aspect was moving the <xsl:variable name="key" select="@code" />
out of the td
element up two layers, so that it can be accessed in the xsl:for-each
.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<xsl:variable name="key" select="@code" />
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Language: ', @code)" /><br /><br />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book[Language[@code=$key]]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Title: ', $key,' Book')" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
<html>
<body>
<table>
<tr>
<td style="border:1px solid red;">Language: English</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: English Book</td>
</tr>
<tr>
<td style="border:1px solid red;">Language: Spanish</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: Spanish Book</td>
</tr>
</table>
</body>
</html>
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
add a comment |
To get your desired output, you can change your XSLT-1.0 file to the following (XSLT-2.0 does not seem to be necessary). One crucial aspect was moving the <xsl:variable name="key" select="@code" />
out of the td
element up two layers, so that it can be accessed in the xsl:for-each
.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<xsl:variable name="key" select="@code" />
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Language: ', @code)" /><br /><br />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book[Language[@code=$key]]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Title: ', $key,' Book')" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
<html>
<body>
<table>
<tr>
<td style="border:1px solid red;">Language: English</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: English Book</td>
</tr>
<tr>
<td style="border:1px solid red;">Language: Spanish</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: Spanish Book</td>
</tr>
</table>
</body>
</html>
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
add a comment |
To get your desired output, you can change your XSLT-1.0 file to the following (XSLT-2.0 does not seem to be necessary). One crucial aspect was moving the <xsl:variable name="key" select="@code" />
out of the td
element up two layers, so that it can be accessed in the xsl:for-each
.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<xsl:variable name="key" select="@code" />
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Language: ', @code)" /><br /><br />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book[Language[@code=$key]]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Title: ', $key,' Book')" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
<html>
<body>
<table>
<tr>
<td style="border:1px solid red;">Language: English</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: English Book</td>
</tr>
<tr>
<td style="border:1px solid red;">Language: Spanish</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: Spanish Book</td>
</tr>
</table>
</body>
</html>
To get your desired output, you can change your XSLT-1.0 file to the following (XSLT-2.0 does not seem to be necessary). One crucial aspect was moving the <xsl:variable name="key" select="@code" />
out of the td
element up two layers, so that it can be accessed in the xsl:for-each
.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="/Library/Languages/Language">
<xsl:variable name="key" select="@code" />
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Language: ', @code)" /><br /><br />
</td>
</tr>
<xsl:for-each select="/Library/Books/Book[Language[@code=$key]]">
<tr>
<td style="border:1px solid red;">
<xsl:value-of select="concat('Title: ', $key,' Book')" />
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The output is:
<html>
<body>
<table>
<tr>
<td style="border:1px solid red;">Language: English</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: English Book</td>
</tr>
<tr>
<td style="border:1px solid red;">Language: Spanish</td>
</tr>
<tr>
<td style="border:1px solid red;">Title: Spanish Book</td>
</tr>
</table>
</body>
</html>
answered Nov 22 '18 at 21:02
zx485zx485
14.8k133048
14.8k133048
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
add a comment |
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
Thank you so much! Works just as expected. Happy Thanksgiving and good luck with your Black Friday shopping!
– Surinder Singh
Nov 23 '18 at 15:29
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%2f53437135%2funable-to-list-relative-elements-xslt-2-0%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
You haven't used any XSLT 2 specific code. Furthermore, does that code run at all? How do you try to use XSLT 2? You have declared the
xsl:variable
inside of atd
element, it will not be available outside of thetd
. You can simply use thecurrent()
function in a predicate of the innerxsl:for-each
:<xsl:for-each select="/Library/Books/Book[Language/@code = current()/@code]">
. Not sure about the output you want, show us the HTML with the sample data matching the input, I don't see any title "English book" or "Spanish book" in the input sample.– Martin Honnen
Nov 22 '18 at 19:59
I would suggest you learn about keys, which were designed for tasks like this.
– michael.hor257k
Nov 23 '18 at 9:29