My code returns the text only from the body of a webpage. I am trying to remove text from class="menu" items from the body of this page:
<div id="pre-header-links-inner" class="header-links"><ul id="menu-top-bar" class="menu"><li id="menu-item-22" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-22">Main Line: +1 000-000-0000</li>
<li id="menu-item-23" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-23">Sales: tel:000-000-0000</li>
<li id="menu-item-24" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-24">Email: info#example.com</li>
</ul></div>
</div>
</div>
</div>
<!-- #pre-header -->
<div id="header">
<div id="header-core">
<div id="logo">
<img width="253" height="50" src="https://www.example.com/logo.png" class="custom-logo" alt="Domain" itemprop="logo" /> </div>
<div id="header-links" class="main-navigation">
<div id="header-links-inner" class="header-links">
<ul id="menu-main-navigation" class="menu"><li id="menu-item-71" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-2 current_page_item"><span>Home</span></li>
<li id="menu-item-70" class="menu-item menu-item-type-post_type menu-item-object-page"><span>About Us</span></li>
<li id="menu-item-108" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Services</span></li>
<li id="menu-item-124" class="menu-item menu-item-type-post_type menu-item-object-page"><span>API</span></li>
<li id="menu-item-68" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Contact Us</span></li>
</ul>
</div>
</div>
<!-- #header-links .main-navigation -->
<div id="header-nav"><a class="btn-navbar" data-toggle="collapse" data-target=".nav-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></a></div>
</div>
</div>
<!-- #header -->
<div id="header-responsive"><div id="header-responsive-inner" class="responsive-links nav-collapse collapse"><ul id="menu-main-navigation-1" class=""><li id="res-menu-item-71" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-2 current_page_item"><span>Home</span></li>
<li id="res-menu-item-70" class="menu-item menu-item-type-post_type menu-item-object-page"><span>About Us</span></li>
<li id="res-menu-item-108" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Services</span></li>
<li id="res-menu-item-124" class="menu-item menu-item-type-post_type menu-item-object-page"><span>API</span></li>
<li id="res-menu-item-68" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Contact Us</span></li>
</ul></div></div>
<div id="header-sticky">
<div id="header-sticky-core">
<div id="logo-sticky">
<img width="253" height="50" src="https://www.example.com/logo.png" class="custom-logo" alt="Logo" itemprop="logo" /> </div>
<div id="header-sticky-links" class="main-navigation">
<div id="header-sticky-links-inner" class="header-links">
<ul id="menu-main-navigation-2" class="menu"><li id="menu-item-71" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-2 current_page_item"><span>Home</span></li>
<li id="menu-item-70" class="menu-item menu-item-type-post_type menu-item-object-page"><span>About Us</span></li>
<li id="menu-item-108" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Services</span></li>
<li id="menu-item-124" class="menu-item menu-item-type-post_type menu-item-object-page"><span>API</span></li>
<li id="menu-item-68" class="menu-item menu-item-type-post_type menu-item-object-page"><span>Contact Us</span></li>
</ul>
Strange thing is - when I call the following line:
text = "".join(tree.xpath("//body//*[not(#class='menu')]//text()")).strip()
it returns the entire plain-text source code as-is (ie. even with the text from class="text" elements).
However, when I remove the not keyword:
text = "".join(tree.xpath("//body//*[(#class='menu')]//text()")).strip()
... it correctly identifies the text from the class="text" elements and isolates their text perfectly:
Main Line: +000-000-0000
Sales: +1 000-000-0000
Email: info#example.com
Home
About Us
Services
API
Contact Us
Home
About Us
Services
API
Contact Us
What am I doing it wrong? I'd like it to return the text from everything EXCEPT elements where the class='menu'.
it returns the entire plain-text source code
You need to be clear about the distinction between what the XPath expression SELECTS, and what the application handling the XPath result DISPLAYS.
XPath returns a set of nodes, and it is very common practice for the calling application to display each of those nodes by showing the entire subtree rooted at that node. But it's not XPath that's doing this; it's the calling application. Your selection criteria determine what nodes are selected by the XPath expression, but they don't affect which descendants of those selected nodes are displayed by the calling application.
Related
I want to display only those pages in the header which have child pages inside them. Using the following code
<nav class="wsmenu clearfix" data-sly-use.nav="in.gov.odishatourism.core.models.HeaderNavigation">
<ul class="wsmenu-list" >
<li aria-haspopup="true" data-sly-repeat="${nav.root.listChildren}" ><span class="wsmenu-click" ><i class="wsmenu-arrow fa fa-angle-down"></i></span>
<a class="" href="javascript:void(0);" data-sly-test="!${item.hasChild}">${item.title}</a>
<div class="wsmegamenu clearfix ">
<div class="container paddingmenu">
<div class="row">
<div class="menuimgdiv" data-sly-repeat.subcategory="${item.listChildren}">
<h3 class="title" data-sly-test.subcat="${subcategory.title}" >${subcat}</h3>
<ul>
<li data-sly-repeat.page="${subcategory.listChildren}"><a class="" href="${page.path # extension='html'}">${page.title}</a></li>
</ul>
</div>
</div>
</div>
</div>
</li>
<li aria-haspopup="true"> Dept of Tourism</li>
</ul>
<a class="wsdownmenu-animated-arrow"><span></span></a>
<div class="wsdownmenu-text">Navigation</div>
</nav>
gives the following output
But this code -
<nav class="wsmenu clearfix" data-sly-use.nav="in.gov.odishatourism.core.models.HeaderNavigation">
<ul class="wsmenu-list" >
<li aria-haspopup="true" data-sly-repeat="${nav.root.listChildren}" ><span class="wsmenu-click" ><i class="wsmenu-arrow fa fa-angle-down"></i></span>
<a class="" href="javascript:void(0);" data-sly-test="${item.hasChild}">${item.title}</a>
<div class="wsmegamenu clearfix ">
<div class="container paddingmenu">
<div class="row">
<div class="menuimgdiv" data-sly-repeat.subcategory="${item.listChildren}">
<h3 class="title" data-sly-test.subcat="${subcategory.title}" >${subcat}</h3>
<ul>
<li data-sly-repeat.page="${subcategory.listChildren}"><a class="" href="${page.path # extension='html'}">${page.title}</a></li>
</ul>
</div>
</div>
</div>
</div>
</li>
<li aria-haspopup="true"> Dept of Tourism</li>
</ul>
<a class="wsdownmenu-animated-arrow"><span></span></a>
<div class="wsdownmenu-text">Navigation</div>
</nav>
gives the output as shown below
The code
<a class="" href="javascript:void(0);" data-sly-test="${item.hasChild}">${item.title}</a>
is not working properly.
My site structure is as shown below
And the actual desired output is
You cannot use ${item.hasChild} in your test condition as the hasChild() method of the Page API requires you to pass a parameter. AFAIK, HTL doesn't support invocation of parameterized methods.
Since there is no direct API available to check if a page has child pages or not, you may need to do the following to validate if the page has child pages
<a class="" href="javascript:void(0);" data-sly-test="${item.listChildren && item.listChildren.hasNext}">${item.title}</a>
However, I would prefer building the entire navigation tree using Sling models or WCM Use API and not invoking so many methods in the HTL. That would make the code easier to maintain, change and test. YMMV
I want to display names of every child pages of the parent of the current page except the current page in the component in aem. But my output is coming including the current page name.
<section id="touractivities">
<div class="container">
<div class="row">
<div class="col-sm-12">
<ul class="row buttontab">
<li class="col-md col-sm-4 col-6" data-sly-repeat="${CurrentPage.parent.listChildren}">
<div class="btnbggradient">
<a href="${item.path}.html" class="shadow">
<i class="icon-${item.name}"></i>
<h5 >${item.title}</h5>
</a>
</div>
</li>
</ul>
</div>
</div>
</div></section>
my aem structure:
required output:
output im getting:
The code is correct except that the bird watching li should not come while i am in bird watching page.
You can switch from data-sly-repeat to data-sly-list (so you can move it to the ul element). You can then use data-sly-test on the li element and check each item path against the current resource/page path:
<li data-sly-test=“${item.path != resource.path}” ...
<section id="touractivities">
<div class="container">
<div class="row">
<div class="col-sm-12">
<ul class="row buttontab" data-sly-list="${CurrentPage.parent.listChildren}">
<li class="col-md col-sm-4 col-6" data-sly-test="${item.path != currentPage.path}" >
<div class="btnbggradient">
<a href="${item.path}.html" class="shadow">
<i class="icon-${item.name}"></i>
<h5 >${item.title}</h5>
</a>
</div>
</li>
</ul>
</div>
</div>
</div></section>
My navbar on the phone opens, but it does not close.
<div class="carousel-inner">
<nav class="navbar navbar-expand-lg navbar-light bg-primary nawigacja">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar10">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse" id="navbar10">
<ul class="navbar-nav nav-fill w-100">
<li class="nav-item">
<a class="nav-link" href="#">First tab</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#button1">2nd Tab</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#button2">3rd Tab</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#button3">4th tab</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="#button4">5th Tab</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">6th Tab</a>
</li>
</ul>
</div>
</div>
in the elements I see that before clicking the code looks like this:
<div class="navbar-collapse collapse" id="navbar10">
and literally for a few seconds the code changes into:
navbar-collapse collapsing
next in 1 sec:
navbar-collapse collapse show
after another click it changes to
navbar-collapse collapsing
and after 1 sec again:
navbar-collapse collapse show
Navbar does not close at all after opening.
//edit:
I will add that on codeply it works.
I managed to fix it.
For others who are struggling with the same problem, I will tell you that I had to remove the following from the section:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
Example of problem is here:
http://liveweave.com/fRs3PL
Basically, I have a modal being triggered from a dropdown, and for some reason the modal hides behind the grey shadow and then disappears altogether a moment later.
What is going on here?!
You need to take the revel modal (<div class="reveal">) and all of its contents and place it so that it is a child of only the <body> element. You don't want it to be contained inside of other elements.
<body>
<div class="row">
<div class="small-12 medium-3 columns">
<ul class="dropdown menu" data-dropdown-menu="" role="menubar" data-dropdownmenu="3z4e20-dropdownmenu" aria-selected="false" aria-expanded="false" data-is-click="false">
<li role="menuitem" class="has-submenu is-dropdown-submenu-parent is-down-arrow" aria-haspopup="true" aria-selected="false" aria-expanded="false" aria-label="Actions" data-is-click="false">
Actions
<ul class="menu submenu is-dropdown-submenu first-sub vertical" data-submenu="" aria-hidden="true" role="menu">
<li role="menuitem" class="is-submenu-item is-dropdown-submenu-item"><a data-open="change-password-modal-0" aria-controls="change-password-modal-0" id="8p079l-reveal" aria-haspopup="true" tabindex="0">Change Password</a>
</li>
<li role="menuitem" class="is-submenu-item is-dropdown-submenu-item">Disable</li>
<li role="menuitem" class="is-submenu-item is-dropdown-submenu-item">Delete</li>
<li role="menuitem" class="is-submenu-item is-dropdown-submenu-item">Transfer</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="reveal" id="change-password-modal-0" data-reveal="rcsjob-reveal" data-reset-on-close="true" aria-labelledby="8p079l-reveal" role="dialog" aria-hidden="true" data-yeti-box="change-password-modal-0" data-resize="change-password-modal-0">
<h1>Change Password</h1>
<p class="lead">You are changing the password for:</p>
<label>Password: <input type="text"></label>
<button class="close-button" data-close="" aria-label="Close reveal" type="button">
<span aria-hidden="true">×</span>
</button>
</div>
</body>
In bootstrap-4, the first time I click a dropdown in a tab menu it works fine, but after that it stops working.
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#home" role="tab" data-toggle="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#profile" role="tab" data-toggle="tab">Profile</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#action" data-toggle="tab">action</a>
<a class="dropdown-item" href="#action_2" data-toggle="tab">Another action</a>
</div>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
HOME
</div>
<div role="tabpanel" class="tab-pane" id="profile">
PROFILE
</div>
<div role="tabpanel" class="tab-pane" id="action">
ACTION 1
</div>
<div role="tabpanel" class="tab-pane" id="action_2">
ACTION 2
</div>
</div>
Here's a Demo in Bootply
The issue appears to be caused by not clearing the .active class from the dropdown item when another tab is opened, therefore leaving it highlighted and preventing the same dropdown item from being clicked again.
According to the workaround in Issue #17371 - navs dropdown tab bug, you can add the following js until the issue is resolved:
$('.nav-tabs').on('shown.bs.tab', 'a', function (e) {
if (e.relatedTarget) {
$(e.relatedTarget).removeClass('active');
}
})
Demo in Stack Snippets
$('.nav-tabs').on('shown.bs.tab', 'a', function (e) {
if (e.relatedTarget) {
$(e.relatedTarget).removeClass('active');
}
})
<!-- 4.0.0-alpha.2 Libraries -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js"></script>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#home" role="tab" data-toggle="tab">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#profile" role="tab" data-toggle="tab">Profile</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#action" data-toggle="tab">action</a>
<a class="dropdown-item" href="#action_2" data-toggle="tab">Another action</a>
</div>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
HOME
</div>
<div role="tabpanel" class="tab-pane" id="profile">
PROFILE
</div>
<div role="tabpanel" class="tab-pane" id="action">
ACTION 1
</div>
<div role="tabpanel" class="tab-pane" id="action_2">
ACTION 2
</div>
</div>