building django template files with xslt - xslt

I have about 4,000 html documents that i am trying to convert into django templates using xslt. The problem that I am having is that xslt is escaping the '{' curly braces for template variables, when I try to include a template variable inside of an attribute tag;
my xslt file looks like this:
<xsl:template match="p">
<p>
<xsl:attribute name="nid"><xsl:value-of select="$node_id"/></xsl:attribute>
<xsl:apply-templates select="*|node()"/>
</p>
<span>
{% get_comment_count for thing '<xsl:value-of select="$node_id"/>' as node_count %}
{{ node_count }} //This works as expected
</span>
<div>
<xsl:attribute name="class">HControl</xsl:attribute>
<xsl:text disable-output-escaping="yes">{% if node_count > 0 %}</xsl:text> // have to escape this because of the '>'
<div class="comment-list">
{% get_comment_list for thing '<xsl:value-of select="$node_id"/>' as node_comments %}
{% for comment in node_comments %}
<div class="comment {{ comment.object_id }}"> // this gets escaped
<a>
<xsl:attribute name="name">c{{ comment.id }}</xsl:attribute> //and so does this
</a>
<a>
<xsl:attribute name="href">
{% get_comment_permalink comment %}
</xsl:attribute>
permalink for comment #{{ forloop.counter }}
</a>
<div>
{{ comment.comment }}
</div>
</div>
{% endfor %}
</div>
{% endif %}
</div>
the output looks something like this:
<div>
<p nid="50:1r:SB:1101S:5">
<span class="Insert">B. A person who violates this section is guilty of a class 1 misdemeanor.</span>
</p>
<span>
1
</span>
<div class="HControl">
<div class="comment-list">
<div class="comment '{ comment.object_id }'"> // this should be class="comment #c123"
<a name="c%7B%7B%20comment.id%20%7D%7D"></a> // this should name="c123"
<a href="%7B%%20get_comment_permalink%20comment%20%%7D"> //this should be an href to the comment
permalink for comment #1
</a>
<div>
Well you should show some respect!
</div>
</div>
</div>
</div>
I transform the file with lxml.etree and then pass the string to a django template object, and render it.
I just dont seem to understand how to get the xslt parser to leave the curly braces alone

XSLT has its own purpose for curly braces - they are used in Attribute Value Templates, like this:
<!-- $someVariableOrExpression will be evaluated here -->
<div title="{$someVariableOrExpression}" />
To get literal curly braces into attribute values in XSLT, you need to escape them, which is done by doubling them:
<!-- the title will be "{$someVariableOrExpression}" here -->
<div title="{{$someVariableOrExpression}}" />
So if you want to output literal double curly braces, you need (guess what):
<div title="{{{{$someVariableOrExpression}}}}" />

Related

xsl loop with new name for template tag

I am trying to use a loop iteration in xslt. I want to loop through all the text with tei of "orgName", and generate a different popover-body for each one. I hope it would be something like (div class="Org-popover-body-1),(div class="Org-popover-body-2)... What should I put in ??? Thanks beforehand.
<xsl:template match="tei:orgName">
<xsl:for-each select="orgName">
<a class="orgName" id="orgNameinfo" data-toggle="popover-2" data-trigger="fcours" data-popover-content-2="#a2" data-placement="right">
<xsl:attribute name="href">
<xsl:text>#</xsl:text>
<xsl:value-of select="#key" />
</xsl:attribute>
</a>
<div id="a2" class="hidden">
<div class="popover-heading2">Orgnization Information <span style="float:right;cursor:pointer;" class="fa fa-times" data-toggle="popover"></span>
</div>
<div class="Org-popover-body-???">
</div>
</div>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
I think what you need are Attribute Value Templates, so you would write this...
<div class="Org-popover-body-{position()}">
You probably want to do this in the id of the hidden div to (to avoid multiple divs with the same id)
<div id="a{position()}" class="hidden">
And similarly in the data-popover-content-2 attribute
... data-popover-content-2="#a{position()}" ...

Form validation, HTML5 pattern does not match

I am new in Thymeleaf, had searched on the web and SO for usage of html regex. I am not passing a th:object to my frontend for this page, hence What I would like is to handle validation with a pattern attribute in the form input. Would like to warn user if they don't enter extension for the file. (".xml" in this case). So I'd like to see if string contains substring or not.
However could not see why following fails.
(Used the fileName field with pattern="/xml/", since static value is being checked on the regex. Also tried th:pattern but no luck.)
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" layout:decorator="#{layouts/main}">
<div layout:fragment="content" class="container">
<div class="page-header">
<h2>Add New Project</h2>
</div>
<!-- multistep form -->
<div id="msform">
<!-- progressbar -->
<ul id="progressbar">
<li class="active">Project Details</li>
<li>Choose operations</li>
<li>Save operations</li>
<li>project saved</li>
</ul>
<form id="project-details" class="project" method="post">
<h2 class="fs-title">Project Details</h2>
<h3 class="fs-subtitle">This is step 1</h3>
<input type="text" th:field="${testSuitProject.name}" placeholder="Project Name" />
<input type="text" th:field="${testSuitProject.endpoint}" placeholder="Service End Point" />
<input type="text" th:field="${testSuitProject.fileName}" placeholder="FileName ex: sample_project.xml" pattern="/xml/" />
<input type="submit" name="next" id="btn-first" class="next action-button" value="Next" />
</form>
</div>
</div>
Any suggestions appreciated.
Your regex is simply wrong. To match string to end with ".xml":
<input type="text" placeholder="FileName ex: sample_project.xml" pattern=".*\.xml" />
.* = match any characters (in the beginning of the string).
\. = a literal dot, since dot means any character in regex.
(Maybe you confuse with JavaScript where reg expression is surrounded by /:es to create a regex object.)

preg_replace regular expression to replace link within a particular tags

I need one help, i want to replace the href link to my link within a particular div class only.
<div id="slider1" class="owl-carousel owl-theme">
<div class="item">
<div class="imagens">
<img src="https://image.oldste.org" alt="The Fate of the Furious" width="100%" height="100%" />
<span class="imdb">
<b class="icon-star"></b> N/A
</span>
</div>
<span class="ttps">The Fate of the Furious</span>
<span class="ytps">2017</span>
</div>
</div>
Here i want to change http://oldsite.com/ to http://newsite.com/?id=
i want these href links like
<a href="http://newsite.com/?id=the-fate-of-the-furious">
Please help me with preg_replace regular expression.
Thanks
this may help you
$content = get_the_content();
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$newurl = get_permalink();
$content = preg_replace($pattern,$newurl,$content);
echo $content;
Lookbehinds are too expensive, use \K to start the fullstring match and avoid a capture group.
<a href="\K[^"]+\/ This pattern will be very efficient. I should state that this pattern will match ALL <a href urls. It also matches greedily until it finds the last / in the url -- I assume this is okay by your input sample.
Pattern Demo
Code (PHP Demo):
$in='<div id="slider1" class="owl-carousel owl-theme">
<div class="item">
<div class="imagens">
<img src="https://image.oldste.org" alt="The Fate of the Furious" width="100%" height="100%" />
<span class="imdb"><b class="icon-star"></b> N/A</span>
</div>
<span class="ttps">The Fate of the Furious</span>
<span class="ytps">2017</span>
</div>';
echo preg_replace('/<a href="\K[^"]+\//','http://newsite.com/?id=',$in);
Output:
<div id="slider1" class="owl-carousel owl-theme">
<div class="item">
<div class="imagens">
<img src="https://image.oldste.org" alt="The Fate of the Furious" width="100%" height="100%" />
<span class="imdb"><b class="icon-star"></b> N/A</span>
</div>
<span class="ttps">The Fate of the Furious</span>
<span class="ytps">2017</span>
</div>

How to automatically assign value to an attribute using XSLT in for-each loop?

In a for-each XSLT loop I construct an HTML structure of the form:
<div class="panel-group" id="accordion1" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading001">
...
</div>
</div>
</div>
Is it possible in that loop to generate the suffix of the attributes id and assign to it? So that in first loop the IDs become "accordion1" and "heading001", in the second loop become "accordion2" and "heading002", etc? And if so, can you provide an example?
in your for each loop, example for the id do something like below
<xsl:attribute name="id">
<xsl:value-of select="concat('accordion', position()" />
</xsl:attribute>

get_profile alternative in for loop django

I'm trying to get information of a users avatar based in an extended profile model. I usually call the information via get_profile(). However on this occasion the call is within a for loop in the template and I get errors if one of the users are the same.
How would I go about avoiding this error?
{% for fevent in fevents %}
<!-- EVENT ><! -->
<div class="event">
<div class="pic">
{{ fevent.getPublishedPredictionsCount }}
<img src="{% thumbnail fevent.artwork.get_absolute_url 100x98 crop,upscale %}" alt="" width="100" height="98" />
<div class="overlay">
</div>
</div>
<h1>{{ fevent.title|trunchar:30 }}</h1>
{% autoescape off %}
{{ fevent.getEventPredictionScore|makestars }}
{% endautoescape %}
<ul class="details">
<li class="cat">
Category: {{ fevent.catagory }}
</li>
<li class="location">
{{ fevent.location }}
</li>
<li class="date">
{{ fevent.date_and_time }}
</li>
<li class="time">
7:00pm - 8:00pm
</li>
</ul>
<!-- CLEAR ><! --><div class="clear"></div>
<div class="hype">
<div class="avatar">
<img src="{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}" alt="" width="120" height="120" />
</div>
<p>{{ fevent.description|trunchar:200 }}… Read More</p>
</div>
<!-- CLEAR ><! --><div class="clear"></div>
</div>
<!-- END OF EVENT ><! -->
{% endfor %}
The problem is here:
{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}
Error Message returned:
Caught MultipleObjectsReturned while rendering: get() returned more than one UserProfile -- it returned 2! Lookup parameters were {'user__id__exact': 4L}
To expand on what Matt said, while using get_or_create is a good idea, you should definitely define your profile model's User link with a OneToOneField, instead of a ForeignKey.
user = models.OneToOneField(User, verbose_name=_(u'user'))
Now, if you forget to use get_or_create(), or somehow accidentally try to create a duplicate profile for the same user, the database will raise an IntegrityError.
That error means there are two UserProfile objects in the database matching the query used by get_profile, not that get_profile was called twice. You need to remove one of those profile objects from the database and make sure there aren't multiples created again. You should be able to use the get_profile method multiple times without issue. Maybe you have a get_or_create call in that function without checking the proper values.