Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using CDATA sections.

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
How do I specify that I want all elements to be output as CDATA sections? My Parser fails (with error * is not a valid QNAME) when I specify the following:

Code:
<xsl:output method="xml" cdata-section-elements="*" indent="no" encoding="UTF-8"/>

Thanks.
 
cdata-section-elements specifies a list of the names of elements whose [!]text node children[/!] should be output using CDATA sections

Just put your
Code:
cdata-section-elements = "rootNode (whatever it may be)"

[monkey][snake] <.
 
That does not seem to work for me...I am using Oxygen and Saxon6.5.5.
Code:
<xsl:output method="xml" cdata-section-elements="PurchaseOrder" indent="no" encoding="UTF-8"/>

just output escapes special characters..but no CDATA sections are inserted.

--or--
Code:
<xsl:output method="xml" cdata-section-elements="PurchaseOrder POHeader POLineList POFooter"/>

creates this:
Code:
<?xml version="1.0" encoding="utf-8"?><PurchaseOrder><![CDATA[]]><POHeader><![CDATA[
        ]]><ProductLine>TEST</ProductLine><![CDATA[
        ]]><OrderType type="CP"/><![CDATA[
        ]]><Date>20060915</Date><![CDATA[
        ]]><OrderStatus type="1">
            <Name>REPRINT</Name>

 
This is a stab here but try not specifying method="xml" in your xsl:eek:utput element.

[monkey][snake] <.
 
To:eek:p
The specific and default behaviour of the whitespace is implementation dependent. There are two solutions.

[1] Either you include only a selected set of leaf element nodes in the cdata-section-elements. Like this.
[tt]
<xsl:eek:utput method="xml" cdata-section-elements="PurchaseLine OrderType Date"/>
[/tt]
(But those elements are not selected in your list.)

[2] Or, for mixed-content type or in general elements which are not leaf, you still want to include in the list, then it would be acute in implementation specific behaviour. In that case you have to supplement by the xsl:strip-space instruction. List this.
[tt]
<xsl:eek:utput method="xml" cdata-section-elements="PurchaseOrder POHeader POLineList POFooter" indent="yes"/>
<xsl:strip-space elements="PurchaseOrder POHeader POLineList POFooter" />
[/tt]
(Just include intended strip-space list which may not necessarily coincide with the cdata-section-elements list.)
 
Below is the code...and perhaps there is an easier way of doing what I want to do and that is add a new Element:

PurchaseOrder/POLineList/POLine/Cost/CalcQty which is the extendedAmount divided by the UnitCost. Thanks All.

XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
<xsl:output method="xml" cdata-section-elements="PurchaseOrder"/>
    <xsl:template match="/">
        <PurchaseOrder>
                <xsl:copy-of select="PurchaseOrder/*[local-name() != 'POLineList']"/>
                <POLineList>
                <xsl:for-each select="//POLineList/POLine">
                     <POLine>
                         <xsl:copy-of select="*"/>
                         <CalcQty><xsl:value-of select="translate(//Cost/ExtendedAmount,'+','') div translate(//Cost/UnitCost,'+','')"/></CalcQty>
                     </POLine>
                </xsl:for-each>
                </POLineList> 
        </PurchaseOrder>
    </xsl:template>
</xsl:stylesheet>

XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<PurchaseOrder>
    <POHeader>
        <ProductLine><![CDATA[TEST]]></ProductLine>
        <OrderType type="CP"/>
        <Date><![CDATA[20060915]]></Date>
        <OrderStatus type="1">
            <Name><![CDATA[REPRINT]]></Name>
        </OrderStatus>
        <POIdentification>
            <PONumber><![CDATA[91]]></PONumber>
            <POCode><![CDATA[1151]]></POCode>
            <PORelease>0000</PORelease>
        </POIdentification>
        <Revision>
            <Number><![CDATA[1]]></Number>
            <Date><![CDATA[20070607]]></Date>
            <Time><![CDATA[150735]]></Time>
        </Revision>
        <IssueInstructions>
            <IssueMethod type="P">
                <Name><![CDATA[Paper]]></Name>
            </IssueMethod>
        </IssueInstructions>
        <InvoiceMethod type="IBM">
            <Name><![CDATA[Invoice by mail]]></Name>
        </InvoiceMethod>
        <Vendor>
            <VendorID>
                <ID><![CDATA[2456]]></ID>
                <VendorGroupID>
                    <ID><![CDATA[VEND]]></ID>
                </VendorGroupID>
            </VendorID>
            <Name><![CDATA[P & G/GILLETTE]]></Name>
            <VendorContact>
                <Contact>
                    <Name/>
                    <TelephoneNumber>
                        <Number/>
                    </TelephoneNumber>
                    <FaxNumber>
                        <TelephoneNumber>
                            <Number/>
                        </TelephoneNumber>
                    </FaxNumber>
                </Contact>
            </VendorContact>
            <Address>
                <AddressLineList>
                    <AddressLine><![CDATA[PO BOX 905798]]></AddressLine>
                </AddressLineList>
                <City><![CDATA[CHARLOTTE]]></City>
                <StateOrProvince><![CDATA[NC]]></StateOrProvince>
                <PostalCode><![CDATA[28290-5798]]></PostalCode>
            </Address>
        </Vendor>
        <BuyerOrganization>
            <Company>
                <ID><![CDATA[5000]]></ID>
                <Name><![CDATA[XXXXXX]]></Name>
            </Company>
            <Address>
                <AddressLineList/>
            </Address>
            <DefaultDelivery>
                <Date><![CDATA[20060918]]></Date>
                <Location>
                    <ID><![CDATA[50101]]></ID>
                    <Name><![CDATA[XXXXXXXXXXXX]]></Name>
                    <Address>
                        <AddressLineList>
                            <AddressLine><![CDATA[100 Central Avenue, Bldg. 73-A]]></AddressLine>
                        </AddressLineList>
                        <City><![CDATA[Kearny]]></City>
                        <StateOrProvince><![CDATA[XX]]></StateOrProvince>
                        <PostalCode><![CDATA[99999]]></PostalCode>
                    </Address>
                </Location>
                <Contact>
                    <Name/>
                    <TelephoneNumber>
                        <Number/>
                    </TelephoneNumber>
                    <FaxNumber>
                        <TelephoneNumber>
                            <Number/>
                        </TelephoneNumber>
                    </FaxNumber>
                </Contact>
            </DefaultDelivery>
            <BuyerContact>
                <UserID><![CDATA[sjones]]></UserID>
                <ID><![CDATA[SJ]]></ID>
                <Contact>
                    <Name><![CDATA[Steve Jones]]></Name>
                    <TelephoneNumber>
                        <Number><![CDATA[999 732 4535]]></Number>
                    </TelephoneNumber>
                    <FaxNumber>
                        <TelephoneNumber>
                            <Number/>
                        </TelephoneNumber>
                    </FaxNumber>
                    <EMail>
                        <EMailAddress><![CDATA[sjones@xxxxx.com]]></EMailAddress>
                    </EMail>
                </Contact>
            </BuyerContact>
        </BuyerOrganization>
        <PaymentTerms>
            <LetterOfCredit/>
            <DiscountTerms>
                <ID><![CDATA[N30]]></ID>
                <Name><![CDATA[NET 30]]></Name>
                <DiscountDaysDue><![CDATA[0]]></DiscountDaysDue>
                <DiscountRate><![CDATA[+0.00000]]></DiscountRate>
                <NetDaysDue><![CDATA[30]]></NetDaysDue>
            </DiscountTerms>
            <Currency>
                <ID><![CDATA[USD]]></ID>
                <Name><![CDATA[UNITED STATES DOLLAR]]></Name>
            </Currency>
            <EnteredConversionRate><![CDATA[+1.0000000]]></EnteredConversionRate>
            <ReceiptConversionRate><![CDATA[+1.0000000]]></ReceiptConversionRate>
            <ProcessLevel/>
        </PaymentTerms>
        <DeliveryTerms>
            <FreightTerms>
                <ID><![CDATA[P]]></ID>
                <Name/>
            </FreightTerms>
        </DeliveryTerms>
        <POUserData>
            <POUserDates>
                <Date/>
                <Date/>
            </POUserDates>
            <POUserFields>
                <UserField/>
                <UserField><![CDATA[p&g 895442]]></UserField>
                <UserField/>
            </POUserFields>
        </POUserData>
        <AddOnChargeList/>
        <MessageList/>
    </POHeader>
    <POLineList>
        <POLine>
            <LineNumber><![CDATA[1]]></LineNumber>
            <BuyerItem>
                <ID><![CDATA[3096794]]></ID>
                <Description><![CDATA[GSF4 USA COFFEE FILTER]]></Description>
            </BuyerItem>
            <VendorItem>
                <ID><![CDATA[3096794]]></ID>
            </VendorItem>
            <SupplierPartAuxiliary>
                <SupplierPartAuxiliaryID/>
            </SupplierPartAuxiliary>
            <Quantity>
                <Number><![CDATA[+360.0000]]></Number>
                <UnitOfMeasure><![CDATA[RU]]></UnitOfMeasure>
            </Quantity>
            <Cost>
                <UnitCost><![CDATA[+6.0000000]]></UnitCost>
                <UnitOfMeasure><![CDATA[RU]]></UnitOfMeasure>
                <TaxableUnitCost><![CDATA[+0.0000000]]></TaxableUnitCost>
                <ExtendedAmount><![CDATA[+2160.00]]></ExtendedAmount>
                <ExtendedTaxableAmount><![CDATA[+0.00]]></ExtendedTaxableAmount>
                <InvoiceTaxAmount><![CDATA[+0.00]]></InvoiceTaxAmount>
            </Cost>
            <Delivery>
                <DeliveryInstructionList/>
            </Delivery>
            <HealthIndustryNumber/>
            <GlobalLocationNumber/>
            <MSDSRequired><![CDATA[N]]></MSDSRequired>
            <CertificationRequired><![CDATA[N]]></CertificationRequired>
            <MatchDetailKey><![CDATA[3096794]]></MatchDetailKey>
            <AddOnChargeList/>
            <MessageList/>
            <POUserData>
                <POUserDates>
                    <Date/>
                    <Date/>
                </POUserDates>
                <POUserFields>
                    <UserField/>
                    <UserField/>
                    <UserField/>
                </POUserFields>
            </POUserData>
        </POLine>
        <POLine>
            <LineNumber><![CDATA[5]]></LineNumber>
            <BuyerItem>
                <ID><![CDATA[3105710]]></ID>
                <Description><![CDATA[KF 580 E WH NA COFFEE MAKER]]></Description>
            </BuyerItem>
            <VendorItem>
                <ID><![CDATA[3096794]]></ID>
            </VendorItem>
            <SupplierPartAuxiliary>
                <SupplierPartAuxiliaryID/>
            </SupplierPartAuxiliary>
            <Quantity>
                <Number><![CDATA[+12.0000]]></Number>
                <UnitOfMeasure><![CDATA[RU]]></UnitOfMeasure>
            </Quantity>
            <Cost>
                <UnitCost><![CDATA[+52.0000000]]></UnitCost>
                <UnitOfMeasure><![CDATA[RU]]></UnitOfMeasure>
                <TaxableUnitCost><![CDATA[+0.0000000]]></TaxableUnitCost>
                <ExtendedAmount><![CDATA[+624.00]]></ExtendedAmount>
                <ExtendedTaxableAmount><![CDATA[+0.00]]></ExtendedTaxableAmount>
                <InvoiceTaxAmount><![CDATA[+0.00]]></InvoiceTaxAmount>
            </Cost>
            <Delivery>
                <DeliveryInstructionList/>
            </Delivery>
            <HealthIndustryNumber/>
            <GlobalLocationNumber/>
            <MSDSRequired><![CDATA[N]]></MSDSRequired>
            <CertificationRequired><![CDATA[N]]></CertificationRequired>
            <MatchDetailKey><![CDATA[3105710]]></MatchDetailKey>
            <AddOnChargeList/>
            <MessageList/>
            <POUserData>
                <POUserDates>
                    <Date/>
                    <Date/>
                </POUserDates>
                <POUserFields>
                    <UserField/>
                    <UserField/>
                    <UserField/>
                </POUserFields>
            </POUserData>
        </POLine>
    </POLineList>
    <POFooter>
        <POTotal><![CDATA[+62637.78]]></POTotal>
        <TaxTotal><![CDATA[+0.00]]></TaxTotal>
        <AddOnChargeTotal><![CDATA[+0.00]]></AddOnChargeTotal>
        <VendorGrandTotal><![CDATA[+62637.78]]></VendorGrandTotal>
        <TotalTaxableAmount><![CDATA[+0.00]]></TotalTaxableAmount>
        <TotalProductAmount><![CDATA[+62637.78]]></TotalProductAmount>
        <Lines><![CDATA[18]]></Lines>
        <TaxSummary/>
        <AddOnChargeSummary/>
        <MessageList/>
    </POFooter>
 
I thought I answered the question. They apparently don't think so. Is that a new question?
 
tsuji,

I am not sure I understand your solution. As you can see there are several hundread elements in my document. I do not want to create a list of all the elements that need to be CDATA sections. I was hoping there would be a way to tell the parser that all elements should be CDATA sections?

k5tm asked for code so I posted it.

Thanks.
 
XSLT By Doug Tidwell (First Edition August 2001, ISBN 0-596-00053-7) publishes its examples on the net at One of the examples in Appendix D is an XSLT which makes an alphabetized list of the elements in an XML document. I ran this against your example with the following result:
Code:
Summary of Elements

Element AddOnChargeList occurs 3 times.
Element AddOnChargeSummary occurs 1 times.
Element AddOnChargeTotal occurs 1 times.
Element Address occurs 3 times.
Element AddressLine occurs 2 times.
Element AddressLineList occurs 3 times.
Element BuyerContact occurs 1 times.
Element BuyerItem occurs 2 times.
Element BuyerOrganization occurs 1 times.
Element CertificationRequired occurs 2 times.
Element City occurs 2 times.
Element Company occurs 1 times.
Element Contact occurs 3 times.
Element Cost occurs 2 times.
Element Currency occurs 1 times.
Element Date occurs 9 times.
Element DefaultDelivery occurs 1 times.
Element Delivery occurs 2 times.
Element DeliveryInstructionList occurs 2 times.
Element DeliveryTerms occurs 1 times.
Element Description occurs 2 times.
Element DiscountDaysDue occurs 1 times.
Element DiscountRate occurs 1 times.
Element DiscountTerms occurs 1 times.
Element EMail occurs 1 times.
Element EMailAddress occurs 1 times.
Element EnteredConversionRate occurs 1 times.
Element ExtendedAmount occurs 2 times.
Element ExtendedTaxableAmount occurs 2 times.
Element FaxNumber occurs 3 times.
Element FreightTerms occurs 1 times.
Element GlobalLocationNumber occurs 2 times.
Element HealthIndustryNumber occurs 2 times.
Element ID occurs 12 times.
Element InvoiceMethod occurs 1 times.
Element InvoiceTaxAmount occurs 2 times.
Element IssueInstructions occurs 1 times.
Element IssueMethod occurs 1 times.
Element LetterOfCredit occurs 1 times.
Element LineNumber occurs 2 times.
Element Lines occurs 1 times.
Element Location occurs 1 times.
Element MSDSRequired occurs 2 times.
Element MatchDetailKey occurs 2 times.
Element MessageList occurs 4 times.
Element Name occurs 12 times.
Element NetDaysDue occurs 1 times.
Element Number occurs 9 times.
Element OrderStatus occurs 1 times.
Element OrderType occurs 1 times.
Element POCode occurs 1 times.
Element POFooter occurs 1 times.
Element POHeader occurs 1 times.
Element POIdentification occurs 1 times.
Element POLine occurs 2 times.
Element POLineList occurs 1 times.
Element PONumber occurs 1 times.
Element PORelease occurs 1 times.
Element POTotal occurs 1 times.
Element POUserData occurs 3 times.
Element POUserDates occurs 3 times.
Element POUserFields occurs 3 times.
Element PaymentTerms occurs 1 times.
Element PostalCode occurs 2 times.
Element ProcessLevel occurs 1 times.
Element ProductLine occurs 1 times.
Element PurchaseOrder occurs 1 times.
Element Quantity occurs 2 times.
Element ReceiptConversionRate occurs 1 times.
Element Revision occurs 1 times.
Element StateOrProvince occurs 2 times.
Element SupplierPartAuxiliary occurs 2 times.
Element SupplierPartAuxiliaryID occurs 2 times.
Element TaxSummary occurs 1 times.
Element TaxTotal occurs 1 times.
Element TaxableUnitCost occurs 2 times.
Element TelephoneNumber occurs 6 times.
Element Time occurs 1 times.
Element TotalProductAmount occurs 1 times.
Element TotalTaxableAmount occurs 1 times.
Element UnitCost occurs 2 times.
Element UnitOfMeasure occurs 4 times.
Element UserField occurs 9 times.
Element UserID occurs 1 times.
Element Vendor occurs 1 times.
Element VendorContact occurs 1 times.
Element VendorGrandTotal occurs 1 times.
Element VendorGroupID occurs 1 times.
Element VendorID occurs 1 times.
Element VendorItem occurs 2 times.

A minor modification produces:
Code:
AddOnChargeList AddOnChargeSummary AddOnChargeTotal Address AddressLine AddressLineList BuyerContact BuyerItem BuyerOrganization CertificationRequired City Company Contact Cost Currency Date DefaultDelivery Delivery DeliveryInstructionList DeliveryTerms Description DiscountDaysDue DiscountRate DiscountTerms EMail EMailAddress EnteredConversionRate ExtendedAmount ExtendedTaxableAmount FaxNumber FreightTerms GlobalLocationNumber HealthIndustryNumber ID InvoiceMethod InvoiceTaxAmount IssueInstructions IssueMethod LetterOfCredit LineNumber Lines Location MSDSRequired MatchDetailKey MessageList Name NetDaysDue Number OrderStatus OrderType POCode POFooter POHeader POIdentification POLine POLineList PONumber PORelease POTotal POUserData POUserDates POUserFields PaymentTerms PostalCode ProcessLevel ProductLine PurchaseOrder Quantity ReceiptConversionRate Revision StateOrProvince SupplierPartAuxiliary SupplierPartAuxiliaryID TaxSummary TaxTotal TaxableUnitCost TelephoneNumber Time TotalProductAmount TotalTaxableAmount UnitCost UnitOfMeasure UserField UserID Vendor VendorContact VendorGrandTotal VendorGroupID VendorID VendorItem

This need for CDATA is extraordinary. Is there an explanation offered? I can understand using CDATA for elements that might encounter the occasional ampersand, less-than, etc., but to do it for every element seems bizarre.

Tom Morrison
 
Now, regarding that calculation. You have
Code:
<xsl:for-each select="//POLineList/POLine">
    <POLine>
        <xsl:copy-of select="*"/>
        <CalcQty><xsl:value-of select="translate([COLOR=red]//[/color]Cost/ExtendedAmount,'+','') div translate([COLOR=red]//[/color]Cost/UnitCost,'+','')"/></CalcQty>
    </POLine>
</xsl:for-each>

This will calculate the identical value for each POLine. If you eliminate the '//', indicated above in red, you will get the result you expect. The // is causing you to collect the identical node set of all Cost/ExtendedAmount and Cost/UnitCost nodes. However, I think your intention is to use the single nodes that are associated with the POLine node that is the current context of the xsl:for-each, correct?

Tom Morrison
 
The XML document I am processing is not of my creation. It is coming from a off-the-shelf ERP system. I have no control over the number of CDATA sections or the overall structure of the XML file.


Thanks.
 
Ok, I have seen such misunderstanding in other implementations as well. Probably the document is being produced by some brute-force method instead of an XML tool.

The use of CDATA in the input document should not have any effect on the output document. Your XML processor should provide entity escaping if you are producing an XML document as output. Only if the consumer of your transformed XML document is also a 'brute fore' consumer should you worry. The only place that things might get tricky is if you are using the text output method.

Thanks for satisfying my curiosity! [bigsmile]

Tom Morrison
 
Brute force is right...when I look at the code within the ERP system I see alot of:

Code:
MOVE "CDATA[....]" TO WS-XML-LINE


Looks like Cobol to me. [afro2] I love the 70's
 
Careful, be very careful... [small]Have a look at my web site.[/small]
rofl.gif


That is, however, the unfortunate result when the right tools are not employed.


Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top