user685590
user685590

Reputation: 2564

Ignoring XML tags in XSL

I have a question regarding XML translations , I have an XML that I translate to another XML . My xls is pretty straightforward , just taking the fields I want . What I notice though is that if for example I have 1 , 2 ,3 ,4 and in my xsl I just decide i want 1,3 that 2 will come with it as well. I believe I read about xsl going by default rules and such so:...

Do I need to create a rule for each tag, even the ones I don't want?. How do I handle ones I do not want ?. ( I tried a few things but it still outputs it). Is there any tutorials or good pages on straight XML 2 XML translations?.

All insight is great, I am away to google more.

Thanks.

This is my XSL as it currently stands without the Match Filter on:

<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="xml" />

    <!--FileImport-->

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

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

    <xsl:template match="StartParam">
        <StartParam>
            <xsl:attribute name="name">
                <xsl:value-of select="@name" />
            </xsl:attribute>
            <xsl:attribute name="value">
                <xsl:value-of select="@value" />
            </xsl:attribute>
        </StartParam>
    </xsl:template>


    <!-- CLip -->
    <xsl:variable name="fields" select="'|clip|number|technical_comments|channel|'" />
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Clip">
        <xsl:copy>
            <xsl:apply-templates select=
                "*[contains($fields, concat('|', @name, '|'))]" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

And a bit of my XML is

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

<FileImport>
    <Global>
        <GlobalParam name="RollName" value="Scene1" />
        <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>
        <Field name="ingest_report" value="&lt;?xml version=&quot;1.0&quot;  standalone=&quot;yes&quot;?&gt;&#xD;&#xA;&lt;DataSet1 
        </Roll>
        <Clip>
            <Field name="audio_format" value="" group="Ingest" />
            <Field name="camera_id" value="" group="Ingest" />
        </Clip>

    </FileImport>

And Hopefully my output will look like

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="testFIDEF.xsl"?>

<FileImport>
    <Global>
        <GlobalParam name="RollName" value="Scene_Around_Six_Tape_3_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>

    <MasterClip>
        <Field name="clip_description" value="Interview Captain Austin Ardill re Terence O'Neill" group="Ingest" />
        <Field name="rushes_roll_number" value="BUFVC003" group="Ingest" />
        <Field name="source_image_format" value="" group="Ingest" />
        <Field name="technical_comments" value="" group="Ingest" />
    </MasterClip>
</FileImport>

Upvotes: 0

Views: 840

Answers (2)

Emiliano Poggi
Emiliano Poggi

Reputation: 24826

As per your comments:

The search works and it only returns the one's I specify but obviously i don't want <roll> to come along with the output.

Look at this part of your transform:

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

Such a template is known as Identity Transformation. Because its task is to copy everything in the input to the output, when you use it in a transform you must explicitely shut up the unwanted elements. As per your comments, if you do not want roll you need a template like:

<xsl:template match="roll"/>

That is, you override the identity and make the transform do nothing for that element.

Upvotes: 1

detaylor
detaylor

Reputation: 7280

By default transforms will out the value of any text node if there is not a match with another template rule. You can avoid this by adding the template:

<xsl:template match="text()|@*" />

This will override the default rule for text and attributes. This does mean that you will need to explicitly select the values for text to be output using xsl:value-of.

Upvotes: 2

Related Questions