Subject: RE: XSLT to query/output a portion of an XML source document
From: Chris Coyle <chriscoyle@xxxxxxxxx>
Date: Fri, 2 Feb 2007 03:49:07 -0800 (PST)
|
Thanks for the responses. Based on your help, the
following stylesheet was implemented.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"
encoding="UTF-8"/>
<xsl:param name="productid"/>
<xsl:param name="productidext"/>
<xsl:param name="companynumber"/>
<xsl:template match="/">
<DataService>
<Transaction>
<Command type="Add">
<MasterCatalogRecord etype="Entity"
commandqualifier="Validate/Process">
<xsl:apply-templates/>
</MasterCatalogRecord>
</Command>
</Transaction>
</DataService>
</xsl:template>
<xsl:template match="Transaction">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Response">
<xsl:apply-templates
select="MasterCatalogRecord[ExternalKeys[Key[@name='PRODUCTID']=$productid
and Key[@name='PRODUCTIDEXT']=$productidext]]"/>
</xsl:template>
<xsl:template match="MasterCatalogRecord">
<xsl:copy-of select="ExternalKeys"/>
<xsl:copy-of select="EntityData"/>
</xsl:template>
<!-- <xsl:template
match="//DataService/Transaction/Response/MasterCatalogRecord/ExternalKeys/Key[@name='PRODUCTIDEXT']">-->
<xsl:template
match="//Key[@name='PRODUCTIDEXT']/text()">
<xsl:value-of select="$companynumber"/>
</xsl:template>
<xsl:template match="TransactionResult">
</xsl:template>
</xsl:stylesheet>
I now have a requirement to only extract the <Key>
elements which have values.
Thanks in advance.
Chris
--- cknell@xxxxxxxxxx wrote:
> I had to clean up your XML ("MasterCatalogResponse"
> in the opening tag does not match "" in the closing
> tag), but here is a quick and dirty stylesheet that
> does what you ask.
>
> Whenever I do one of these, the next person to come
> along invariably has a much more elegant solution,
> so you may want to hold your fire pending further
> responses.
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:strip-space elements="*" />
> <xsl:output method="xml" indent="yes"
> encoding="UTF-8" />
>
> <xsl:param name="key1" />
> <xsl:param name="key2" />
>
> <xsl:template match="/">
> <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="Transaction">
> <xsl:apply-templates />
> </xsl:template>
>
> <xsl:template match="Response">
> <xsl:apply-templates
> select="MasterCatalogResponse[ExternalKeys[Key=$key1
> and Key=$key2]]" />
> </xsl:template>
>
> <xsl:template match="MasterCatalogResponse">
> <xsl:copy-of select="EntityData" />
> </xsl:template>
>
> </xsl:stylesheet>
> --
> Charles Knell
> cknell@xxxxxxxxxx - email
>
>
>
> -----Original Message-----
> From: Chris Coyle <chriscoyle@xxxxxxxxx>
> Sent: Wed, 24 Jan 2007 11:51:12 -0800 (PST)
> To: "Mullberytech.com"
> <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
> Subject: XSLT to query/output a portion of an
> XML source document
>
>
>
> I need to write a query to pull 1 node out of a
> source
> XML document.
>
> The source document has a structure of
>
> <Transaction>
> <Response>
> <MasterCatalogResponse>
> <ExternalKeys>
> <Key>1</Key>
> <Key>a</Key>
> </ExternalKeys>
> <EntityData>
> <Attribute>a</Attribute>
> <Attribute>b</Attribute>
> </EntityData>
> </MasterCatalogRecord>
> <MasterCatalogResponse>
> <ExternalKeys>
> <Key>2</Key>
> <Key>b</Key>
> </ExternalKeys>
> <EntityData>
> <Attribute>a</Attribute>
> <Attribute>b</Attribute>
> </EntityData>
> </MasterCatalogRecord>
> <MasterCatalogResponse>
> <ExternalKeys>
> <Key>3</Key>
> <Key>c</Key>
> </ExternalKeys>
> <EntityData>
> <Attribute>a</Attribute>
> <Attribute>b</Attribute>
> </EntityData>
> </MasterCatalogRecord>
> </Response>
> </Transaction>
>
> I need to extract the <EntityData> element for a
> given
> <ExternalKeys> value match. The result should look
> like this for an ExternalKeys (2,b)
>
> <EntityData>
> <Attribute>a</Attribute>
> <Attribute>b</Attribute>
> </EntityData>
>
>
> This is what I tried:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <?altova_samplexml SampleQueryResponse.xml?>
> <xsl:stylesheet version="2.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:fn="http://www.w3.org/2005/xpath-functions">
>
> <xsl:output method="xml" encoding="UTF-8"
> indent="yes" />
>
> <xsl:template match="/">
> <xsl:for-each
>
select="//DataService/Transaction/Response/MasterCatalogRecord">
> <xsl:element name="EntityData">
> <xsl:value-of select="."/>
> </xsl:element>
> </xsl:for-each>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
> Any suggestions are welcome.
>
>
>
>
>
>
>
____________________________________________________________________________________
> Bored stiff? Loosen up...
> Download and play hundreds of games for free on
> Yahoo! Games.
> http://games.yahoo.com/games/front
>
>
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367
|