XSLT copy by attribute










0















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










share|improve this question
























  • 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
















0















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










share|improve this question
























  • 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














0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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













2 Answers
2






active

oldest

votes


















1














You can do simply:



<xsl:template match="/root">
<xsl:copy>
<xsl:copy-of select="mouse[@code='red']"/>
</xsl:copy>
</xsl:template>





share|improve this answer






























    -1














    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.






    share|improve this answer


















    • 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










    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    You can do simply:



    <xsl:template match="/root">
    <xsl:copy>
    <xsl:copy-of select="mouse[@code='red']"/>
    </xsl:copy>
    </xsl:template>





    share|improve this answer



























      1














      You can do simply:



      <xsl:template match="/root">
      <xsl:copy>
      <xsl:copy-of select="mouse[@code='red']"/>
      </xsl:copy>
      </xsl:template>





      share|improve this answer

























        1












        1








        1







        You can do simply:



        <xsl:template match="/root">
        <xsl:copy>
        <xsl:copy-of select="mouse[@code='red']"/>
        </xsl:copy>
        </xsl:template>





        share|improve this answer













        You can do simply:



        <xsl:template match="/root">
        <xsl:copy>
        <xsl:copy-of select="mouse[@code='red']"/>
        </xsl:copy>
        </xsl:template>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 7:03









        michael.hor257kmichael.hor257k

        74.9k42237




        74.9k42237























            -1














            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.






            share|improve this answer


















            • 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















            -1














            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.






            share|improve this answer


















            • 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













            -1












            -1








            -1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 6:49









            JtakedaJtakeda

            91




            91







            • 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












            • 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







            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

















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto