xslt-1.0 for-each循环获取所有子元素

时间:2018-11-05 17:08:16

标签: xml xslt xml-parsing xslt-1.0

我是xsl的新手。我在为每个循环而苦苦挣扎。我在input.xml下面,并且需要预期的output.xml。但我在中得到了所有后续结果。

input.xml

<?xml version="1.0" encoding="utf-8" ?>
<FOLDER-HIERARCHY>
                <OBJECT>
                    <FOLDER-REF>_f9a2c991-0b91-40cb-a58c-ff1bdbd12d5b</FOLDER-REF>
                </OBJECT>
                <CHILDREN>
                            <FOLDER-HIERARCHY>
                                <OBJECT>
                                    <FOLDER-REF>_ac767e78-1221-475f-a9f7-e31873fcaecc</FOLDER-REF>
                                </OBJECT>
                                <CHILDREN>
                                    <OBJECT>
                                        <SPEC-OBJECT-REF>_42d23e8b-3b0b-4753-93c3-61651a8a361b</SPEC-OBJECT-REF>
                                    </OBJECT>
                                    <FOLDER-HIERARCHY>
                                        <OBJECT>
                                            <FOLDER-REF>_FOLDER_1</FOLDER-REF>
                                        </OBJECT>
                                        <CHILDREN>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_b7341e02-8bc4-4e7d-8099-6c5791c6008a</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_0762a8b6-05ec-4c1e-a0f8-dd5801daa647</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_55973a3d-a08e-40e1-bfa9-c709e3d68c71</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_25346101-eecb-4966-b915-070d96721a1e</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_6b4fbabe-85e6-489c-9e01-99a4db9cdbb7</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_206c7a4b-e5d0-440d-8b88-21cb895fef82</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_1082798f-3850-482d-a976-5824d962d7fc</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_6e8ba622-8e53-458f-9ea6-735d866ecb6c</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_9db12317-37ff-48c2-92c6-ade9fb18ab66</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_8736d564-1577-4ded-8dea-a36fd2807429</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_161bd8c7-f87a-49e9-bf95-fded2da04ccf</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_5ae77274-b35b-4265-ad57-d65779e7dac6</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_af78d1b5-2ff9-4076-baf3-75e4e5de7360</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_60cb1be5-eaa5-403b-911b-1e39d8b65d68</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_6f489ff2-fd20-48da-aeb2-e67d4f8d9797</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_f239c20b-a7f7-4645-bc77-c4b27b11108e</SPEC-OBJECT-REF>
                                            </OBJECT>
                                        </CHILDREN>
                                    </FOLDER-HIERARCHY>
                                    <FOLDER-HIERARCHY>
                                        <OBJECT>
                                            <FOLDER-REF>_FOLDER_2</FOLDER-REF>
                                        </OBJECT>
                                        <CHILDREN>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_10839c74-faf2-4add-96b1-65db4f90a851</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_00bdbcff-8fba-4811-9d77-24461c0bd447</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_e0500607-393d-4e9b-8e11-8864f81fbeb2</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_77d7b866-68bf-41b9-b30e-d6e74db2a940</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_52a278ad-8088-4caa-bb43-09f90393063c</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_48792dd6-80e4-451b-9c0a-45d56beb1270</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_4cfb2c9b-43cc-4dea-85d4-19f50adfbf54</SPEC-OBJECT-REF>
                                            </OBJECT>
                                            <OBJECT>
                                                <SPEC-OBJECT-REF>_1eb413b1-68ce-4e73-8571-046f63e6e586</SPEC-OBJECT-REF>
                                            </OBJECT>
                                        </CHILDREN>
                                    </FOLDER-HIERARCHY>
                                </CHILDREN>
                            </FOLDER-HIERARCHY>
                        </CHILDREN>
            </FOLDER-HIERARCHY> 
我想要的

output.xml是:

<Folder>
   <ul>_b7341e02-8bc4-4e7d-8099-6c5791c6008a</ul>
   <ul>_0762a8b6-05ec-4c1e-a0f8-dd5801daa647</ul>
   <ul>_55973a3d-a08e-40e1-bfa9-c709e3d68c71</ul>
   <ul>_25346101-eecb-4966-b915-070d96721a1e</ul>
   <ul>_6b4fbabe-85e6-489c-9e01-99a4db9cdbb7</ul>
   <ul>_206c7a4b-e5d0-440d-8b88-21cb895fef82</ul>
   <ul>_1082798f-3850-482d-a976-5824d962d7fc</ul>
   <ul>_6e8ba622-8e53-458f-9ea6-735d866ecb6c</ul>
   <ul>_9db12317-37ff-48c2-92c6-ade9fb18ab66</ul>
   <ul>_8736d564-1577-4ded-8dea-a36fd2807429</ul>
   <ul>_161bd8c7-f87a-49e9-bf95-fded2da04ccf</ul>
   <ul>_5ae77274-b35b-4265-ad57-d65779e7dac6</ul>
   <ul>_af78d1b5-2ff9-4076-baf3-75e4e5de7360</ul>
   <ul>_60cb1be5-eaa5-403b-911b-1e39d8b65d68</ul>
   <ul>_6f489ff2-fd20-48da-aeb2-e67d4f8d9797</ul>
   <ul>_f239c20b-a7f7-4645-bc77-c4b27b11108e</ul>
</Folder>
<Folder>
<ul>_10839c74-faf2-4add-96b1-65db4f90a851</ul>
   <ul>_00bdbcff-8fba-4811-9d77-24461c0bd447</ul>
   <ul>_e0500607-393d-4e9b-8e11-8864f81fbeb2</ul>
   <ul>_77d7b866-68bf-41b9-b30e-d6e74db2a940</ul>
   <ul>_52a278ad-8088-4caa-bb43-09f90393063c</ul>
   <ul>_48792dd6-80e4-451b-9c0a-45d56beb1270</ul>
   <ul>_4cfb2c9b-43cc-4dea-85d4-19f50adfbf54</ul>
   <ul>_1eb413b1-68ce-4e73-8571-046f63e6e586</ul>
</Folder>

您可以看到,我想根据_FOLDER_1和_FOLDER_2将其分为两组。 当我使用下面的xsl代码进行迭代时,会得到所有结果,并且不会根据_FOLDER_1和_FOLDER_2进行区分。

xsl:

<xsl:template match="/">

      <Folder>
    <xsl:for-each select="FOLDER-HIERARCHY/CHILDREN/FOLDER-HIERARCHY/CHILDREN/FOLDER-HIERARCHY/CHILDREN/OBJECT/SPEC-OBJECT-REF">
        <xsl:variable name="specObjectRef" select="."/>

        <ul><xsl:value-of select="$specObjectRef"/></ul>

    </xsl:for-each>
    </Folder>

  </xsl:template>

如果我使用2 for循环,FOLDER的外部循环和子元素的inner循环,可以得到理想的结果。但是由于某些原因,我无法使用该方法,因为我需要在另一个外部循环中使用此数据。 请帮忙。提前致谢。

1 个答案:

答案 0 :(得分:1)

您只需要将xsl:for-each分成两部分并嵌套即可。外部的将选择主FOLDER-HIERARCHY,然后为每个Folder创建一个SPEC-OBJECT-REF。然后,在其中,您可以选择<xsl:template match="/"> <xsl:for-each select="FOLDER-HIERARCHY/CHILDREN/FOLDER-HIERARCHY/CHILDREN/FOLDER-HIERARCHY"> <Folder> <xsl:for-each select="CHILDREN/OBJECT/SPEC-OBJECT-REF"> <ul><xsl:value-of select="."/></ul> </xsl:for-each> </Folder> </xsl:for-each> </xsl:template>

尝试此模板:

public List<String> findUrls(String url) {

    List<String> urls = null;
    InputStream response = null;
    try {
        response = new URL(url).openStream();

        System.err.println("opened stream");

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(response);
        String responseBody = scanner.useDelimiter("\\A").next();

        urls = getUrls(responseBody);

    } catch (IOException ex) {
        ex.printStackTrace();
        System.err.println("Error finding articles");
    } finally {
        try {
            response.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    return urls;
}

@Scheduled(fixedDelay = 86400000)
public void autoUpload() {

    System.out.println("Method executed at every 24 hours. Current time is :: " + new Date());

    List<String> urls = findUrls(
            "https://www.google.com/search?q=site:foxnews.com&tbas=0&tbs=qdr:d&tbm=nws&tbas=0&source=lnt&sa=X&ved=0ahUKEwiY6On-1L3eAhUBQhoKHb2rBfIQpwUIHw&biw=1540&bih=832&dpr=1.09");
    createArticles(urls);
}