473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

XSLT to create filtered subset


Given this XML:

<?xml version="1.0" encoding="UTF-8"?>
<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
<position row="1" bay="1" level="B">
<client id="DEF"></client>
</position>
<position row="1" bay="1" level="C">
<client id="GHI"></client>
</position>
</pallet>

I want to return a subset,

<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
</pallet>
But I can't seem to extend my stylesheet to something 3 nodes deep.

This:

<?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"/>
<xsl:template match="/">
<pallet>
<xsl:apply-templates select="pallet/position"/>
</pallet>
</xsl:template>
<xsl:template match="pallet">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="position[@row=0]">
<position>
<xsl:attribute name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribute name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="@level"/>
</xsl:attribute>
</position>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

will return

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
</pallet>
But say I want to extend it to return:

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
<client id="ABC">
</client>
</pallet>

How do I do that?

Jun 12 '06 #1
6 2058
John Bailo wrote:

Given this XML:

<?xml version="1.0" encoding="UTF-8"?>
<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
<position row="1" bay="1" level="B">
<client id="DEF"></client>
</position>
<position row="1" bay="1" level="C">
<client id="GHI"></client>
</position>
</pallet>

I want to return a subset,

<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
</pallet>
But I can't seem to extend my stylesheet to something 3 nodes deep.

This:

<?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"/>
<xsl:template match="/">
<pallet>
<xsl:apply-templates select="pallet/position"/>
</pallet>
</xsl:template>
<xsl:template match="pallet">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="position[@row=0]">
<position>
<xsl:attribute name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribute name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="@level"/>
</xsl:attribute>
</position>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

will return

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
</pallet>
But say I want to extend it to return:

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
<client id="ABC">
</client>
</pallet>

How do I do that?

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jun 12 '06 #2
The complicating factor here is that you want to pull <client> out of
<position>. That means you need to independently match on "the client
which is within the position with row='0'".

Maintaining your somewhat verbose style:

<xsl:template match="client[../position[@row='0']"
<client>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<!-- possible apply-templates here? -->
</client>
</xsl:template/>

If you haven't already done so, you really want to look at the xsl:copy
and xsl:copy-of directives, and at the standard "identity transformation
with exceptions" approachh; those techniques would make this partcular
example significantly cleaner, unless you have a Good Reason for doing
everything explicitly.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jun 12 '06 #3
Sorry, forgot to say that you also need to modify:

<xsl:template match="pallet">
<xsl:apply-templates select="position"/>
<xsl:apply-templates select="position/client"/>
</xsl:template>

Actually, performance would probably be better if you changed that to

<xsl:template match="pallet">
<xsl:apply-templates select="position[@row=0]"/>
<xsl:apply-templates select="position[@row=0]/client"/>
</xsl:template>

You could then consider removing the predicates from the templates for
position and client.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jun 12 '06 #4
Joe Kesselman wrote:
You could then consider removing the predicates from the templates for
position and client.


I am still having trouble. I changed the xslt to this. I think it
should produce an exact replica of the original XML:

<?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"/>

<xsl:template match="/">
<pallet>
<xsl:apply-templates select="pallet/position"/>
</pallet>
</xsl:template>

<xsl:template match="pallet">
<!-- BEGIN YOUR CHANGE -->
<xsl:apply-templates select="position"/>
<xsl:apply-templates select="position/client"/>
<!-- END YOUR CHANGE -->
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="position">
<position>
<xsl:attribute name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribute name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="@level"/>
</xsl:attribute>
</position>
<xsl:apply-templates/>
</xsl:template>

<!-- BEGIN YOUR CHANGE -->
<xsl:template match="client[../position]">
<client>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<!-- possible apply-templates here? -->
</client>
</xsl:template>
<!-- END YOUR CHANGE -->

</xsl:stylesheet>
And the result is:

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
<position row="1" bay="1" level="B" />
<position row="1" bay="1" level="C" />
</pallet>
Instead of

<?xml version="1.0" encoding="UTF-8"?>
<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
<position row="1" bay="1" level="B">
<client id="DEF"></client>
</position>
<position row="1" bay="1" level="C">
<client id="GHI"></client>
</position>
</pallet>

Jun 12 '06 #5
Ok, I think I got it -- thanks for all your help!
You're second suggestion gave me the clues.

I also will try your optimizations -- you've opened my eyes as far as
the potential elegance of this language.
Here's my stylesheet:

<?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"/>

<xsl:template match="/">
<pallet>
<xsl:apply-templates select="pallet/position"/>
</pallet>
</xsl:template>

<xsl:template match="pallet">
<xsl:apply-templates select="position"/>
<xsl:apply-templates select="position/client"/>

</xsl:template>

<xsl:template match="position">
<position>
<xsl:attribute name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribute name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="@level"/>
</xsl:attribute>
<xsl:apply-templates select="client"/>
</position>

</xsl:template>
<xsl:template match="client">
<client>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<!-- possible apply-templates here? -->
</client>

</xsl:template>

</xsl:stylesheet>

John Bailo wrote:
Joe Kesselman wrote:
You could then consider removing the predicates from the templates for
position and client.

I am still having trouble. I changed the xslt to this. I think it
should produce an exact replica of the original XML:

<?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"/>

<xsl:template match="/">
<pallet>
<xsl:apply-templates select="pallet/position"/>
</pallet>
</xsl:template>

<xsl:template match="pallet">
<!-- BEGIN YOUR CHANGE -->
<xsl:apply-templates select="position"/>
<xsl:apply-templates select="position/client"/>
<!-- END YOUR CHANGE -->
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="position">
<position>
<xsl:attribute name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribute name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="@level"/>
</xsl:attribute>
</position>
<xsl:apply-templates/>
</xsl:template>

<!-- BEGIN YOUR CHANGE -->
<xsl:template match="client[../position]">
<client>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<!-- possible apply-templates here? -->
</client>
</xsl:template>
<!-- END YOUR CHANGE -->

</xsl:stylesheet>
And the result is:

<?xml version="1.0" encoding="UTF-16"?>
<pallet>
<position row="0" bay="0" level="A" />
<position row="1" bay="1" level="B" />
<position row="1" bay="1" level="C" />
</pallet>
Instead of

<?xml version="1.0" encoding="UTF-8"?>
<pallet>
<position row="0" bay="0" level="A">
<client id="ABC"></client>
</position>
<position row="1" bay="1" level="B">
<client id="DEF"></client>
</position>
<position row="1" bay="1" level="C">
<client id="GHI"></client>
</position>
</pallet>

Jun 12 '06 #6
John Bailo wrote:

Actually, let's make that cleaner in several ways
<xsl:template match="pallet"> <xsl:for-each select="position[@row='0']">
<xsl:apply-templates select="."/>
<xsl:apply-templates select="client"/>
</xsl:for-each> </xsl:template> .... <xsl:template match="position"> .... <xsl:template match="client">


Let pallet handle selecting which descendents you care about, then just
provide templates for how to render them once selected.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Jun 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Harry Zoroc | last post by:
I would like to treat an xsd Schema file as XML file and to display the targetNamespace and all the imports. That's it. But the following does not work. Why? I did not enter the stylesheet in the...
0
by: Massimiliano | last post by:
Hi all, I created a database application, and created the protection setup, with users and grants. I have this problem: I would like to filter/give access & grants on a particular subset of...
4
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
1
by: Foxpointe | last post by:
Given some arbitrary XHTML, I'd like to obtain a 'simplified' XHTML result which strips out a large subset of standard elements and attributes - but not all. The main things I would like to...
3
by: melnhed | last post by:
---Report the current filtered records from a Form--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. My Config: ...
2
by: Scott Sauyet | last post by:
I'm trying to select a subset of a WSDL document using XSLT and a simple text document describing the high-level elements to include. I have it working fine for selecting the services, bindings,...
1
by: qbp90x5lb | last post by:
I'm using an XSLT transform to output the element value contents from a simple XML file into a new .TXT file. Everything works fine except for certain XML files, when calling msxsl with the .xslt, I...
0
by: ramseyscripts | last post by:
I have Table A that is displayed through a form. Table A can be filtered through the form to create a subset of Table A records. How can I create a new table, Table B, with only the records in the...
11
by: Ebenezer | last post by:
Let's suppose I have some nodes in an XML file, with an URL attribute: <node url="mypage.php?name1=value1&foo=bar&foo2=bar2&name2=value0" /> <node...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.