Jump to content

Reading values of dynamically created elements


Malc
 Share

Recommended Posts

Hi. I'm trying to write an XSLT that parses an XML file that can have different elements from XML file to XML file. This is basically what happens. I write a suite of SQL tests. These tests are run through a validation utility against a database. The results of the tests are dumped to XML as either passed or failed. If failed, then nodes are created basically to contain a table of the failed results. I don't have control over how the XML is created, and I know it could be written more properly to 'name' each element correctly...but it doesn't. I have the XSLT partially working, and I'll include the results here too. Any help would be appreciated, I'm sure one of you Gurus will find this easy enough.XML:

<?xml version="1.0" encoding="utf-8"?><CTestResults xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  <Tests>    <Description>ABSENTEEAPPLICATIONS - AbsenteeApplicationID missing or invalid</Description>    <Test>SELECT top 20 * FROM AbsenteeApplications WHERE AbsenteeApplicationID IS NULL OR AbsenteeApplicationID = ''</Test>    <Passed>true</Passed>  </Tests>  <Tests>    <Description>ABSENTEEAPPLICATIONS - AppTypeCode = 'PERM' (Information Only)</Description>    <Test>SELECT count(*) as CountOf_PERM FROM AbsenteeApplications WHERE AppTypeCode = 'PERM' HAVING count(*) > 0</Test>    <Passed>false</Passed>    <Results>      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">        <xs:element name="NewDataSet" msdata:IsDataSet="true">          <xs:complexType>            <xs:choice maxOccurs="unbounded">              <xs:element name="Table1">                <xs:complexType>                  <xs:sequence>                    <xs:element name="CountOf_PERM" type="xs:int" minOccurs="0" />                  </xs:sequence>                </xs:complexType>              </xs:element>            </xs:choice>          </xs:complexType>        </xs:element>      </xs:schema>      <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">        <NewDataSet>          <Table1 diffgr:id="Table11" msdata:rowOrder="0">            <CountOf_PERM>1030</CountOf_PERM>          </Table1>        </NewDataSet>      </diffgr:diffgram>    </Results>  </Tests>  <Tests>    <Description>ABSENTEEAPPLICATIONS - ApplicationDate missing or invalid or in future</Description>    <Test>SELECT top 20 * FROM AbsenteeApplications WHERE (ApplicationDate IS NULL OR ApplicationDate = '' OR ApplicationDate > getdate()) AND ApplicationDate <> '01-01-1900'</Test>    <Passed>true</Passed>  </Tests>  <Tests>    <Description>ABSENTEEAPPLICATIONS - GovIDNum invalid format (X111-1111-1111-11)</Description>    <Test>SELECT top 20 AbsenteeApplicationID, VoterID, GovIDNum FROM AbsenteeApplicationsWHERE GovIDNum IS NOT NULL AND GovIDNum <> '' AND GovIDNum NOT LIKE '[A-Z][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9]'</Test>    <Passed>true</Passed>  </Tests>  <Tests>    <Description>ABSENTEEAPPLICATIONS - ElectionSelectedMethodCode = 'ALL'</Description>    <Test>SELECT count(*) as CountOf_ALL FROM AbsenteeApplications WHERE ElectionSelectedMethodCode = 'ALL' HAVING count(*) > 0</Test>    <Passed>false</Passed>    <Results>      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">        <xs:element name="NewDataSet" msdata:IsDataSet="true">          <xs:complexType>            <xs:choice maxOccurs="unbounded">              <xs:element name="Table1">                <xs:complexType>                  <xs:sequence>                    <xs:element name="CountOf_ALL" type="xs:int" minOccurs="0" />                  </xs:sequence>                </xs:complexType>              </xs:element>            </xs:choice>          </xs:complexType>        </xs:element>      </xs:schema>      <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">        <NewDataSet>          <Table1 diffgr:id="Table11" msdata:rowOrder="0">            <CountOf_ALL>1036</CountOf_ALL>          </Table1>        </NewDataSet>      </diffgr:diffgram>    </Results>  </Tests>  <Tests>    <Description>ABSENTEEAPPLICATIONS - NameMiddle has invalid characters</Description>    <Test>SELECT top 20 AbsenteeApplicationID, VoterID, NameMiddle FROM AbsenteeApplications WHERE NameMiddle = '' OR NameMiddle LIKE '%[ ,-$%\^\&*\(\)=\+\_\!\\\/\?\<\>\.`\~]%'OR NameMiddle LIKE '% JR%' OR NameMiddle LIKE '% SR%' OR NameMiddle LIKE '% II%' OR NameMiddle LIKE '% III%' OR NameMiddle LIKE '% IV%' </Test>    <Passed>false</Passed>    <Results>      <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">        <xs:element name="NewDataSet" msdata:IsDataSet="true">          <xs:complexType>            <xs:choice maxOccurs="unbounded">              <xs:element name="Table1">                <xs:complexType>                  <xs:sequence>                    <xs:element name="AbsenteeApplicationID" type="xs:int" minOccurs="0" />                    <xs:element name="VoterID" type="xs:int" minOccurs="0" />                    <xs:element name="NameMiddle" type="xs:string" minOccurs="0" />                  </xs:sequence>                </xs:complexType>              </xs:element>            </xs:choice>          </xs:complexType>        </xs:element>      </xs:schema>      <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">        <NewDataSet>          <Table1 diffgr:id="Table11" msdata:rowOrder="0">            <AbsenteeApplicationID>4022001</AbsenteeApplicationID>            <VoterID>4022071</VoterID>            <NameMiddle>J.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table12" msdata:rowOrder="1">            <AbsenteeApplicationID>4022002</AbsenteeApplicationID>            <VoterID>4022072</VoterID>            <NameMiddle>M.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table13" msdata:rowOrder="2">            <AbsenteeApplicationID>4022003</AbsenteeApplicationID>            <VoterID>4022102</VoterID>            <NameMiddle>L.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table14" msdata:rowOrder="3">            <AbsenteeApplicationID>4022004</AbsenteeApplicationID>            <VoterID>4022106</VoterID>            <NameMiddle>B.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table15" msdata:rowOrder="4">            <AbsenteeApplicationID>4022006</AbsenteeApplicationID>            <VoterID>4022159</VoterID>            <NameMiddle>A.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table16" msdata:rowOrder="5">            <AbsenteeApplicationID>4022008</AbsenteeApplicationID>            <VoterID>4022312</VoterID>            <NameMiddle>C.</NameMiddle>          </Table1>          <Table1 diffgr:id="Table17" msdata:rowOrder="6">            <AbsenteeApplicationID>4022009</AbsenteeApplicationID>            <VoterID>4022314</VoterID>            <NameMiddle>F.</NameMiddle>          </Table1>        </NewDataSet>      </diffgr:diffgram>    </Results>  </Tests></CTestResults>

XSLT:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="2.0"    xmlns:xs="http://www.w3.org/2001/XMLSchema"    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"    xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">    <html>      <head>        <style type="text/css">          body {background-color: rgb(250,210,250)}          .h1 {font-family: "courier new", monospace}          .h1 {color: blue}          .h2 {font-family: "courier new", monospace}          .h2 {color: darkblue}          p.sql {font-family: "courier new", monospace}          p.sql {color: green}          p.sql {font-weight: bold}        </style>      </head>    <body>      <h1>SVU FAILED Results</h1>      <xsl:for-each select="CTestResults/Tests">        <xsl:if test="Passed != 'true'">          <span class="h1">            <br>Description: <xsl:value-of select="Description"/></br>          </span>          <span class="h2">            <br>Test: <xsl:value-of select="Test"/></br>          </span>          <table border="1">            <tr>              <xsl:for-each select="Results/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element">                <td><xsl:value-of select="@name"/></td>              </xsl:for-each>            </tr>            <xsl:for-each select="Results/diffgr:diffgram/NewDataSet/Table1">              <tr>                <td><xsl:value-of select="current()"/></td>              </tr>            </xsl:for-each>          </table>        </xsl:if>      </xsl:for-each>    </body>    </html></xsl:template></xsl:stylesheet>

HTML:

<html xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">  <head>    <META http-equiv="Content-Type" content="text/html; charset=utf-8">    <style type="text/css">          body {background-color: rgb(250,210,250)}          .h1 {font-family: "courier new", monospace}          .h1 {color: blue}          .h2 {font-family: "courier new", monospace}          .h2 {color: darkblue}          p.sql {font-family: "courier new", monospace}          p.sql {color: green}          p.sql {font-weight: bold}        </style>  </head>  <body>    <h1>SVU FAILED Results</h1>    <span class="h1">      <br>Description: ABSENTEEAPPLICATIONS - AppTypeCode = 'PERM' (Information Only)    </span>    <span class="h2">      <br>Test: SELECT count(*) as CountOf_PERM FROM AbsenteeApplications WHERE AppTypeCode = 'PERM' HAVING count(*) > 0    </span>    <table border="1">      <tr>        <td>CountOf_PERM</td>      </tr>      <tr>        <td>1030</td>      </tr>    </table>    <span class="h1">      <br>Description: ABSENTEEAPPLICATIONS - ElectionSelectedMethodCode = 'ALL'    </span>    <span class="h2">      <br>Test: SELECT count(*) as CountOf_ALL FROM AbsenteeApplications WHERE ElectionSelectedMethodCode = 'ALL' HAVING count(*) > 0    </span>    <table border="1">      <tr>        <td>CountOf_ALL</td>      </tr>      <tr>        <td>1036</td>      </tr>    </table>    <span class="h1">      <br>Description: ABSENTEEAPPLICATIONS - NameMiddle has invalid characters    </span>    <span class="h2">      <br>Test: SELECT top 20 AbsenteeApplicationID, VoterID, NameMiddle FROM AbsenteeApplications WHERE NameMiddle = '' OR NameMiddle LIKE '%[ ,-$%\^\&*\(\)=\+\_\!\\\/\?\<\>\.`\~]%'OR NameMiddle LIKE '% JR%' OR NameMiddle LIKE '% SR%' OR NameMiddle LIKE '% II%' OR NameMiddle LIKE '% III%' OR NameMiddle LIKE '% IV%'     </span>    <table border="1">      <tr>        <td>AbsenteeApplicationID</td>        <td>VoterID</td>        <td>NameMiddle</td>      </tr>      <tr>        <td>40220014022071J.</td>      </tr>      <tr>        <td>40220024022072M.</td>      </tr>      <tr>        <td>40220034022102L.</td>      </tr>      <tr>        <td>40220044022106B.</td>      </tr>      <tr>        <td>40220064022159A.</td>      </tr>      <tr>        <td>40220084022312C.</td>      </tr>      <tr>        <td>40220094022314F.</td>      </tr>      <tr>        <td>40220104022328R.</td>      </tr>      <tr>        <td>40220124022526W.</td>      </tr>      <tr>        <td>40220134022612A.</td>      </tr>      <tr>        <td>40220144022645M.</td>      </tr>      <tr>        <td>40220154023254B.</td>      </tr>      <tr>        <td>40220184023590T.</td>      </tr>      <tr>        <td>40220194023625J.</td>      </tr>      <tr>        <td>40220204023679A.</td>      </tr>      <tr>        <td>40220214023695A.</td>      </tr>      <tr>        <td>40220224023698P.</td>      </tr>    </table>  </body></html>

Sorry it's so long, I cut out most of the xml/html. If you look at the last section for the HTML, you'll see it takes all 3 element values and creates 1 cell (1 value) with them in the resultant table.Malc :)

Link to comment
Share on other sites

Hi. I have one solution, but if anyone has another idea, that'd be great. Here it is, thanks to Choster @ webmasterworld.com. This will work for more generalized cases.XSLT:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="/">    <html>      <head>        <style type="text/css">          body {background-color: rgb(0,0,0)}          body {color: white}          .h1 {font-family: "courier new", monospace}          .h1 {color: yellow}          .h2 {font-family: "courier new", monospace}          .h2 {color: lightgreen}          p.sql {font-family: "courier new", monospace}          p.sql {color: green}          p.sql {font-weight: bold}        </style>      </head>      <body>        <h1>SVU FAILED Results</h1>        <xsl:apply-templates select="CTestResults/Tests[Passed!= 'true']"/>      </body>    </html>  </xsl:template>  <xsl:template match="Tests">    <span class="h1">      <br>        Description: <xsl:value-of select="Description"/>      </br>    </span>    <span class="h2">      <br>        Test: <xsl:value-of select="Test"/>      </br>    </span>    <table border="1">      <tr>        <xsl:apply-templates select="Results/xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/xs:element"/>      </tr>      <xsl:apply-templates select="Results/diffgr:diffgram/NewDataSet/Table1" />    </table>  </xsl:template>  <xsl:template match="xs:element">    <th>      <xsl:value-of select="@name"/>    </th>  </xsl:template>  <xsl:template match="Table1">    <tr>      <xsl:for-each select="*">        <td>          <xsl:value-of select="."/>        </td>      </xsl:for-each>    </tr>  </xsl:template></xsl:stylesheet>

Regards,Malc :) p.s. the new color scheme is my doing, for those who detest it :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...