XSLT copy by attribute
I have a problem. Cant get to copy only node with specific attribute in XSLT. My XML;
<root>
<mouse code="red"> .. </mouse>
<mouse code="blue"> .. </mouse>
<mouse code="green"> .. </mouse>
</root>
I am trying to copy only node with red attribute to get XML like this:
<root>
<mouse code="red"> .. </mouse>
</root>
Can this be done with simple XSLT transformation?
DeLuka
xslt attributes nodes
add a comment |
I have a problem. Cant get to copy only node with specific attribute in XSLT. My XML;
<root>
<mouse code="red"> .. </mouse>
<mouse code="blue"> .. </mouse>
<mouse code="green"> .. </mouse>
</root>
I am trying to copy only node with red attribute to get XML like this:
<root>
<mouse code="red"> .. </mouse>
</root>
Can this be done with simple XSLT transformation?
DeLuka
xslt attributes nodes
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
No,code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.
– michael.hor257k
Nov 14 '18 at 12:06
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38
add a comment |
I have a problem. Cant get to copy only node with specific attribute in XSLT. My XML;
<root>
<mouse code="red"> .. </mouse>
<mouse code="blue"> .. </mouse>
<mouse code="green"> .. </mouse>
</root>
I am trying to copy only node with red attribute to get XML like this:
<root>
<mouse code="red"> .. </mouse>
</root>
Can this be done with simple XSLT transformation?
DeLuka
xslt attributes nodes
I have a problem. Cant get to copy only node with specific attribute in XSLT. My XML;
<root>
<mouse code="red"> .. </mouse>
<mouse code="blue"> .. </mouse>
<mouse code="green"> .. </mouse>
</root>
I am trying to copy only node with red attribute to get XML like this:
<root>
<mouse code="red"> .. </mouse>
</root>
Can this be done with simple XSLT transformation?
DeLuka
xslt attributes nodes
xslt attributes nodes
edited Nov 14 '18 at 14:37
DeLuka
asked Nov 14 '18 at 5:48
DeLukaDeLuka
103
103
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
No,code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.
– michael.hor257k
Nov 14 '18 at 12:06
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38
add a comment |
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
No,code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.
– michael.hor257k
Nov 14 '18 at 12:06
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
No,
code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.– michael.hor257k
Nov 14 '18 at 12:06
No,
code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.– michael.hor257k
Nov 14 '18 at 12:06
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38
add a comment |
2 Answers
2
active
oldest
votes
You can do simply:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>
add a comment |
This can be done simply with xsl:templates matching the node you want and using copy/copy-of.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="mouse[@code='red']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Of course, if there are other elements within mouse[@code='red'] then you'll need to handle them with another template.
1
This is not a good idea, because you are applying templates to allmouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ
– michael.hor257k
Nov 14 '18 at 7:05
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%2f53293894%2fxslt-copy-by-attribute%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do simply:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>
add a comment |
You can do simply:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>
add a comment |
You can do simply:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>
You can do simply:
<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>
answered Nov 14 '18 at 7:03
michael.hor257kmichael.hor257k
74.9k42237
74.9k42237
add a comment |
add a comment |
This can be done simply with xsl:templates matching the node you want and using copy/copy-of.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="mouse[@code='red']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Of course, if there are other elements within mouse[@code='red'] then you'll need to handle them with another template.
1
This is not a good idea, because you are applying templates to allmouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ
– michael.hor257k
Nov 14 '18 at 7:05
add a comment |
This can be done simply with xsl:templates matching the node you want and using copy/copy-of.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="mouse[@code='red']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Of course, if there are other elements within mouse[@code='red'] then you'll need to handle them with another template.
1
This is not a good idea, because you are applying templates to allmouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ
– michael.hor257k
Nov 14 '18 at 7:05
add a comment |
This can be done simply with xsl:templates matching the node you want and using copy/copy-of.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="mouse[@code='red']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Of course, if there are other elements within mouse[@code='red'] then you'll need to handle them with another template.
This can be done simply with xsl:templates matching the node you want and using copy/copy-of.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="mouse[@code='red']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
Of course, if there are other elements within mouse[@code='red'] then you'll need to handle them with another template.
answered Nov 14 '18 at 6:49
JtakedaJtakeda
91
91
1
This is not a good idea, because you are applying templates to allmouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ
– michael.hor257k
Nov 14 '18 at 7:05
add a comment |
1
This is not a good idea, because you are applying templates to allmouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ
– michael.hor257k
Nov 14 '18 at 7:05
1
1
This is not a good idea, because you are applying templates to all
mouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ– michael.hor257k
Nov 14 '18 at 7:05
This is not a good idea, because you are applying templates to all
mouse
nodes - and the built-in template rules will process them and write their text content to the output - see: xsltransform.hikmatu.com/94hvTyJ– michael.hor257k
Nov 14 '18 at 7:05
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%2f53293894%2fxslt-copy-by-attribute%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
Your "XML" is not well-formed.
– michael.hor257k
Nov 14 '18 at 7:06
Corrected. Thanks for noticing.
– DeLuka
Nov 14 '18 at 11:44
No,
code=red
is not "corrected". Why does it matter? Because I had to correct it in order to test.– michael.hor257k
Nov 14 '18 at 12:06
Strange, I am pretty sure it was corrected last time. Thanks.
– DeLuka
Nov 14 '18 at 14:38