XSLT: Replacing div with script on multiple levels -


i trying replace div-tags script-tags within level. code within div set display none need (discard 2 outer divs), , divs within code has class of "is-script" must replaced script, while keeping attributes , nodes. examples of how code can look:

 <div data-type="embed">     <div style="display:none;">         <div class="is-script" src="http://example.js"></div>     </div> </div>  <div data-type="embed">     <div style="display:none;">         <div class="is-script" src="http://example1.js"></div>         <div class="is-script" src="http://example2.js"></div>     </div> </div>  <div data-type="embed">     <div style="display:none;">         <div>             <div class="is-script" src="http://example.js"></div>         </div>     </div> </div>  <div data-type="embed">     <div style="display:none;">         <div>             <div class="is-script" src="http://example1.js"></div>             <div class="is-script" src="http://example2.js"></div>         </div>     </div> </div>  <div data-type="embed">     <div style="display:none;">         <div>             <div class="is-script" src="http://example1.js"></div>             <div class="is-script">some inline javascript</div>         </div>     </div> </div>  <div data-type="embed">     <div style="display:none;">         <iframe src="http://example.html" width="100%" height="400px"></iframe>     </div> </div> 

the actual json xsl parsing:

<div data-type=\"embed\" style=\"width:545px;height:200px;background-color:#ccc;\">html embed     <div style=\"display:none;\">         <div class=\"is-script\">console.log(\"test1\");</div>     </div> </div>  <div data-type=\"embed\" style=\"width:545px;height:200px;background-color:#ccc;\">html embed     <div style=\"display:none;\">         <div>             <div class=\"is-script\">console.log(\"test2\");</div>         </div>     </div> </div>  <div data-type=\"embed\" style=\"width:545px;height:200px;background-color:#ccc;\">html embed     <div style=\"display:none;\">         <div class=\"is-script\" src=\"example.js\"></div>     </div> </div>  <div data-type=\"embed\" style=\"width:545px;height:200px;background-color:#ccc;\">html embed     <div style=\"display:none;\">         <div>             <div class=\"is-script\" src=\"example.js\"></div>         </div>     </div> </div>  <div data-type=\"embed\" style=\"width:545px;height:200px;background-color:#ccc;\">html embed     <div style=\"display:none;\">         <div>             <div class=\"is-script\" src=\"example1.js\"></div>             <div class=\"is-script\" src=\"example2.js\"></div>             <div class=\"is-script\" src=\"example3.js\"></div>         </div>     </div> </div> 

wanted result:

<script class="is-script" src="http://example.js"></script>   <script class="is-script" src="http://example1.js"></script> <script class="is-script" src="http://example2.js"></script>   <div>     <script class="is-script" src="http://example.js"></script> </div>   <div>     <script class="is-script" src="http://example1.js"></script>     <script class="is-script" src="http://example2.js"></script> </div>   <div>     <script class="is-script" src="http://example1.js"></script>     <script class="is-script">some inline javascript</script> </div>   <iframe src="http://example.html" width="100%" height="400px"></iframe> 

current result:

<script class="was-script-tag">console.log("test1");</script>  <div>     <div class="was-script-tag">console.log("test2");</div> </div>  <script class="was-script-tag" src="/templates/v1/js/sticky_article_v7.js?v0"></script>  <div>     <div class="was-script-tag" src="/templates/v1/js/sticky_article_v7.js?v1"></div> </div>  <div>     <div class="was-script-tag" src="/templates/v1/js/sticky_article_v7.js?v2"></div>     <div class="was-script-tag" src="/templates/v1/js/sticky_article_v7.js?v2"></div>     <div class="was-script-tag" src="/templates/v1/js/sticky_article_v7.js?v2"></div> </div> 

my xsl:

<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0">      <xsl:output method="html" indent="yes"/>       <xsl:template match="@*|node()">       <xsl:copy>         <xsl:apply-templates select="@*|node()"/>       </xsl:copy>     </xsl:template>      <xsl:template match="div[@data-type='embed' or parent::div[@data-type='embed']]">         <xsl:apply-templates />     </xsl:template>      <xsl:template match="div[@class='is-script']">         <script>             <xsl:apply-templates select="@*|node()"/>         </script>     </xsl:template>  </xsl:stylesheet>  

so remove 2 outer divs, , keep everyting inside is, except divs class of "is-script" must renamed script. 2 outer divs same.

i have basic understanding of xsl, can't make work. using xslt 2.

you possibly start off identity template, copy existing nodes without change

<xsl:template match="@*|node()">   <xsl:copy>     <xsl:apply-templates select="@*|node()"/>   </xsl:copy> </xsl:template> 

then, ignore "outer 2 divs" can add overriding template so:

<xsl:template match="div[@data-type='embed' or parent::div[@data-type='embed']]">   <xsl:apply-templates /> </xsl:template> 

this ignores div elements data-type of embed, or immediate child div.

then change "is-script" divs script elements can add template

<xsl:template match="div[@class='is-script']">   <script>     <xsl:apply-templates select="@*|node()"/>   </script> </xsl:template> 

try xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0">   <xsl:template match="@*|node()">     <xsl:copy>       <xsl:apply-templates select="@*|node()"/>     </xsl:copy>   </xsl:template>    <xsl:template match="div[@data-type='embed' or parent::div[@data-type='embed']]">     <xsl:apply-templates />   </xsl:template>    <xsl:template match="div[@class='is-script']">     <script>       <xsl:apply-templates select="@*|node()"/>     </script>   </xsl:template> </xsl:stylesheet> 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -