473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:styleshe et 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:attribut e name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribut e name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribut e 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 2072
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:styleshe et 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:attribut e name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribut e name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribut e 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:attribut e 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="positio n"/>
<xsl:apply-templates select="positio n/client"/>
</xsl:template>

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

<xsl:template match="pallet">
<xsl:apply-templates select="positio n[@row=0]"/>
<xsl:apply-templates select="positio n[@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:styleshe et 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="positio n"/>
<xsl:apply-templates select="positio n/client"/>
<!-- END YOUR CHANGE -->
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="position ">
<position>
<xsl:attribut e name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribut e name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribut e 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:attribut e 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:styleshe et 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="positio n"/>
<xsl:apply-templates select="positio n/client"/>

</xsl:template>

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

</xsl:template>
<xsl:template match="client">
<client>
<xsl:attribut e 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:styleshe et 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="positio n"/>
<xsl:apply-templates select="positio n/client"/>
<!-- END YOUR CHANGE -->
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="position ">
<position>
<xsl:attribut e name="row">
<xsl:value-of select="@row"/>
</xsl:attribute>
<xsl:attribut e name="bay">
<xsl:value-of select="@bay"/>
</xsl:attribute>
<xsl:attribut e 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:attribut e 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="positio n[@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
1674
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 xsd file directly but tried to compute the output on the command line e.g. with xalan like: java net.sf.saxon.Transform -o myout.html myschema.xsd myxsltfile.xslt Using Saxon yields no better result. The produced myout.html contains all HTML...
0
1138
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 data, on the basis of the user logged in. In other words, say you have a table with a field containing a value, I would like a particular user who logs in to see modify and delete only his own data subset,
4
8969
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 document. My original XML is, <Dates> <Birth Year="1966" />
1
1272
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 accomplish: 1) Provide a list of elements/attributes to be stripped (i.e. everything else should be passed through) or those that should be passed through (i.e. everything else should be stripped) which would be applied recursively. 2) If an element...
3
3460
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: Access 2002 front-end using SQL Server 2000 (MSDE actually) via ADP/ADE Access Data Project.
2
3780
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, portTypes, and messages to use. But I'm stumped on how to get the xs:simpleType and xs:complexType elements filtered properly. I am not trying to do this in a generic way. The WSDL is generated by a tool in our backend system, and I know a fair...
1
3812
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 get the following error: Code: 0xc00ce504 File: ...... Line: ... Column: ... A name was started with an invalid character. The error refers to the '<' found in the string "<20" from my example xml file below (a).
0
1581
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 filtered subset of Table A?
11
4558
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 url="myotherpage.php?name4=value4&foo=bar3&foo2=bar5&name2=value8" /> and so on. Let's suppose I want to retrieve this @url parameter, BUT ONLY with the values, in querystring, associated with "foo" and "foo2" (thus discarding name1, name2, name4 and every other different ones).
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.