[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] xsl with dynamic xpath statement from param and variable


Subject: [xsl] xsl with dynamic xpath statement from param and variable
From: "Ralph Price" <Ralph.Price@xxxxxxxxxxx>
Date: Thu, 6 Sep 2007 11:38:56 +1200

Hi to the list

My problem seems like it should be simple

Essentially there are nodes in my xml that I wish to discard based on a
child value existing in that node. That value is to be supplied by a
param.
The original data looks like:

---------------------------------------------

<origenxml xmlns="">
	<data>
		<item>
			<id>07011*810*00*</id>
			<obj>RT.VALUATION</obj>
			<group>
				<id>MAIN</id>
				<dict id="RT.VAL.ID.RAW">
					<desc>valuation number</desc>
					<val>07011*810*00*</val>
				</dict>
				<dict id="RT.VAL.CANCELDATE">
					<desc>cancel date</desc>
					<val>01 Jan 2006</val>
				</dict>
			</group>
		</item>
		<item>
			<id>07011*810*06*</id>
			<obj>RT.VALUATION</obj>
			<group>
				<id>MAIN</id>
				<dict id="RT.VAL.ID.RAW">
					<desc>valuation number</desc>
					<val>07011*810*06*</val>
				</dict>
				<dict id="RT.VAL.CANCELDATE">
					<desc>cancel date</desc>
				</dict>
			</group>
		</item>
	</data>
</origenxml>

---------------------------------------------

I only wish to retain in the output from the xsl the item elements
where, in this case the <dict id="RT.VAL.CANCELDATE"> does not have a
<val> child.

So far the stylesheet looks like:

---------------------------------------------

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
	<xsl:output omit-xml-declaration="yes" indent="yes"/>
	<xsl:param name="argDictItem">RT.VAL.CANCELDATE</xsl:param>
	<xsl:variable name="sgl_qte">%27</xsl:variable>
	<xsl:variable name="xpathDictItem"
select="concat('group/dict[@id=',$sgl_qte,$argDictItem,$sgl_qte,']/val')
"/>
	<xsl:template match="/">
	<xsl:comment>
	$xpathDictItem = <xsl:value-of select="$xpathDictItem"/>
	</xsl:comment>
		<xsl:copy>
			<xsl:apply-templates select="node()"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="node()">
		<xsl:if test="name()!='item'">
			<!-- carry on copying nodes -->
			<xsl:copy>
				<xsl:apply-templates select="node()"/>
			</xsl:copy>
		</xsl:if>
		<xsl:if test="name()='item'">
			<!-- copy item node only if the argDictItem does
not have a value -->
			<xsl:choose>
				<xsl:when test="not($xpathDictItem)">
					<xsl:comment>
				node discarded - variable value used
				</xsl:comment>
				</xsl:when>
				<xsl:when
test="group/dict[@id='RT.VAL.CANCELDATE']/val">
					<xsl:comment>
				node discarded - hardcoded value used
					</xsl:comment>
				</xsl:when>
				<xsl:otherwise>
					<xsl:copy>
						<xsl:apply-templates
select="node()"/>
					</xsl:copy>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

---------------------------------------------

The output at this stage is generating:
<!--
	$xpathDictItem = group/dict[@id=%27RT.VAL.CANCELDATE%27]/val-->
<origenxml>
	<data>
		<!--
				node discarded - hardcoded value used
					-->
		<item>
			<id>07011*810*06*</id>
			<obj>RT.VALUATION</obj>
			<group>
				<id>MAIN</id>
				<dict>
					<desc>valuation number</desc>
					<val>07011*810*06*</val>
				</dict>
				<dict>
					<desc>cancel date</desc>
				</dict>
			</group>
		</item>
	</data>
</origenxml>

---------------------------------------------

However I wish to be able to delete the <item> elements based on the
argDictItem parameter rather than my hardcoded value.

Any leads would be most appreciated.

Regards

Ralph Price
New Zealand
------------------------------------------------------------------------
CAUTION - This message may contain privileged and confidential information
intended only for the use of the addressee(s) named above.  If you are not
the intended recipient of this message you are notified that any use,
dissemination, distribution or reproduction of this message is prohibited;
If you have received this message in error please notify the sender
immediately via return email and erase all copies of this message and
attachments.  Any views expressed in this message are those of the
individual sender and may not necessarily reflect the views of Rotorua
District Council.
------------------------------------------------------------------------


Current Thread
Keywords