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 December 8th, 2007, 08:49 PM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select a path to keep in XSLT

Hi,
i’m a beginner whit xslt then i need help.
I want to delete all elementos son of the node Article that they aren’t except ‘PubModel’ and ‘ArticleDate’. I used the following code. It works but it doesn’t keep attributes in the resulting XML. Why? How can I do?

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

<xsl:template match="//PubmedArticle/MedlineCitation/Article/*[name()!='PubModel'][name()!='ArticleDate']


 
Old December 9th, 2007, 07:17 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You want the first bit to be the 'identity transform':

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


/- Sam Judson : Wrox Technical Editor -/
 
Old December 9th, 2007, 07:06 PM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Tank you. It work but it keep all attributes.
I wanted a little bit different thing.
For example, if i want to keep MedlineCitation node's child Article, that is an element and MedlineCitation node's attributes Owner and status,
i use the following expression, but i keep also all other attributes. Why?


<xsl:template match="//PubmedArticle/MedlineCitation/*[name()!='Article'][name()!='Owner'][name()!='Status']" />

Tanks a lot again.



 
Old December 10th, 2007, 05:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think there may be a terminology problem here. You are referring to "Owner" and "Status" as attributes, but your XPath expression is treating them as child elements. It might be useful if you show us your source XML and desired output XML so we can work out what you are talking about.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 10th, 2007, 12:03 PM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For example.
If I have this tree structure:

<PubmedArticle>
    <MedlineCitation Owner="NLM" Status="Publisher">
      <PMID>15688454</PMID>
      <MedlineJournalInfo>
        <MedlineTA>J Morphol</MedlineTA>
        <NlmUniqueID>0406125</NlmUniqueID>
      </MedlineJournalInfo>
    </MedlineCitation>
  </PubmedArticle>

And I want this output:

<MedlineCitation Owner="NLM" >
      <MedlineJournalInfo>
        <MedlineTA>J Morphol</MedlineTA>
        <NlmUniqueID>0406125</NlmUniqueID>
      </MedlineJournalInfo>
    </MedlineCitation>
  </PubmedArticle>

I had then delete red elements.
If i use the following template:

<xsl:template match="//PubmedArticle/MedlineCitation/*[name()!='MedlineJournalInfo'][name()!='Owner'] />

It doesn't work. Maybe because Owner isn't a child of MedlineCitation. Then How can i say to xslt processor delete all
MedlineCitation attributes except 'Owner'attribute?



 
Old December 10th, 2007, 12:17 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you want to DELETE the red elements then you simply need the identity template like so:

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

Followed by the matches for the ones you want to delete (i.e. not copy):

<xsl:template match="MedlineCitation/@Status" Priority="2" />
<xsl:template match="MedlineCitation/PMID" Priority="2" />

The priority isn't actually required but it highlights what you are trying to do.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 10th, 2007, 12:26 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You seem confused by the difference between attributes and child elements. MedLineJournalInfo is an element, Owner is an attribute. So you want:

<xsl:template match="//PubmedArticle/MedlineCitation/*[name()!='MedlineJournalInfo'] />

<xsl:template match="//PubmedArticle/MedlineCitation/@*[name()!='Owner'] />

Except that this is far more complicated than it needs to be. Better to use the identity template (as given by Sam) and then define empty template rules for the nodes you want to delete, namely match="PMID" and match="@Status".



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 10th, 2007, 12:30 PM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried this template,
and now all work properly.


<xsl:template match="//PubmedArticle/MedlineCitation/@*[name()!='Owner'] />


Tanks a lot Michael Kay!
Tanks a lot Sam Judson!







Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing the all-path shortest path problem bitwords XSLT 1 December 6th, 2006 11:37 AM
using app.path in database path and filename kd8con VB Databases Basics 2 October 25th, 2006 11:45 AM
XSLT and SELECT with a specfic date range pallone XSLT 15 May 8th, 2006 12:41 PM
XSLT Select Distinct jaa XSLT 1 April 6th, 2006 11:10 AM
Convert logical path to absolute path zoostar J2EE 1 April 15th, 2005 10:36 AM





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