开发者

Need to close a node only on a particular condition

开发者 https://www.devze.com 2023-01-08 10:38 出处:网络
Please find the 2 XML\'s Input XML- <feed xmlns:es=\"http://ucmservice\"> <element> <es:RepositoryDetails>

Please find the 2 XML's

Input XML-

<feed xmlns:es="http://ucmservice">
 <element>
<es:RepositoryDetails>
    <es:Repository Type="DatabaseRepository" />
        <es:ConnetionDetails>
        <es:Param MigrationSetId="439" />
    </es:ConnetionDetails>
</es:RepositoryDetails>
<es:DocProperties>
    <es:UniqueDocId Value="_1_15">
            <es:DocProperty propertyName="EmployeeName">
                <es:PropValues>
                  <es:PropValue>Index</es:PropValue>
                </es:PropValues>
            </es:DocProperty>
            <es:DocProperty propertyName="EmployeeAddress">" +
                      <es:PropValues>
                        <es:PropValue>Gurgaon</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                      <es:PropValues>
                        <es:PropValue>15000</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
        </es:UniqueDocId>
    </es:DocProperties>
</element>

<element>
<es:RepositoryDetails>
    <es:Repository Type="DatabaseRepository" />
        <es:ConnetionDetails>
        <es:Param MigrationSetId="439" />
    </es:ConnetionDetails>
</es:RepositoryDetails>
<es:DocProperties>
    <es:UniqueDocId Value="_1_15">
            <es:DocProperty propertyName="EmployeeName">
                <es:PropValues>
            开发者_C百科      <es:PropValue>Index1</es:PropValue>
                </es:PropValues>
            </es:DocProperty>
            <es:DocProperty propertyName="EmployeeAddress">" +
                      <es:PropValues>
                        <es:PropValue>Delhi</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                      <es:PropValues>
                        <es:PropValue>25000</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
        </es:UniqueDocId>
    </es:DocProperties>
</element>

<element>
<es:RepositoryDetails>
    <es:Repository Type="Trim" />
        <es:ConnetionDetails>
        <es:Param MigrationSetId="439" />
    </es:ConnetionDetails>
</es:RepositoryDetails>
<es:DocProperties>
    <es:UniqueDocId Value="_1_15">
            <es:DocProperty propertyName="EmployeeName">
                <es:PropValues>
                  <es:PropValue>Quality</es:PropValue>
                </es:PropValues>
            </es:DocProperty>
            <es:DocProperty propertyName="EmployeeAddress">" +
                      <es:PropValues>
                        <es:PropValue>Mumbai</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                      <es:PropValues>
                        <es:PropValue>20000</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
        </es:UniqueDocId>
    </es:DocProperties>
</element>

<element>
<es:RepositoryDetails>
    <es:Repository Type="DatabaseRepository" />
        <es:ConnetionDetails>
        <es:Param MigrationSetId="439" />
    </es:ConnetionDetails>
</es:RepositoryDetails>
<es:DocProperties>
    <es:UniqueDocId Value="_1_15">
            <es:DocProperty propertyName="EmployeeName">
                <es:PropValues>
                  <es:PropValue>Index</es:PropValue>
                </es:PropValues>
            </es:DocProperty>
            <es:DocProperty propertyName="EmployeeAddress">" +
                      <es:PropValues>
                        <es:PropValue>Gurgaon</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                      <es:PropValues>
                        <es:PropValue>21000</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
        </es:UniqueDocId>
    </es:DocProperties>
</element>


<element>
<es:RepositoryDetails>
    <es:Repository Type="Trim" />
        <es:ConnetionDetails>
        <es:Param MigrationSetId="439" />
    </es:ConnetionDetails>
</es:RepositoryDetails>
<es:DocProperties>
    <es:UniqueDocId Value="_1_15">
            <es:DocProperty propertyName="EmployeeName">
                <es:PropValues>
                  <es:PropValue>Index1</es:PropValue>
                </es:PropValues>
            </es:DocProperty>
            <es:DocProperty propertyName="EmployeeAddress">" +
                      <es:PropValues>
                        <es:PropValue>Gurgaon</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                      <es:PropValues>
                        <es:PropValue>15000</es:PropValue>
                      </es:PropValues>
                </es:DocProperty>
        </es:UniqueDocId>
    </es:DocProperties>
</element></feed>

Required Output XML-

<tree>
<item level="0" name="DatabaseRepository">
    <item level="1" name="Index">
        <item level="2" name="Gurgaon"/>
        <item level="2" name="Mumbai"/>
    </item>
    <item level="1" name="Index1">
        <item level="2" name="Delhi"/>
    </item>
</item>
<item level="0" name="Trim">
    <item level="1" name="Quality">
        <item level="0" name="Mumbai"/>
    <item level="1" name="Index1">
        <item level="0" name="Gurgaon"/>
    </item>
</item>
</tree>

I don't know how to implement it. I tried few things in XSL and Javascript but they are not enough.


XSLT is about tree, not tags. If your output method is not text you can't produce a non-wellformed tree. And that's a feature!

I'm sure that your desired output can be produce with proper XSLT. You need to post an input sample and desired output.

EDIT: Also, in order to help you, you must to revert this sentence:

"i want to close a node only if a poarticular condition is true"

For: I want to add some template if a particular condition is true

EDIT 2: Now with input and output, it's look like you want to group by two keys. So, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:es="http://ucmservice" exclude-result-prefixes="es">
    <xsl:key name="ByType" match="element" use="es:RepositoryDetails/es:Repository/@Type"/>
    <xsl:key name="ByType-Name" match="es:UniqueDocId" use="concat(../../es:RepositoryDetails/es:Repository/@Type,'++',es:DocProperty[@propertyName='EmployeeName']/es:PropValues/es:PropValue)"/>
    <xsl:template match="text()"/>
    <xsl:template match="feed">
        <tree>
            <xsl:apply-templates/>
        </tree>
    </xsl:template>
    <xsl:template match="element"/>
    <xsl:template match="element[count(.|key('ByType',es:RepositoryDetails/es:Repository/@Type)[1])=1]">
        <item level="0" name="{es:RepositoryDetails/es:Repository/@Type}">
            <xsl:apply-templates select="key('ByType',es:RepositoryDetails/es:Repository/@Type)/*"/>
        </item>
    </xsl:template>
    <xsl:template match="es:UniqueDocId"/>
    <xsl:template match="es:UniqueDocId[count(.|key('ByType-Name',concat(../../es:RepositoryDetails/es:Repository/@Type,'++',es:DocProperty[@propertyName='EmployeeName']/es:PropValues/es:PropValue))[1])=1]">
        <item level="1" name="{es:DocProperty[@propertyName='EmployeeName']/es:PropValues/es:PropValue}">
            <xsl:apply-templates select="key('ByType-Name',concat(../../es:RepositoryDetails/es:Repository/@Type,'++',es:DocProperty[@propertyName='EmployeeName']/es:PropValues/es:PropValue))/*"/>
        </item>
    </xsl:template>
    <xsl:template match="es:DocProperty[@propertyName='EmployeeAddress']/es:PropValues/es:PropValue">
        <item level="2" name="{.}" />
    </xsl:template>
</xsl:stylesheet>

Output:

<tree>
    <item level="0" name="DatabaseRepository">
        <item level="1" name="Index">
            <item level="2" name="Gurgaon" />
            <item level="2" name="Gurgaon" />
        </item>
        <item level="1" name="Index1">
            <item level="2" name="Delhi" />
        </item>
    </item>
    <item level="0" name="Trim">
        <item level="1" name="Quality">
            <item level="2" name="Mumbai" />
        </item>
        <item level="1" name="Index1">
            <item level="2" name="Gurgaon" />
        </item>
    </item>
</tree>

Note: Your desired output has some mistakes (there is no DatabaseRepository-Index-Mumbai) so I can't tell if you really want item[@name='Trim'] grandchilds with @leve 2 or 0 as it was written.

0

精彩评论

暂无评论...
验证码 换一张
取 消