0

I am preety new to XSLT and thats my first Question in stackoverflow.

The Problem is that I have a System Generated XML like this one:

<?xml version="1.0" encoding="utf-16"?>
<Record contentId="1">
    <Field1>
        <ListValues>
            <ListValue id="84994" displayName="Infrastructure Platform:Database Management (ODP)">Database Management (ODP)</ListValue>
        </ListValues>
    </Field1>
        
    <Field2>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
        </ListValues>
    </Field2>
    <Field3>
        <Reference id="7">Name-Record</Reference>
        <Reference id="8">Name-Record</Reference>
    </Field3>
</Record>
<Record contentId="2">
    <Field1>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
        </ListValues>
    </Field1>
        
    <Field2>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
            <ListValue id="84993" displayName="Infrastructure Platform:Web Server (ODP)">Web Server (ODP)</ListValue>
            <ListValue id="84994" displayName="Infrastructure Platform:Database Management (ODP)">Database Management (ODP)</ListValue>
            <ListValue id="84998" displayName="Infrastructure Platform:Virtualization (ODP)">Virtualization (ODP)</ListValue>
            <ListValue id="84999" displayName="Infrastructure Platform:Storage Management (ODP)">Storage Management (ODP)</ListValue>
        </ListValues>
    </Field2>
    <Field3>
        <Reference id="3">Name-Record</Reference>
        <Reference id="4">Name-Record</Reference>
    </Field3>
</Record>
<Record contentId="3">
    <Field1>
        <ListValues>
            <ListValue id="84994" displayName="Infrastructure Platform:Database Management (ODP)">Database Management (ODP)</ListValue>
        </ListValues>
    </Field1>
        
    <Field2>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
            <ListValue id="84993" displayName="Infrastructure Platform:Web Server (ODP)">Web Server (ODP)</ListValue>
            <ListValue id="84994" displayName="Infrastructure Platform:Database Management (ODP)">Database Management (ODP)</ListValue>
        </ListValues>
    </Field2>
    <Field3>
        <Reference id="4">Name-Record</Reference>
        <Reference id="1">Name-Record</Reference>
    </Field3>
</Record>

Record 4
<Record contentId="4">
    <Field1>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
        </ListValues>
    </Field1>
        
    <Field2>
        <ListValues>
            <ListValue id="84992" displayName="Infrastructure Platform:Application Server (ODP)">Application Server (ODP)</ListValue>
            <ListValue id="84993" displayName="Infrastructure Platform:Web Server (ODP)">Web Server (ODP)</ListValue>
            <ListValue id="84994" displayName="Infrastructure Platform:Database Management (ODP)">Database Management (ODP)</ListValue>
        </ListValues>
    </Field2>
    <Field3>
        <Reference id="1">Name-Record</Reference>
    </Field3>
</Record>

What I want to achive is:

  1. It needs to find matches where my Field1 have a Match in the other records with Field2.
  2. Than I need to look if the Previous Match is also the same Record which is in Field3.

For Example: Record 1 have in Field1 "Database Management (ODP)" management, now he looks for matches, he will find Record 2,3 and 4. Now he needs too look if in Field3 is Record 1. Result is than Record 3 and Record 4.

To achive this I have build something like this(I know it's not accurate, this is only for example the real Stylesheet with the real paths and nodes is working):

    <?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <archer_records>
            <xsl:for-each select="Record">
                <xsl:variable name="ParentRecordId" select="@contentId"/>
                <!--Field1 Check -->
                <xsl:choose>
                    <xsl:when test="Field1/ListValues/ListValue">
                        <xsl:for-each select="Field1/ListValues/ListValue">
                            <xsl:variable name="ParentType" select="@displayName"/>
                            <xsl:for-each select="../../../../Record">
                                <xsl:choose>
                                    <xsl:when test="Field2/ListValues/ListValue">
                                        <xsl:for-each select="Field2/ListValues/ListValue">
                                        <!--Field3 Check -->
                                            <xsl:variable name="ChildType" select="@displayName"/>
                                            <xsl:if test="$ParentType=$ChildType">
                                                <xsl:variable name="ChildRecordID" select="../../../@contentId"/>
                                                <xsl:choose>
                                                    <xsl:when test="../../../Field3">
                                                        <xsl:for-each select="Field3[@id=$SPI_Tracking_Haupt]">
                                                            <record>
                                                                <ParentRecord>
                                                                    <xsl:value-of select="$ParentrecordID" />
                                                                </ParentRecord>
                                                                <ChildRecord>
                                                                    <xsl:value-of select="$ChildRecordID" />
                                                                </ChildRecord>
                                                            </record>
                                                        </xsl:for-each>
                                                    </xsl:when>
                                                </xsl:choose>
                                            </xsl:if>
                                        </xsl:for-each>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </archer_records>
    </xsl:template>
</xsl:stylesheet>

Thats working. I am getting a Result like this:

<record>
    <ParentRecord>1</ParentRecord>
    <ChildRecord>3</ChildRecord>
</record>
<record>
    <ParentRecord>1</ParentRecord>
    <ChildRecord>4</ChildRecord>
</record>
<record>
    <ParentRecord>2</ParentRecord>
    <ChildRecord>3</ChildRecord>
</record>
<record>
    <ParentRecord>4</ParentRecord>
    <ChildRecord>3</ChildRecord>
</record>

But I want something like this:

<record>
    <ParentRecord>1</ParentRecord>
    <ChildRecord>3</ChildRecord>
    <ChildRecord>4</ChildRecord>
</record>
<record>
    <ParentRecord>2</ParentRecord>
    <ChildRecord>3</ChildRecord>
</record>
<record>
    <ParentRecord>4</ParentRecord>
    <ChildRecord>3</ChildRecord>
</record>

I hope someone could Understand what my Problem is.

Thank you!

ager24
  • 1
  • Does this answer your question? [How to convert array to SimpleXML](https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml) –  Aug 25 '20 at 09:33
  • Where is the root element of your XML sample? You can't have more than one `Record` element at the top level. If your real stylesheet gives you the right output and you want to group the result then consider chaining a second step or stylesheet that performs the grouping. – Martin Honnen Aug 25 '20 at 10:17
  • @wamar No. Thats PHP. – ager24 Aug 25 '20 at 11:27
  • @MartinHonnen forgot the Root Element, it's "Records". The Stylesheet gives me the right result. Yes but the problem is I can only run one Stylesheet. What do you mean with chaining a second step ? – ager24 Aug 25 '20 at 11:30
  • Basically you would store the first step in a variable e.g. `...`, then you could apply grouping (e.g. Muenchian grouping in XSLT 1 or `for-each-group` in XSLT 2/3) to that variable, keeping in mind that for XSLT 1 you would need to apply `exsl:node-set($records)` or a similar, processor dependent extension function to the variable to use it further. – Martin Honnen Aug 25 '20 at 11:37
  • @MartinHonnen Thanks for your Answer. I tried some things but I am not getting it right. I can use XSLT 3.0 but not getting any result with "for-each-group". Would it help if I post the orginal Stylesheet and the original XML Sample ? – ager24 Aug 25 '20 at 13:16

0 Answers0