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 July 5th, 2011, 10:59 AM
Registered User
 
Join Date: Jul 2011
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Red face Beginner Problem with XLST

I am very new to XSLT , have a book and completed a few examples. However this is all for a small project. which takes a large XML file and outputs the XML omitting certain field. A sample of the XML is below.

Code:
<?xml version="1.0" encoding="windows-1252" ?>
<?xml-stylesheet type="text/xsl" href="testFIDEF.xsl"?>

<FileImport>
  <Global>
    <GlobalParam name="RollName" value="BUFVC003-14 10:00:00:00" />
    <GlobalParam name="TapeOrg" value="10:00:00:00" />
    <GlobalParam name="ReadStart" value="00:00:00:00" />
    <GlobalParam name="ReadDuration" value="00:02:26:18" />
  </Global>
  <Roll>
<!-- roll contains lots of useless data that i don't want nor would it be helpful to you -->
<Field name="pd_roll_ingest_report" value="&lt;?xml />
</Roll>
<MasterClip>
    <Field name="audio_format" value="" group="Ingest" />
    <Field name="camera_id" value="" group="Ingest" />
    <Field name="clip_description" value="railway" group="Ingest" />
    <Field name="clip_type" value="" group="Ingest" />
    <Field name="clip_comments" value="" group="Ingest" />
</MasterClip>
</FileImport>
There are many more fields in MasterClip and the rest of the XML but these are the one ones i need to manage. I need to output only certain Field names in MasterClip eg the final XML transformation should look like.

Code:
<FileImport>
  <Global>
    <GlobalParam name="RollName" value="BUFVC003-08 10:00:00:00" />
    <GlobalParam name="TapeOrg" value="10:00:00:00" />
    <GlobalParam name="ReadStart" value="00:00:00:00" />
    <GlobalParam name="ReadDuration" value="00:00:21:07" />
  </Global>
  <Roll>
  </Roll>
  <MasterClip>
    <Field name="clip_description" value="railway " group="Ingest" />
    <Field name="episode_no." value="BUFVC003-08" group="Ingest" />
    <Field name="rushes_roll_number" value="BUFVC003" group="Ingest" />
    <Field name="source_image_format" value="" group="Ingest" />
    <Field name="technical_comments" value="MUTE" group="Ingest" />
  </MasterClip>
</FileImport>
So I played around with the XSL and found a way to pick the ones I need.

Code:
 <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="no" />
    <xsl:variable name="fields" select="'|clip_description|episode_no|'" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="MasterClip">
        <xsl:copy>
            <xsl:apply-templates select=
                "*[contains($fields, concat('|', @name, '|'))]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
However this brings the "Rolls" tag with it in the output. I think this is because i am using @* instead of just dealing with searching inside MasterClip but i can not figure out how to handle the identity transformation. essentially I want Global - outputted the same as input , Roll with just opening and closing tags (no data) and then handle the MasterClip file "like" the above code.

Please help me, it seems easy enough doing one thing till i start trying to do something else on top :S.

The global tag i handled this way , if it helps:

Code:
 <!--Global-->
    <xsl:template match="Global">
        <Global>
            <xsl:apply-templates />
        </Global>
    </xsl:template>

    <xsl:template match="GlobalParam">
        <GlobalParam>
            <xsl:attribute name="name">
                <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="@value" />
            </xsl:attribute>
        </GlobalParam>
    </xsl:template>
Any and all help very much appreciated.
 
Old July 5th, 2011, 01:34 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't need to tell us you're a beginner, we can always recognize beginners because their fingers invariably type "XLST"!

You say that the problem with your code is that it copies the Rolls tag into the output, but there is no Rolls tag in your input. It looks as if you want to copy the Roll start and end tag, but not the content between them. To achieve that you just need to add another rule:

Code:
<xsl:template match="Roll">
  <xsl:copy/>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
amendir (July 6th, 2011)
 
Old July 6th, 2011, 02:26 PM
Registered User
 
Join Date: Jul 2011
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Default Thanks

Thanks very much for the Help , code snippet worked a treat . I laughed at the XLST , noticed i have been doing that a lot my self. I am sure it will fade with practice.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XLST Replace rangeshram XSLT 1 November 23rd, 2009 07:50 AM
Beginner problem E2209 C++ Programming 1 November 7th, 2009 02:08 PM
Problem for a beginner antoneath Beginning PHP 1 January 29th, 2007 09:13 AM
Beginner problem PAZII Java Basics 4 February 1st, 2006 05:27 AM
XLST transformNode problem beyondtron Classic ASP XML 0 February 4th, 2004 04:52 PM





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