Showing posts with label Filter List using XSLT. Show all posts
Showing posts with label Filter List using XSLT. Show all posts

Wednesday, 30 August 2017

Filter List using multiple sub/child elements in XSLT

<xsl:for-each select="//DocumentList/DocumentDetails[./DocumentSource=$DocumentSource or $DocumentSource= ''][./Category = $Category or $Category = '']">


I have Request something like below:

<DocumentList>
  <DocumentDetails>
    <DocumentID>Document1</DocumentID>
    <DocumentSource>DocumentSource1</DocumentSource>
    <Category>Category1</Categoty>
  </DocumentDetails>
<DocumentDetails>
    <DocumentID>Document2</DocumentID>
    <DocumentSource>DocumentSource2</DocumentSource>
    <Category>Category2</Categoty>
  </DocumentDetails>
  <DocumentDetails>
    <DocumentID>Document3</DocumentID>
    <DocumentSource>DocumentSource2</DocumentSource>
    <Category>Category2</Categoty>
  </DocumentDetails>
</DocumentList>


Expected Output:


The DocumentList has to be filtered out using DocumentSource and Category.

For Ex: DocumentSource=DocumentSource2 and Category=Category2
Result:

<DocumentList>
  <DocumentDetails>
    <DocumentID>Document2</DocumentID>
    <DocumentSource>DocumentSource2</DocumentSource>
    <Category>Category2</Categoty>
  </DocumentDetails>
  <DocumentDetails>
    <DocumentID>Document3</DocumentID>
    <DocumentSource>DocumentSource2</DocumentSource>
    <Category>Category2</Categoty>
  </DocumentDetails>
</DocumentList>

Solution:



<xsl:for-each select="/ns0:DocumentList/ns0:DocDetails[./ns0:DocumentSource=$DocumentSource or $DocumentSource=''][./ns0:Category=$Category or $Category=''][./ns0:SubCategory=$SubCategory or $SubCategory='']">

$DocumentSource and $Category are variable holding the data "DocumentSource2" and "Category2" respectively.