简体   繁体   中英

xslt-1.0 for-each loop fetching all subelements

I am new to xsl. I am struggling with for-each loop. I have below input.xml and need expected output.xml. But I am getting all the subsequent results in .

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 that I want is:

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

You can see, I want to divide this into 2 groups based on _FOLDER_1 & _FOLDER_2. When I iterate using below xsl code I get all the results and those are not differentiated based on _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>

I can get the desired result if I loop over using 2 for loops outer loop for FOLDER & inner for subelements. But for some reasons I cannot use that approach as I need this data in another outer loop. Please help. Thanks in advance.

You just need to split your xsl:for-each into two and nest them. The outer one will select the main FOLDER-HIERARCHY , and you then create a Folder for each. Then, within that, you can then select the SPEC-OBJECT-REF

Try this template:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM