xml - Selecting attributes of nodes using for-each in XSLT -


i have requirement need prepare xml list of questions , answer. 1 eform can have multiple documents. each document can have multiple questions.

i have prepared 2 xml same. please suggest 1 approach.

xml 1

<?xml version="1.0" encoding="utf-8"?> <eform>     <documents>         <document id="c7ba73bb-c096-4099-ad70-fd47d2320d00">             <questions>                 <question id="1a4eb1e0-f657-483e-a8fa-59e9002d7c3f" title="doc 1 - question 1 - paraghraph" answer="abc" />                 <question id="d43fcf9d-91f6-43ce-b970-737aa0a51d36" title="doc 1 - question 2 - date" answer="2014-10-08t18:30:00z" />             </questions>         </document>         <document id="12d726d9-6e77-4657-ab8c-dddb7104a14a">             <questions>                 <question id="3a9d6172-90b6-420d-911a-43857e7c21a2" title="doc 2 - question 1 - selectlist" answer="option 03" />                 <question id="e4ae1e80-3fd5-4f6a-8d57-29166e399deb" title="doc 2 - question 2 - file" answer="" />             </questions>         </document>         <document id="9e3e79d0-0417-4ecd-8d5f-1881a5045705">             <questions>                 <question id="3b6c296e-8cc3-4ba5-9b16-7b5d0bfa6ce0" title="doc 3 - question 1 - text" answer="abc" />             </questions>         </document>         <document id="328cae06-13c4-425e-adc0-e7c32d89b044">             <questions>                 <question id="d9877c67-12fc-4058-8dfe-3997335bce02" title="dropdown list" answer="option 05" />                 <question id="a56aae48-94b8-43d7-b17e-5eb0d16aeebb" title="checkbox list" answer="option 03" />                 <question id="02ea17f1-8c34-4916-89ab-d6dfaf37a403" title="radiobutton list" answer="option 03" />             </questions>         </document>     </documents> </eform> 

xml 2

    <?xml version="1.0" encoding="utf-8"?> <eform>     <document id="c7ba73bb-c096-4099-ad70-fd47d2320d00">             <question>                  <id>1a4eb1e0-f657-483e-a8fa-59e9002d7c3f</id>                 <title>doc 1 - question 1 - paraghraph</title>                 <answer>abc</answer>             </question>              <question>                  <id>d43fcf9d-91f6-43ce-b970-737aa0a51d36</id>                 <title>doc 1 - question 2 - date</title>                 <answer>2014-10-08t18:30:00z</answer>             </question>      </document>     <document id="12d726d9-6e77-4657-ab8c-dddb7104a14a">             <question>                  <id>3a9d6172-90b6-420d-911a-43857e7c21a2</id>                 <title>doc 2 - question 1 - selectlist</title>                 <answer>option 03</answer>             </question>              <question>                  <id>e4ae1e80-3fd5-4f6a-8d57-29166e399deb</id>                 <title>doc 2 - question 2 - file</title>                 <answer></answer>             </question>      </document> </eform> 

i want generate xslt 1 of above xmls in order generate html follows:

question 1 answer of question 1  question 2 answer of question 2 

i working xmls first time , trying out things. tried generating xslt xml2 follows. not giving required output.

<?xml version="1.0" encoding="iso-8859-1"?> <!-- edited xmlspy® --> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/">   <html>   <body>       <xsl:for-each select="eform/document">  <xsl:for-each select="question">         <p><b><xsl:value-of select="title"/></b></p>         <p><xsl:value-of select="answer"/></p>         <br/>  </xsl:for-each>       </xsl:for-each>   </body>   </html> </xsl:template> </xsl:stylesheet> 

please suggest right way go ahead , how select attributes/values using nested foreach in xslt. let me know if there other better way of doing this.

for first format, try:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/">     <html>         <body>             <xsl:for-each select="eform/documents/document/questions/question">                 <p><b><xsl:value-of select="@title"/></b></p>                 <p><xsl:value-of select="@answer"/></p>                 <br/>             </xsl:for-each>         </body>     </html> </xsl:template>  </xsl:stylesheet> 

your second format not well-formed; if were, work it:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:template match="/">     <html>         <body>             <xsl:for-each select="eform/document/question">                 <p><b><xsl:value-of select="title"/></b></p>                 <p><xsl:value-of select="answer"/></p>                 <br/>             </xsl:for-each>         </body>     </html> </xsl:template>  </xsl:stylesheet> 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -