Reputation: 285
I'm having a hard time creating a xls transformation file for the following xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<ExportedData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<StartDate>2015-02-02T00:00:00</StartDate>
<EndDate>2015-03-04T23:59:00</EndDate>
<RecordCount>4</RecordCount>
<Client />
<DocumentCount>0</DocumentCount>
</Header>
<Applicants>
<Applicant>
<ApplicantId>2176</ApplicantId>
<ModuleTypeId>1</ModuleTypeId>
<ApplicantInfo>
<Applications>
<Application>
<ApplicationId>6177</ApplicationId>
<Fields>
<Field>
<FieldName>Action Status</FieldName>
<FieldText>Submitted</FieldText>
</Field>
<Field>
<FieldName>BGCheck Result</FieldName>
<FieldText />
</Field>
<Field>
<FieldName>Date Hired</FieldName>
<FieldText />
</Field>
<Field>
<FieldName>Location Code</FieldName>
<FieldText>ManNY</FieldText>
</Field>
</Fields>
</Application>
</Applications>
</ApplicantInfo>
<ApplicantActionDocsInfo />
<ApplicantFormsInfo />
<ApplicantActionsInfo />
</Applicant>
Obviously there is more to the xml file (more Applicants). I have been trying to use this resource: http://www.w3schools.com/xsl/xsl_transformation.asp which I understand, but when trying to apply it to my xml, it isnt quite working out. I have tried to start with something extremely simple but I'm not getting the correct output.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Applicants Info</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ApplicantId</th>
</tr>
<xsl:for-each select="ExportedData/Applicants/Applicant">
<tr>
<td><xsl:value-of select="ApplicantId"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Again, this is just a simple test case that I have tried (It just shows Applicant Id). My final goal is to essentially display all information presented in the xml. What am I doing wrong?
Upvotes: 0
Views: 77
Reputation: 190
Try to write templates instead of for-each selectors. See comments below.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- match root - build html elements -->
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="ExportedData">
<xsl:apply-templates/>
</xsl:template>
<!-- Do nothing for the header -->
<xsl:template match="Header"/>
<!-- Build table for applicants -->
<xsl:template match="Applicants">
<h2>Applicants Info</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>ApplicantId</th>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
<!-- for each applicant create a table row -->
<xsl:template match="Applicant">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<!-- for each Applicant/ApplicantId generate table cell -->
<xsl:template match="ApplicantId">
<td>
<xsl:apply-templates/>
</td>
</xsl:template>
<!-- you probably will want to add more stuff to the table, use templates to do so.-->
<xsl:template match="ModuleTypeId | ApplicantInfo | ApplicantActionDocsInfo | ApplicantFormsInfo | ApplicantActionsInfo"/>
</xsl:stylesheet>
Upvotes: 1
Reputation: 116992
<xsl:template match="/">
puts you in the context of the root (document) node. From this context,
<xsl:for-each select="Applicants/Applicant">
selects nothing, because Applicants
is not a child of the root node. Try instead:
<xsl:for-each select="ExportedData/Applicants/Applicant">
Upvotes: 2
Reputation: 11416
You'll get the value of ApplicantId
when you change this
<xsl:for-each select="Applicants/Applicant">
to
<xsl:for-each select="//Applicants/Applicant">
or
<xsl:for-each select="ExportedData/Applicants/Applicant">
or the match pattern of your template:
<xsl:template match="/">
to
<xsl:template match="ExportedData">
Currently, your template is matching the root level of the input XML, and Applicants
is not the first element of the input.
Upvotes: 2