Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:41:21+00:00 2026-06-10T23:41:21+00:00

i have xml file and xslt transformation below: <?xml version=1.0 encoding=utf-8 ?> <?xml-stylesheet version=1.0

  • 0

i have xml file and xslt transformation below:

    <?xml version="1.0" encoding="utf-8" ?>
    <?xml-stylesheet version="1.0" type="text/xml" href="pets.xsl" ?>
    <pets>
        <pet name="Jace" type="dog" />
        <pet name="Babson" type="" />
        <pet name="Oakley" type="cat" />
        <pet name="Tabby" type="dog" />
    </pets>

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:key name="pets-key" match="pet" use="type" />
    <xsl:template match="/" >
        <html>
            <head><title></title></head>
            <body>
                <xsl:for-each select="key('pets-key', '' )" >
                    <xsl:value-of select="@name" />
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

How can i select all pets which types not empty using key function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:41:23+00:00Added an answer on June 10, 2026 at 11:41 pm

    Two points to note:

    1. There is an error in your key definition. You need to use use=”@type”, not use=”type”
    2. You need a set difference to select all pets whose type is non-empty, and still use the key() function. The general recipe for set difference in XPATH 1.0 is…

      $node-set1[count(. | $node-set2) != count($node-set2)]

    Putting it altogether, a correct but inefficent XSLT 1.0 style-sheet to use the key() and list all pets with not empty type is…

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="html" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:key name="kPets" match="pet" use="@type" />
    
    <xsl:template match="/" >
      <html>
        <head><title>Pets with a type</title></head>
        <body>   
          <ul>
            <xsl:for-each select="*/pet[count(. | key('kPets', '' )) != count(key('kPets', '' ))]" >
              <li><xsl:value-of select="@name" /></li>
            </xsl:for-each>
          </ul>
        </body>
       </html>
    </xsl:template>
    
    </xsl:stylesheet>
    

    This yields output…

    <html>
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Pets with a type</title>
      </head>
      <body>
        <ul>
          <li>Jace</li>
          <li>Oakley</li>
          <li>Tabby</li>
        </ul>
      </body>
    </html>
    

    Having said that, the question does not really fit as a good excersize in using keys. In real life, if you wanted to achieve this outcome, a better, more efficient XSLT 1.0 solution would be …

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="html" indent="yes" />
    <xsl:strip-space elements="*" />
    
    <xsl:key name="kPets" match="pet" use="@type" />
    
    <xsl:template match="/*" >
      <html>
        <head><title>Pets with a type</title></head>
        <body>   
          <ul>
            <xsl:apply-templates />
          </ul>
        </body>
       </html>
    </xsl:template>
    
    <xsl:template match="pet[@type != .]">
      <li><xsl:value-of select="@name" /></li>
    </xsl:template>  
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file like this one: receipt.xml <?xml version=1.0 encoding=ISO-8859-1?> <?xml-stylesheet type=text/xml
i have xml file that looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE pointList SYSTEM
I have an XSLT stylesheet that transforms an XML file to JSON format and
I have an XML file formatted like <paragraph> Some Free text goes here<LinkType1 href=link1
I have this XML file: <?xml version=1.0?> <xi:include href=http://www.w3schools.com/dom/books.xml xmlns:xi=http://www.w3.org/2003/XInclude/> and I expected that
I have an XML file which has to be transformed using XSLT, I am
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have written an xslt that reads some xml file names and does some
im novice in XML and XSLT. i have a an xml file ( book.xml
I have a book.xml and book.xslt the output has been set on text-mode, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.