如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)
我试图根据周围兄弟姐妹的起始和结束属性对几个元素进行分组。
示例XML:
<list> <item>One</item> <item class="start">Two</item> <item>Three</item> <item class="end">Four</item> <item>Five</item> <item class="start">Six</item> <item class="end">Seven</item> <item>Eight</item> </list>
期望的结果:
<body> <p>One</p> <div> <p>Two</p> <p>Three</p> <p>Four</p> </div> <p>Five</p> <div> <p>Six</p> <p>Seven</p> </div> <p>Eight</p> </body>
我通过以下XSLT接近了期望的结果。 但是,以下兄弟匹配在到达结束属性后不会停止。 此外,标准匹配重复已经从以下兄弟匹配输出的元素。
<?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*" /> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="list"> <body> <xsl:apply-templates /> </body> </xsl:template> <xsl:template match="item"> <p> <xsl:apply-templates /> </p> </xsl:template> <xsl:template match="item[@class='start']"> <div> <p><xsl:apply-templates /></p> <xsl:apply-templates select="following-sibling::*[not(preceding-sibling::*[1][@class='end'])]" /> </div> </xsl:template> </xsl:transform>
I am trying to group several elements based on a starting and ending attribute of their surrounding siblings.
Sample XML:
<list> <item>One</item> <item class="start">Two</item> <item>Three</item> <item class="end">Four</item> <item>Five</item> <item class="start">Six</item> <item class="end">Seven</item> <item>Eight</item> </list>
Desired Result:
<body> <p>One</p> <div> <p>Two</p> <p>Three</p> <p>Four</p> </div> <p>Five</p> <div> <p>Six</p> <p>Seven</p> </div> <p>Eight</p> </body>
I come close to the desired results with the following XSLT. However, the following-sibling match doesn't stop after it reaches the ending attribute. Also, the standard matching repeats the elements that were already output from the following-sibling match.
<?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> <xsl:strip-space elements="*" /> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="list"> <body> <xsl:apply-templates /> </body> </xsl:template> <xsl:template match="item"> <p> <xsl:apply-templates /> </p> </xsl:template> <xsl:template match="item[@class='start']"> <div> <p><xsl:apply-templates /></p> <xsl:apply-templates select="following-sibling::*[not(preceding-sibling::*[1][@class='end'])]" /> </div> </xsl:template> </xsl:transform>
原文:https://stackoverflow.com/questions/38191389
最满意答案
data = [{"message_id":"3","box":"0","from_id":"3","to_id":"1","title":"Hello sir!","message":"how are you?","sender_ip":"","date_sent":"","status":"0"}] $('#divid').html(data[0].message);
您可能必须使用
jQuery.parseJSON
解析JSON字符串。// results is your JSON string from the request data = jQuery.parseJSON(results); $('#divid').html(data[0].message);
data = [{"message_id":"3","box":"0","from_id":"3","to_id":"1","title":"Hello sir!","message":"how are you?","sender_ip":"","date_sent":"","status":"0"}] $('#divid').html(data[0].message);
You might have to parse a JSON string using
jQuery.parseJSON
.// results is your JSON string from the request data = jQuery.parseJSON(results); $('#divid').html(data[0].message);
相关问答
更多-
将while循环更改为... $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows[] = $r; } echo json_encode($rows); 并且您的JavaScript应该始终使用$.each()迭代data 。 Change your while loop to... $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows[] ...
-
要计算组中的项目,请使用Count()方法。 var query = Enumerable.From(data) .GroupBy("$.Name", null, "{ Name: $, Count: $$.Count() }") .ToArray(); To count the items in the group, use the Count() method. var query = Enumerable.From(data) .GroupBy("$.Name", null, ...
-
如何提醒我的json结果?(How do I alert my json results?)[2023-02-20]
data = [{"message_id":"3","box":"0","from_id":"3","to_id":"1","title":"Hello sir!","message":"how are you?","sender_ip":"","date_sent":"","status":"0"}] $('#divid').html(data[0].message); DEMO 您可能必须使用jQuery.parseJSON解析JSON字符串。 // results is your JSON s ... -
result不是数组。 result.key是一个数组。 您的字段属性也是message ,而不是MESSAGE : $.each(result.key, function(i, field){ alert(field.message); }); result isn't an array. result.key is an array. Also your field's property is message, not MESSAGE: $.each(result.key, function( ...
-
{something:1} 但是,不是一个有效的JSON字符串 {"something":1} 是。 如果你用你的电话代替 $.ajax({ url: 'http://localhost/call', dataType: 'json', success: function(){}, error: function(xhr, textStatus, errorThrown){ //you should get a parse error and end u ...
-
如何使用JSON过滤结果(how to filter results with JSON)[2023-06-02]
代码与您的问题无关,我没有在这里看到任何文件。 如果你只想显示kIID ==客户端,只要条件如下 if(json[i].klID === 'What ever name'){ var dateString = json[i].datum.substr(6); var currentTime = new Date(parseInt(dateString)); var month = currentTime.getMonth() ... -
您正在尝试比较两个不同的对象。 尝试将它们转换为字符串并比较: if(JSON.stringify(tempData)==JSON.stringify(tempData2)) You are trying to compare two different objects. Try convert them to string and compare : if(JSON.stringify(tempData)==JSON.stringify(tempData2))
-
提醒空取回(Alert on empty fetch)[2023-02-14]
如果你想在return response.json();取消执行return response.json(); 你可以做到以下几点 handleSubmit(event){ const url = 'http://localhost:8000/issue/'; var issueid = document.getElementById("SearchTxt").value; if(issueid == 0 || issueid == "" || issueid == undefine ... -
该对象位于解析响应的结果Array的索引0处。 您可以使用括号表示法来引用JavaScript对象的索引0处的元素 let response =`[{"ID":"25","Serial":"1","Purchase_id":"8","Item":"23","Unit":"1","HSN":"84212120","Quantity":"10","Purchase_rate":"100","Discount":"10","Discount_2":"5","Net_rate":"85.5","CGST_Perce ...
-
当我使用jqueryui自动完成时,为什么我的网站会提醒“xssvuln”?(Why does my site alert “xssvuln” when I use jqueryui autocomplete?)[2021-12-15]
我猜这与jQueryUI自动完成没什么关系,但可能表明某人已经攻击了你的网站,尽管是以友好警告的方式。 这可能暗示某人设法在您的数据库中获取某些内容,例如 ,表明您的站点容易受到跨站点脚本注入的攻击。 您可以查看实时页面的源代码,看看xssvuln警报的来源吗? 因为我怀疑这是自动完成,或者至少不是直接的。 如果在您使用自动填充功能时发生这种情况,我会检查您在实时系统中搜索的搜索结果,看看它们是否包含一些黑客放入的Javascript,然后 ...