Jump to content

Why Doesn't This Xslt Work?


danalario
 Share

Recommended Posts

I'm writing an XSLT to create an XML file that pulls out only a few selected values from a large XML report. I'm fairly new to this, so please take it easy on me :)Here's the XSLT I've created...I'm only attempting to select one value right now just to test it and get it working.

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <xsl:element name="ObtmActivity">      <xsl:attribute name="FILE_ID">      <xsl:value-of select="CrystalReport/ReportFooter/Section[@SectionNumber='0']/Field[@Name='startdate1']/Value"/>      </xsl:attribute>        </xsl:element>  </xsl:template></xsl:stylesheet>

Here's the XML. Its a bit of a mess because Its generated by Crystal Reports 2008, I've highlighted in Red the value I am attempting to select with my XSLT.

<?xml version="1.0" encoding="UTF-8" ?><!--?xml-stylesheet type="text/xsl" href="exporttest.xslt"?><!--CrystalReport xmlns="urn:crystal-reports:schemas:report-detail"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:crystal-reports:schemas:report-detail [url="http://www.businessobjects.com/products/xml/CR2008Schema.xsd">"]http://www.businessobjects.com/products/xm...a.xsd">[/url]<ReportFooter><Section SectionNumber="0"><Text Name="Text1"><TextValue>Actual Penetration</TextValue></Text><Text Name="Text2"><TextValue>Actual Contacts</TextValue></Text><Text Name="Text3"><TextValue>CRP Sales w/ Filter</TextValue></Text><Text Name="Text4"><TextValue>CRP Sales w/ Cleaner</TextValue></Text><Text Name="Text6"><TextValue>Total CRP Sales</TextValue></Text><Text Name="Text24"><TextValue>CRP Primary Premium Sale</TextValue></Text><Text Name="Text25"><TextValue>CRP Alt. Premium Sale</TextValue></Text><Text Name="Text26"><TextValue>DM_KEY</TextValue></Text><Text Name="Text28"><TextValue>101</TextValue></Text><Field Name="Key1ActualPenetration1" FieldName="{#Key1 Actual Penetration}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="Key2ActualPenetration1" FieldName="{#Key2 Actual Penetration }"><FormattedValue>861</FormattedValue><Value>861</Value></Field><Field Name="Key1ActualContacts1" FieldName="{#Key 1 Actual Contacts}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="FlvFilterSalesKey11" FieldName="{#CRP Sales w/Filter Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="FlvFilterSalesKey21" FieldName="{#CRP Sales w/Filter Key 2}"><FormattedValue>9</FormattedValue><Value>9</Value></Field><Field Name="FlvCleanerSalesKey1" FieldName="{#CRP Sales w/Cleaner Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="FlvCleanerSalesKey21" FieldName="{#CRP Sales w/Cleaner Key 2}"><FormattedValue>7</FormattedValue><Value>7</Value></Field><Field Name="GSContSalesKey1" FieldName="{#CRP Sales Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="GSContSalesKey21" FieldName="{#CRP Sales Key 2}"><FormattedValue>69</FormattedValue><Value>69</Value></Field><Field Name="SFLPrimaryPremSaleKey11" FieldName="{#CRP Primary Prem Sale Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="SFLAltPremSaleKey11" FieldName="{#CRP Alt. Prem Sale Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="SFLAltPremSaleKey21" FieldName="{#CRP Alt. Prem Sale Key 2}"><FormattedValue>12</FormattedValue><Value>12</Value></Field><Field Name="Key2ActualContacts1" FieldName="{#Key 2 Actual Contacts}"><FormattedValue>727</FormattedValue><Value>727</Value></Field><Text Name="Text35"><TextValue>CRP Report run on</TextValue></Text><Field Name="PrintDate2" FieldName="PrintDate"><FormattedValue>2/25/2010</FormattedValue><Value>2010-02-25</Value></Field><Text Name="Text37"><TextValue>CRP Sales w/ Delay</TextValue></Text><Text Name="Text38"><TextValue>10$ One-Time Shipping </TextValue></Text><Text Name="Text39"><TextValue>$5.95 S&H Each Shipment</TextValue></Text><Field Name="CRPSaleswDelayKey1" FieldName="{#CRP Sales w/Delay Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="CRPSaleswDelayKey21" FieldName="{#CRP Sales w/Delay Key 2}"><FormattedValue>7</FormattedValue><Value>7</Value></Field><Field Name="OneTimeShippingKey11" FieldName="{#10$ One-Time Shipping Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="OneTimeShippingKey21" FieldName="{#10$ One-Time Shipping Key 2}"><FormattedValue>47</FormattedValue><Value>47</Value></Field><Field Name="SHKey11" FieldName="{#$5.95 S&H Key 1}"><FormattedValue>0</FormattedValue><Value>0</Value></Field><Field Name="SHKey21" FieldName="{#$5.95 S&H Key 2}"><FormattedValue>22</FormattedValue><Value>22</Value></Field><Field Name="CRPPrimaryPremSaleKey21" FieldName="{#CRP Primary Prem Sale Key 2}"><FormattedValue>57</FormattedValue><Value>57</Value></Field><Text Name="Text5"><TextValue>for dates</TextValue></Text><Text Name="Text7"><TextValue>to</TextValue></Text><Field Name="ActualPenetrationTotal1" FieldName="{@Actual Penetration Total}"><FormattedValue>861</FormattedValue><Value>861.00</Value></Field><Field Name="ActualContactsTotal1" FieldName="{@Actual Contacts Total}"><FormattedValue>727</FormattedValue><Value>727.00</Value></Field>[color="#FF0000"]<Field Name="startdate1" FieldName="{?startdate}"><FormattedValue>20100224</FormattedValue><Value>20100224</Value></Field>[/color]<Field Name="enddate1" FieldName="{?enddate}"><FormattedValue>20100224</FormattedValue><Value>20100224</Value></Field><Field Name="PrintDate1" FieldName="PrintDate"><FormattedValue>2/25/2010</FormattedValue><Value>2010-02-25</Value></Field><Text Name="Text8"><TextValue>Hours</TextValue></Text><Field Name="HoursKey11" FieldName="{@Hours Key 1}"><FormattedValue>0.00</FormattedValue><Value>0.00</Value></Field><Field Name="HoursKey21" FieldName="{@Hours Key 2}"><FormattedValue>49.72</FormattedValue><Value>49.72</Value></Field><Text Name="Text9"><TextValue>105</TextValue></Text></Section></ReportFooter></CrystalReport>

When I run my XSLT against that XML I get this as Output

<?xml version="1.0" encoding="UTF-8"?><ObtmActivity FILE_ID=""/>

when I'd be expecting to see a value of '20100224' in FILE_ID.Why does this not work?Thanks in Advance.I have a copy of the Crystal Reports 2008 Schema if that helps, but I thought all I'd really need to make this work was an example of the default XML output to make a working XSLT.

Link to comment
Share on other sites

XSLT and XPath operate on XML with namespaces and in your XML input document the elements are in a namespace. With XSLT/XPath 1.0, to select elements in a namespace, you need to bind a prefix to the namespace URI and use that prefix in XPath expressions to qualify element names. So put e.g. xmlns:rt="urn:crystal-reports:schemas:report-detail"on the xsl:stylesheet element in your stylesheet and then use that prefix 'rt' to qualify your element names e.g.

<xsl:value-of select="rt:CrystalReport/rt:ReportFooter/rt:Section[@SectionNumber='0']/rt:Field[@Name='startdate1']/rt:Value"/>

The prefix can be choosen freely of course.Your current select expressions selects e.g. elements named 'CrystalReport' in no namespace and that is why you don't see any value as the input XML does not have elements in no namespace.Note also that you can shorten your template contents to

<ObtmActivity FILE_ID="{rt:CrystalReport/rt:ReportFooter/rt:Section[@SectionNumber='0']/rt:Field[@Name='startdate1']/rt:Value}"/>

which is less verbose than those xsl:element/xsl:attribute/xsl:value you currently have.

Link to comment
Share on other sites

Thanks very much! That worked perfectly :)

Note also that you can shorten your template contents to
<ObtmActivity FILE_ID="{rt:CrystalReport/rt:ReportFooter/rt:Section[@SectionNumber='0']/rt:Field[@Name='startdate1']/rt:Value}"/>

which is less verbose than those xsl:element/xsl:attribute/xsl:value you currently have.

This is a very neat way to do it...will that work with XSLT/XPath 1.0?Is there any kind of shortcut aside from altering my <xsl:template match="/"> statement that I could use to avoid having to put 'rt:' in front of every node in the XPath?Thanks again, this has been a HUGE stumbling block for me.
Link to comment
Share on other sites

This is a very neat way to do it...will that work with XSLT/XPath 1.0?
Yes.
Is there any kind of shortcut aside from altering my <xsl:template match="/"> statement that I could use to avoid having to put 'rt:' in front of every node in the XPath?
Not in XSLT 1.0. In XSLT 2.0, you can set "xpath-default-namespace" attribute on the xsl:stylesheet element with the default namespace URI as a value.
Link to comment
Share on other sites

What I posted is a "literal result element and attribute with an attribute value template". That is fully supported in XSLT 1.0.As for the prefix, there is no shorter way in XSLT 1.0 I am afraid. In XSLT 2.0 you can define an xpath-default-namespace to avoid using a prefix but that is a new option in XSLT 2.0.

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...