You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Tim Hibbs <tv...@fedex.com> on 2010/02/18 19:16:59 UTC

Validate XSL approach?

All,
 
I'm soliciting some validation of my approach to developing XSL for a
project I'm working on after receiving some very welcome help yesterday.
 
Questions:

1.	In the XSL below, is it a reasonable approach to discard
"unwanted" text nodes with the template
	
	 <xsl:template match="frq:From | frq:HeaderDate | frq:HeaderTime
| frq:FaxNumber">
	 </xsl:template>?
	

	*	Is there a better way to do this?
	*	Must this be done element-by-element, or is there a
"shorthand" way of eliminating a set of sibling nodes via XPath
expression?

2.	Given the inclusion of the namespace specifier
"xmlns:frq="http://www.fedex.com/schemas/freightRateQuotation" in the
XSL, must all references to nodes in that namespace always be preceded
with "frq:" wherever they are referenced (such as in xsl:template
match="..." statements)?
3.	I'd like to "left align" the "To:" and "Email Address:" lines in
my output. Is there a defined set of output formatting rules in XSL
(which in particular defines how output lines are indented)? As
suggested yesterday by Michael Ludwig, I included an "<xsl:output
method="html" indent="yes"/>" line, but that doesn't seem to impact
other than the HTML syntax elements.

 
XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tFreightRateQuotation
xmlns="http://www.fedex.com/schemas/freightRateQuotation">
    <AccountInformation>
        <CompanyName>Customer Company</CompanyName>
        <Address1>123 Main Street</Address1>
        <City>Anytown</City>
        <State>CO</State>
        <Postal>80134</Postal>
        <Country>US</Country>
    </AccountInformation>
    <CommonData>
        <To>John Customer</To>
        <From>Fedex</From>
        <HeaderDate>02/02/10</HeaderDate>
        <HeaderTime>12:01:00</HeaderTime>
        <FaxNumber>3031234567</FaxNumber>
        <EmailAddress>John.Customer@customerco.com</EmailAddress>
    </CommonData>
    <RateQuote>
        <QuoteNumber>1 12345678</QuoteNumber>
        <OriginAddress>
            <OriginCity>Salt Lake City</OriginCity>
            <OriginState>UT</OriginState>
            <OriginPostal>84106</OriginPostal>
            <OriginCountry>US</OriginCountry>
        </OriginAddress>
    </RateQuote>
</tFreightRateQuotation>
 
XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:frq="http://www.fedex.com/schemas/freightRateQuotation"
exclude-result-prefixes="frq">
 <xsl:output method="html" indent="yes"/>
 <xsl:template match="frq:tFreightRateQuotation">
  <html>
   <body>
   <!--Add a newline-->
    <xsl:text>
    </xsl:text>
    
    <xsl:apply-templates select="frq:CommonData"/>
   <!--Add a newline-->
    <xsl:text>
    </xsl:text>
 
   </body>
  </html>
 </xsl:template>
 
 <xsl:template match="frq:To">
  <xsl:text> To: </xsl:text>
  <xsl:value-of select="."/>
  <!--Add a newline-->
  <xsl:text>
  </xsl:text>
 </xsl:template>
 
 <xsl:template match="frq:From | frq:HeaderDate | frq:HeaderTime |
frq:FaxNumber">
 </xsl:template>
 
 <xsl:template match="frq:EmailAddress">
  <xsl:text> Email Address: </xsl:text>
  <xsl:value-of select="." />
 </xsl:template>
</xsl:stylesheet>
 
Resulting Output:
<html>
<body>
     To: John Customer
   Email Address: John.Customer@customerco.com
    </body>
</html>
 
Thanks in advance for your time and advice. It's been very encouraging.

Tim Hibbs
FedEx Services
350 Spectrum Loop
Colorado Springs, CO 80921
719-484-2131

 

RE: Validate XSL approach?

Posted by Tim Hibbs <tv...@fedex.com>.
 
 Perfect, Michael. I switch these questions to the other list, though
you've addressed these directly. 

Thanks once again.

Tim Hibbs
FedEx Services
350 Spectrum Loop
Colorado Springs, CO 80921
719-484-2131


-----Original Message-----
From: Michael Ludwig [mailto:milu71@gmx.de] 
Sent: Thursday, February 18, 2010 3:57 PM
To: Xalan-J Users
Subject: Re: Validate XSL approach?

Tim Hibbs schrieb am 18.02.2010 um 12:16:59 (-0600):

> 1.	In the XSL below, is it a reasonable approach to discard
> "unwanted" text nodes with the template
> 	
> 	 <xsl:template match="frq:From | frq:HeaderDate | frq:HeaderTime
> | frq:FaxNumber">
> 	 </xsl:template>?

I'd prefer <xsl:template match="..."/>, the other version might result
in additional whitespace depending on your settings.

> 	*	Is there a better way to do this?
> 	*	Must this be done element-by-element, or is there a
> "shorthand" way of eliminating a set of sibling nodes via XPath 
> expression?

<!-- discard all element nodes below frq:CommonData --> <xsl:template
match="frq:CommonData/*"/>

Read up about priorities when matching nodes.

> 2.	Given the inclusion of the namespace specifier
> "xmlns:frq="http://www.fedex.com/schemas/freightRateQuotation" in the 
> XSL, must all references to nodes in that namespace always be preceded

> with "frq:" wherever they are referenced (such as in xsl:template 
> match="..." statements)?

Yes.

> 3.	I'd like to "left align" the "To:" and "Email Address:" lines in
> my output. Is there a defined set of output formatting rules in XSL 
> (which in particular defines how output lines are indented)?

Whitespace accuracy is a bit tedious in all template languages I've
seen, which has to do with the nature of the template: sometimes you
want the whitespace for output, sometimes you want it only for source
code formatting.

Take a look at <xsl:strip-space> and <xsl:text>.

Newline with numeric character reference: <xsl:text>&#10;</xsl:text>

> As suggested yesterday by Michael Ludwig, I included an "<xsl:output 
> method="html" indent="yes"/>" line, but that doesn't seem to impact 
> other than the HTML syntax elements.

If you want plain text: <xsl:output method="text"/>

General XSL questions are better asked at xsl-list at mulberrytech dot
com.

--
Michael Ludwig

Re: Validate XSL approach?

Posted by Michael Ludwig <mi...@gmx.de>.
Tim Hibbs schrieb am 18.02.2010 um 12:16:59 (-0600):

> 1.	In the XSL below, is it a reasonable approach to discard
> "unwanted" text nodes with the template
> 	
> 	 <xsl:template match="frq:From | frq:HeaderDate | frq:HeaderTime
> | frq:FaxNumber">
> 	 </xsl:template>?

I'd prefer <xsl:template match="..."/>, the other version might result
in additional whitespace depending on your settings.

> 	*	Is there a better way to do this?
> 	*	Must this be done element-by-element, or is there a
> "shorthand" way of eliminating a set of sibling nodes via XPath
> expression?

<!-- discard all element nodes below frq:CommonData -->
<xsl:template match="frq:CommonData/*"/>

Read up about priorities when matching nodes.

> 2.	Given the inclusion of the namespace specifier
> "xmlns:frq="http://www.fedex.com/schemas/freightRateQuotation" in the
> XSL, must all references to nodes in that namespace always be preceded
> with "frq:" wherever they are referenced (such as in xsl:template
> match="..." statements)?

Yes.

> 3.	I'd like to "left align" the "To:" and "Email Address:" lines in
> my output. Is there a defined set of output formatting rules in XSL
> (which in particular defines how output lines are indented)?

Whitespace accuracy is a bit tedious in all template languages I've
seen, which has to do with the nature of the template: sometimes you
want the whitespace for output, sometimes you want it only for source
code formatting.

Take a look at <xsl:strip-space> and <xsl:text>.

Newline with numeric character reference: <xsl:text>&#10;</xsl:text>

> As suggested yesterday by Michael Ludwig, I included an "<xsl:output
> method="html" indent="yes"/>" line, but that doesn't seem to impact
> other than the HTML syntax elements.

If you want plain text: <xsl:output method="text"/>

General XSL questions are better asked at xsl-list at mulberrytech dot
com.

-- 
Michael Ludwig