首页 \ 问答 \ 如何使用XSLT基于兄弟姐妹对元素进行分组(How to group elements based on their siblings using XSLT)

如何使用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
更新时间:2024-05-04 21:05

最满意答案

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 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);

DEMO

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, ...
  • 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 ...
  • 代码与您的问题无关,我没有在这里看到任何文件。 如果你只想显示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))
  • 如果你想在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警报的来源吗? 因为我怀疑这是自动完成,或者至少不是直接的。 如果在您使用自动填充功能时发生这种情况,我会检查您在实时系统中搜索的搜索结果,看看它们是否包含一些黑客放入的Javascript,然后 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。