1

I am beginner in XSLT. My Source XML:

<Request>
   <Documents>
      <Doc Type="A">123</Doc>
      <Doc Type="C">345</Doc>
   </Documents>
</Request>

My response XML:

<Response>
   <RequestedDoc>
       <Doc Type="A">123</Doc>
       <Doc Type="C">345</Doc>
   </RequestedDoc>
   <Result>
      <Document>
         <Id>A123</Id>
         <Name>Doc1</Name>
      </Document>
      <Document>
         <Id>E143</Id>
         <Name>Doc2</Name>
      </Document>
      <Document>
         <Id>C345</Id>
         <Name>Doc3</Name>
      </Document>
      <Document>
         <Id>D153</Id>
         <Name>Doc4</Name>
      </Document>
   </Result>
</Response>

i need to filter the Result tag using the RequestedDoc tag. i have tried below XSLT but it returns only the 1st result.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:variable name="requestedDoc" select="//RequestedDoc/Doc"/>

   <xsl:template match="Response">
      <xsl:element name="FilterResult">
          <xsl:apply-templates select="//Result/Document[contains(Id,$requestedDoc)]"/>
      </xsl:element>
   </xsl:template>

   <xsl:template match="Document">
      <xsl:element name="Name">
         <xsl:value-of select="Name"/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

but this XSLT always return data for first Doc A123.

<FilterResult>
  <Name>Doc1</Name>
</FilterResult>

Expected result is

<FilterResult>
  <Name>Doc1</Name>
  <Name>Doc3</Name>
</FilterResult>

Please help.

2
  • Please. edit the question and specify the exact wanted result from the transformation -- so that we wouldn't need to guess. Commented Nov 9, 2012 at 12:54
  • Ankur Raiyani, I posted an answer. Commented Nov 9, 2012 at 13:38

1 Answer 1

1

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <FilterResult>
      <xsl:apply-templates/>
     </FilterResult>
 </xsl:template>

 <xsl:template match="Document[substring(Id,2) = /*/RequestedDoc/*]">
  <xsl:copy-of select="Name"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided Response document:

<Response>
   <RequestedDoc>
       <Doc Type="A">123</Doc>
       <Doc Type="C">345</Doc>
   </RequestedDoc>
   <Result>
      <Document>
         <Id>A123</Id>
         <Name>Doc1</Name>
      </Document>
      <Document>
         <Id>E143</Id>
         <Name>Doc2</Name>
      </Document>
      <Document>
         <Id>C345</Id>
         <Name>Doc3</Name>
      </Document>
      <Document>
         <Id>D153</Id>
         <Name>Doc4</Name>
      </Document>
   </Result>
</Response>

produces the wanted, correct result:

<FilterResult>
   <Name>Doc1</Name>
   <Name>Doc3</Name>
</FilterResult>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.