Last modified on 25 November 2014, at 05:42

XQuery/Web XML Viewer

MotivationEdit

You want to view XML documents using your web browser using HTML markup.

MethodEdit

We will use an XQuery function that uses the dispatch pattern and the typeswitch function.

<Return returnVersion="2014v2.0" xmlns="http://www.irs.gov/efile" xmlns:efile="http://www.irs.gov/efile">

 <ReturnHeader binaryAttachmentCnt="0">
   <ReturnTs>2014-09-22T17:35:25Z</ReturnTs>
   <TaxYr>2014</TaxYr>
   <TaxPeriodBeginDt>2014-01-01</TaxPeriodBeginDt>
   <TaxPeriodEndDt>2014-12-31</TaxPeriodEndDt>
   <SoftwareId>13000063</SoftwareId>
   <SoftwareVersionNum>2014Q.0.12.42</SoftwareVersionNum>
   <OriginatorGrp>
     <EFIN>611907</EFIN>
     <OriginatorTypeCd>ERO</OriginatorTypeCd>
     <PractitionerPINGrp>
       <EFIN>611907</EFIN>
       <PIN>33333</PIN>
     </PractitionerPINGrp>
   </OriginatorGrp>
   <PINTypeCd>Practitioner</PINTypeCd>
   <JuratDisclosureCd>Practitioner PIN</JuratDisclosureCd>
   <PrimaryPINEnteredByCd>ERO</PrimaryPINEnteredByCd>
   <SpousePINEnteredByCd>ERO</SpousePINEnteredByCd>
   <PrimarySignaturePIN>11041</PrimarySignaturePIN>
   <SpouseSignaturePIN>12041</SpouseSignaturePIN>
   <PrimarySignatureDt>2015-03-06</PrimarySignatureDt>
   <SpouseSignatureDt>2015-03-06</SpouseSignatureDt>
   <ReturnTypeCd>1040A</ReturnTypeCd>
   <Filer>
     <PrimarySSN>308001041</PrimarySSN>
     <SpouseSSN>308012041</SpouseSSN>
     <NameLine1Txt>FDTEST & FDSPOUSE<FDFOREIGNADDRESS</NameLine1Txt>
     <PrimaryNameControlTxt>FDFO</PrimaryNameControlTxt>
     <SpouseNameControlTxt>FDFO</SpouseNameControlTxt>
     <ForeignAddress>
       <AddressLine1Txt>REJECT 04</AddressLine1Txt>
       <CityNm>MARROCOS</CityNm>
       <CountryCd>SP</CountryCd>
       <ForeignPostalCd>15369</ForeignPostalCd>
     </ForeignAddress>
   </Filer>
   <PaidPreparerInformationGrp>
     <PTIN>P33333333</PTIN>
     <PreparerFirmEIN>610945193</PreparerFirmEIN>
     <PreparerPersonNm>AMANDA HUGNKISS</PreparerPersonNm>
     <PreparerFirmName>
       <BusinessNameLine1Txt>H AND R BLOCK</BusinessNameLine1Txt>
     </PreparerFirmName>
     <PreparerUSAddress>
       <AddressLine1Txt>965 S US 25W STE 26</AddressLine1Txt>
       <CityNm>WILLIAMSBURG</CityNm>
       <StateAbbreviationCd>KY</StateAbbreviationCd>
       <ZIPCd>40769</ZIPCd>
     </PreparerUSAddress>
     <PhoneNum>6065495668</PhoneNum>
     <EmailAddressTxt>taxpro@tax.com</EmailAddressTxt>
   </PaidPreparerInformationGrp>
   <IPAddress>
     <IPv4AddressTxt>10.9.2.16</IPv4AddressTxt>
   </IPAddress>
   <OnlineFilerInformation>
     <EmailAddressTxt>TEST5@TAX.COM</EmailAddressTxt>
   </OnlineFilerInformation>
 </ReturnHeader>
 <ReturnData documentCnt="5">
   <IRS1040A documentId="4">
     <PECFPrimaryInd>X</PECFPrimaryInd>
     <IndividualReturnFilingStatusCd>2</IndividualReturnFilingStatusCd>
     <ExemptSpouseInd>X</ExemptSpouseInd>
     <TotalExemptPrimaryAndSpouseCnt>1</TotalExemptPrimaryAndSpouseCnt>
     <TotalExemptionsCnt>01</TotalExemptionsCnt>
     <WagesSalariesAndTipsAmt referenceDocumentId="1 2 3">14800</WagesSalariesAndTipsAmt>
     <TotalIncomeAmt>14800</TotalIncomeAmt>
     <AdjustedGrossIncomeAmt>14800</AdjustedGrossIncomeAmt>
     <TotalItemizedOrStandardDedAmt>12400</TotalItemizedOrStandardDedAmt>
     <AGILessDeductionsAmt>2400</AGILessDeductionsAmt>
     <ExemptionsAmt>3950</ExemptionsAmt>
     <EstimatedTaxPaymentsAmt>1500</EstimatedTaxPaymentsAmt>
     <EICEligibilityLiteralCd>NO</EICEligibilityLiteralCd>
     <TotalPaymentsAmt>1500</TotalPaymentsAmt>
     <OverpaidAmt>1500</OverpaidAmt>
     <RefundAmt>1500</RefundAmt>
     <Form8888Ind referenceDocumentId="5">X</Form8888Ind>
     <ThirdPartyDesigneeInd>false</ThirdPartyDesigneeInd>
     <PrimaryOccupationTxt>STUDENT</PrimaryOccupationTxt>
     <SpouseOccupationTxt>INSTALLER</SpouseOccupationTxt>
     <DaytimePhoneNum>6145555555</DaytimePhoneNum>
     <RefundAnticipationLoanCd>NO BANK PRODUCT</RefundAnticipationLoanCd>
   </IRS1040A>
   <IRS8888 documentId="5">
     <DirectDepositInfoGroup>
       <DirectDepositRefundAmt>300</DirectDepositRefundAmt>
       <RoutingTransitNum>044000037</RoutingTransitNum>
       <BankAccountTypeCd>1</BankAccountTypeCd>
       <DepositorAccountNum>5555555</DepositorAccountNum>
     </DirectDepositInfoGroup>
     <DirectDepositInfoGroup>
       <DirectDepositRefundAmt>300</DirectDepositRefundAmt>
       <RoutingTransitNum>044000037</RoutingTransitNum>
       <BankAccountTypeCd>1</BankAccountTypeCd>
       <DepositorAccountNum>9999999</DepositorAccountNum>
     </DirectDepositInfoGroup>
     <DirectDepositInfoGroup>
       <DirectDepositRefundAmt>900</DirectDepositRefundAmt>
       <RoutingTransitNum>044000037</RoutingTransitNum>
       <BankAccountTypeCd>2</BankAccountTypeCd>
       <DepositorAccountNum>9955599</DepositorAccountNum>
     </DirectDepositInfoGroup>
     <TotalAllocationOfRefundAmt>1500</TotalAllocationOfRefundAmt>
   </IRS8888>
   <IRSW2 documentId="1">
     <EmployeeSSN>308001041</EmployeeSSN>
     <EmployerEIN>334555468</EmployerEIN>
     <EmployerNameControlTxt>SMAL</EmployerNameControlTxt>
     <EmployerName>
       <BusinessNameLine1Txt>SMALL JOB</BusinessNameLine1Txt>
     </EmployerName>
     <EmployerUSAddress>
       <AddressLine1Txt>888 MAIN ST</AddressLine1Txt>
       <CityNm>DUBLIN</CityNm>
       <StateAbbreviationCd>OH</StateAbbreviationCd>
       <ZIPCd>43017</ZIPCd>
     </EmployerUSAddress>
     <EmployeeNm>FDTEST FDFOREIGNADDRESS</EmployeeNm>
     <EmployeeForeignAddress>
       <AddressLine1Txt>REJECT 04</AddressLine1Txt>
       <CityNm>MARROCOS</CityNm>
       <CountryCd>SP</CountryCd>
       <ForeignPostalCd>15369</ForeignPostalCd>
     </EmployeeForeignAddress>
     <WagesAmt>800</WagesAmt>
     <StandardOrNonStandardCd>S</StandardOrNonStandardCd>
   </IRSW2>
   <IRSW2 documentId="2">
     <EmployeeSSN>308012041</EmployeeSSN>
     <EmployerEIN>664916868</EmployerEIN>
     <EmployerNameControlTxt>ACME</EmployerNameControlTxt>
     <EmployerName>
       <BusinessNameLine1Txt>ACME</BusinessNameLine1Txt>
     </EmployerName>
     <EmployerUSAddress>
       <AddressLine1Txt>147 L ST</AddressLine1Txt>
       <CityNm>Dublin</CityNm>
       <StateAbbreviationCd>OH</StateAbbreviationCd>
       <ZIPCd>43017</ZIPCd>
     </EmployerUSAddress>
     <EmployeeNm>FDSPOUSE FDFOREIGNADDRESS</EmployeeNm>
     <EmployeeForeignAddress>
       <AddressLine1Txt>REJECT 04</AddressLine1Txt>
       <CityNm>MARROCOS</CityNm>
       <CountryCd>SP</CountryCd>
       <ForeignPostalCd>15369</ForeignPostalCd>
     </EmployeeForeignAddress>
     <WagesAmt>9500</WagesAmt>
     <SocialSecurityWagesAmt>9500</SocialSecurityWagesAmt>
     <SocialSecurityTaxAmt>589</SocialSecurityTaxAmt>
     <MedicareWagesAndTipsAmt>9500</MedicareWagesAndTipsAmt>
     <MedicareTaxWithheldAmt>138</MedicareTaxWithheldAmt>
     <StandardOrNonStandardCd>S</StandardOrNonStandardCd>
   </IRSW2>
   <WagesNotShownSchedule documentId="3">
     <WagesNotShownSch>
       <WagesLiteralCd>SCH</WagesLiteralCd>
       <WagesNotShownAmt>4500</WagesNotShownAmt>
     </WagesNotShownSch>
   </WagesNotShownSchedule>
 </ReturnData>

</Return>

Sample XML to HMTL FunctionEdit

(: sequence dispatcher :)
declare function xml-to-html:dispatch($input as node()*, $depth as xs:integer) as item()* {
let $left-margin := concat('margin-left: ', ($depth * 5), 'px' )
for $node in $input
   return 
      typeswitch($node)
        case text() return normalize-space(<span class="d">{$node}</span>)
        case element()
           return
              <div class="element" style="{$left-margin}">
                    <span class="t">&lt;{name($node)}</span>
                    { (: for each attribute create two spans for the name and value :)
                     for $att in $node/@*
                        return
                           (
                              <span class="an"> {name($att)}=</span>,
                              <span class="av">"{string($att)}"</span>
                           )
                    }
                    {normalize-space(<span class="t">&gt;</span>)}
                    { (: now get the sub elements :)
                       for $c in $node
                          return xml-to-html:dispatch($c/node(), $depth + 1)
                    }
                    <span class="t">&lt;/{name($node)}&gt;</span>
             </div> 
        (: otherwise pass it through.  Used for comments, and PIs :)
        default return $node
};

Sample DriverEdit

xquery version "1.0";
 
import module namespace xml-to-html="http://example.com/xml-to-html" at "xml-to-html.xqm";
 
let $title := 'View XML as HTML'
 
let $input :=
<aaa a1="A1" a2="A2" a3="A3">
    <bbb b1="B1" b2="B2" b3="B3">BBB</bbb>
    <ccc c1="C1" c2="C2" c3="C3">
       <ddd d1="D1" d2="D2" d3="D3">DDD</ddd>
       <eee>
           <fff>FFF</fff>
       </eee>
    </ccc>
</aaa>
 
let $output := xml-to-html:xml-to-html($input, 1)
 
return
<html>
   <head>
      <title>{$title}</title>
      <link type="text/css" rel="stylesheet" href="syntax-colors-oxygen.css"/>
   </head>
   <body>
      <div class="xml">
        {$output}
      </div>
   </body>
</html>

Sample OutputEdit

<div class="xml">
    <div class="element" style="margin-left: 5px">
        <span class="t">&lt;aaa</span>
        <span class="an">a1=</span>
        <span class="av">"A1"</span>
        <span class="an">a2=</span>
        <span class="av">"A2"</span>
        <span class="an">a3=</span>
        <span class="av">"A3"</span>&gt;<div class="element" style="margin-left: 10px">
            <span class="t">&lt;bbb</span>
            <span class="an">b1=</span>
            <span class="av">"B1"</span>
            <span class="an">b2=</span>
            <span class="av">"B2"</span>
            <span class="an">b3=</span>
            <span class="av">"B3"</span>&gt;BBB<span class="t">&lt;/bbb&gt;</span>
        </div>
        <div class="element" style="margin-left: 10px">
            <span class="t">&lt;ccc</span>
            <span class="an">c1=</span>
            <span class="av">"C1"</span>
            <span class="an">c2=</span>
            <span class="av">"C2"</span>
            <span class="an">c3=</span>
            <span class="av">"C3"</span>&gt;<div class="element" style="margin-left: 15px">
                <span class="t">&lt;ddd</span>
                <span class="an">d1=</span>
                <span class="av">"D1"</span>
                <span class="an">d2=</span>
                <span class="av">"D2"</span>
                <span class="an">d3=</span>
                <span class="av">"D3"</span>&gt;DDD<span class="t">&lt;/ddd&gt;</span>
            </div>
            <div class="element" style="margin-left: 15px">
                <span class="t">&lt;eee</span>&gt;<div class="element" style="margin-left: 20px">
                    <span class="t">&lt;fff</span>&gt;FFF<span class="t">&lt;/fff&gt;</span>
                </div>
                <span class="t">&lt;/eee&gt;</span>
            </div>
            <span class="t">&lt;/ccc&gt;</span>
        </div>
        <span class="t">&lt;/aaa&gt;</span>
    </div>
</div>

Sample CSS FileEdit

File: syntax-colors-oxygen.css

/* Begin and end tag Delimiter */
.t {color: blue;}
/* Attribute Name and equal sign */
.an {color: orange;}
/* Attribute Values and equal sign */
.av {color: orange;}
/* Element Data Content */
.d {color: black;}

Screen ImageEdit

screen image of rendering in browser with CSS file