根据一组其他元素

时间:2015-09-02 16:06:54

标签: xslt xslt-1.0

我有一个像这样的XML结构:

<people>
  <person id="1" name="Sam" />
  <person id="2" name="Dan" />
</people>
<executives>
  <executive id="1" name="Sam" favorite="yes" />
  <executive id="3" name="Ian" />
</executives>

我需要引用中的所有节点,但我需要首先查看管理员中具有收藏夹属性的节点,然后是其他节点。我想我需要以某种方式合并<xsl:apply-templates select="/people/person[/executives/executive/@id = @id]" /><xsl:apply-templates select="/people/person[@favorite = 'yes']" />

但我不知道如何。

3 个答案:

答案 0 :(得分:1)

您可以通过使用密钥按 $("#drop-list-id").on('changed', function() { $.getJSON('/servlet/level=1').success(function(json) { ... }); });属性

查找管理人员来简化操作
name

然后,要获得高管中最喜欢的<xsl:key name="execs" match="executive" use="@name" /> 个节点,您可以使用这样的键

person

你会否定获得其他人的条件。

假设您有一个结构良好的XML(即带有根元素),请尝试使用此XSLT

<xsl:apply-templates select="person[key('execs', @name)/@favorite = 'yes']" />

答案 1 :(得分:1)

以下内容可行:

<!-- your root here, which you didn't show in the question -->
<xsl:template match="/root">
   <xsl:apply-templates select="people" />
</xsl:template>

<xsl:template match="people">
    <xsl:apply-templates select="person" mode="fav"/>
    <xsl:apply-templates select="person" mode="other"/>
</xsl:template>

<xsl:template match="person[@id = ../../executives/executive[@favorite = 'yes']/@id]" mode="fav">
    <favorite>
        <xsl:copy-of select="." />
    <favorite>
</xsl:template>

<!-- not a favorite -->
<xsl:template match="person" mode="fav" />

<!-- not a non-favorite -->
<xsl:template match="person" mode="other" />

<xsl:template match="person[not(@id = ../../executives/executive[@favorite = 'yes']/@id)]" mode="other">
    <non-favorite>
        <xsl:copy-of select="." />
    <non-favorite>
</xsl:template>

有更好的方法可以使用密钥来编写这个(这个解决方案同时由Tim提供),但我认为这就足够了,除非你有一些非常大的列表使得直接查找太慢。

我用于输出的结构应根据您的需要进行调整。我使用了额外的元素来确保你收集正确的元素。

答案 2 :(得分:1)

这是另一种看待它的方式:

XSLT 1.0

<root>
    <people>
      <person id="1" name="Adam" />
      <person id="2" name="Betty" />
      <person id="4" name="David" />
      <person id="5" name="Eve" />
      <person id="6" name="Frank" />
      <person id="7" name="George" />
    </people>
    <executives>
      <executive id="3" name="Cecil" favorite="yes" />
      <executive id="5" name="Eve" />
      <executive id="6" name="Frank" favorite="yes" />
    </executives>
</root>

测试输入

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <people>
      <person id="6" name="Frank"/>
      <person id="1" name="Adam"/>
      <person id="2" name="Betty"/>
      <person id="4" name="David"/>
      <person id="5" name="Eve"/>
      <person id="7" name="George"/>
   </people>
   <executives>
      <executive id="3" name="Cecil" favorite="yes"/>
      <executive id="5" name="Eve"/>
      <executive id="6" name="Frank" favorite="yes"/>
   </executives>
</root>

<强>结果

            <script>
                $(document).ready(function () {
                    var crudServiceBaseUrl = "//demos.telerik.com/kendo-ui/service",
                        dataSource = new kendo.data.DataSource({
                            transport: {
                                read: {
                                    url: crudServiceBaseUrl + "/Products",
                                    dataType: "jsonp"
                                },
                                update: {
                                    url: crudServiceBaseUrl + "/Products/Update",
                                    dataType: "jsonp"
                                },
                                destroy: {
                                    url: crudServiceBaseUrl + "/Products/Destroy",
                                    dataType: "jsonp"
                                },
                                create: {
                                    url: crudServiceBaseUrl + "/Products/Create",
                                    dataType: "jsonp"
                                },
                                parameterMap: function (options, operation) {
                                    if (operation !== "read" && options.models) {
                                        return { models: kendo.stringify(options.models) };
                                    }
                                }
                            },
                            batch: true,
                            pageSize: 20,
                            schema: {
                                model: {
                                    id: "ProductID",
                                    fields: {
                                        ProductID: { editable: false, nullable: true },
                                        ProductName: {
                                            validation: {
                                                required: true,
                                                productnamevalidation: function (input) {
                                                    if (input.is("[name='ProductName']") && input.val() != "") {
                                                        input.attr("data-productnamevalidation-msg", "Product Name should start with capital letter");
                                                        return /^[A-Z]/.test(input.val());
                                                    }

                                                    return true;
                                                }
                                            }
                                        },
                                        UnitPrice: { type: "number", validation: { required: true, min: 1} },
                                        Discontinued: { type: "boolean" },
                                        UnitsInStock: { type: "number", validation: { unitValidations :                                                                  function(input){
                                                                if ((input.val() <= 1000) && (input.val() >= 10)) {
                        input.attr("data-max-msg","value must in given range");
                        input.attr("data-min-msg","value must in given range");
                        return true;
                    } else {
                        return false;
                    } }, required: true} }
                                    }
                                }
                            }
                        });

                    $("#grid").kendoGrid({
                        dataSource: dataSource,
                        pageable: true,
                        height: 550,
                        toolbar: ["create"],
                        columns: [
                            "ProductName",
                            { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "120px" },
                            { field: "UnitsInStock", title: "Units In Stock", width: "120px" },
                            { field: "Discontinued", width: "120px" },
                            { command: ["edit", "destroy"], title: "&nbsp;", width: "250px"}],
                        editable: "inline"
                    });
                });
            </script>
        </div>