Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 6th, 2005, 01:59 PM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select parent node when any one child has key

Hi All

I am new to XML and XSLT.
I have a requirement to transform XML to XML where the resultant document should contain the parent nodes whose any of the child node has an attribute which is in the argument list.

Source doc is
--------------
<?xml version="1.0" encoding="utf-8" ?>
<rootnode>
<parent>
    <parentnode label="parent1">
        <childnode key="key1">
        </childnode>
        <childnode key="key2">
        </childnode>
        <childnode key="key3">
        </childnode>
    </parentnode>
    <parentnode label="parent2">
        <childnode key="key4">
        </childnode>
        <childnode key="key5">
        </childnode>
        <childnode key="key6">
        </childnode>
    </parentnode>
</parent>
</rootnode>

The argument is |key1|key2|key3|

Resultant doc shd be
--------------------
<?xml version="1.0" encoding="utf-8" ?>
<rootnode>
  <parent>
    <parentnode label="parent1">
        </parentnode>
  </parent>
</rootnode>

The transform I use which is not correct
----------------------------------------

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method = "xml" indent="yes" />
    <xsl:variable name="permissions"></xsl:variable>

    <xsl:template match="/">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="parentnode">
        <xsl:for-each select="child::childnode">
            <xsl:if test="contains($permissions,concat('|',./@key,'|'))">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*" />
                </xsl:copy>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The aspx code I use is
-----------------------

XPathDocument menuDoc = new XPathDocument(Server.MapPath("key.xml"));
XslTransform xslt = new XslTransform();
xslt.Load(Server.MapPath("key.xslt"));
XsltArgumentList xslArg = new XsltArgumentList();
string keys= "|key1|key2|key3|";
xslArg.AddParam("permissions", "", keys);
StringWriter writer = new StringWriter();
xslt.Transform(menuDoc, xslArg, writer, null);
xmlstr = writer.ToString();
Response.Write(xmlstr.ToString());

I tried different transforms which either made the application to hang or gave me the same parentnode many times for each of the child .
The parent node should be selected only once even though its all the children contain keys as that of argument list.

Please help

PParades


 
Old January 6th, 2005, 06:41 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

1st thing, in your code:
Code:
    <xsl:template match="parentnode">
        <xsl:for-each select="child::childnode">
            <xsl:if test="contains($permissions,concat('|',./@key,'|'))">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*" />
                </xsl:copy>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

You're going to be copying the child nodes, not the the parent node. I suggest using a conditional select using braces [] to see if you've found a node.
Code:
    <xsl:template match="parentnode">
        <xsl:if test="childnode[contains($permissions,concat('|',./@key,'|'))]">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:if>
    </xsl:template>
 
Old January 7th, 2005, 11:29 AM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi jkmyoung

Thanks very much for your reply . It works !

I changed
<xsl:template match="parentnode">
        <xsl:if test="childnode[contains($permissions,concat('|',./@key,'|'))]">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:if>
</xsl:template>

to
<xsl:template match="parentnode">
    <xsl:if test="childnode[contains($permissions,concat('|',./@key,'|'))]">
          <xsl:copy>
                <xsl:apply-templates select="parent[node()]|@*" />
          </xsl:copy>
     </xsl:if>
</xsl:template>

to get the desired output.

Thanks again
PParades







Similar Threads
Thread Thread Starter Forum Replies Last Post
Select correct child node richieWolf XSLT 4 September 24th, 2008 08:53 AM
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
Problem with displaying parent /child node in C# vishnu108mishra XML 1 November 16th, 2007 05:37 PM
Select first child node that matches criteria priya81 XSLT 4 July 3rd, 2007 10:32 AM
Select node whose child value matches Fantmx XSLT 5 February 16th, 2007 04:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.