-
Changing an XML document using XSL transformations
Hi. I have an XML document that I want to perform an XSL transformation on
and I'm not sure how to get the results I want. Let me give an example of
the original and desired documents.
Original XML:
<Material_Description Material_ID="MyVideo">
<Volume Material_ID="MyVideo">
<Label>MyVideoFootage</Label>
<Shot Material_ID="Shot1">
<In_Frame />
<Out_Frame />
<Shot Material_ID="SubShot1a">
<In_Frame />
<Out_Frame />
</Shot>
<Shot Material_ID="SubShot1b">
<In_Frame />
<Out_Frame />
</Shot>
</Shot>
<Shot Material_ID="Shot2">
<In_Frame />
<Out_Frame />
</Shot>
<Volume>
</Material_Description>
I'd like to use an XSL transformation to create an EXACT COPY of the original
XML document plus a modification in the XML node hierarchy so that if a "SubShot"
exists, it replaces its "Shot" parent node.
Desired XML:
<Material_Description Material_ID="MyVideo">
<Volume Material_ID="MyVideo">
<Label>MyVideoFootage</Label>
<Shot Material_ID="SubShot1a">
<In_Frame />
<Out_Frame />
</Shot>
<Shot Material_ID="SubShot1b">
<In_Frame />
<Out_Frame />
</Shot>
<Shot Material_ID="Shot2">
<In_Frame />
<Out_Frame />
</Shot>
<Volume>
</Material_Description>
Everything I read about XSL transformations indicates that this should be
possible, but I can't work out the syntax to achieve the desired result.
I believe I could solve this using a DOM, but I'm very new to XSL and I
suspect the XSL solution would be more elegant and extensible. Humble cries
for help from an XSL rookie.
Best regards,
ADK.
-
Re: Changing an XML document using XSL transformations
You need to test the Shot nodes to see if they contain child nodes using the
xsl:if or (more likely) the xsl:choose commands. To get the name of the
context node, use the name() function, for example:
<xsl:template match="Shot">
<xsl:choose>
<xsl:when test="count(child::Shot) > 0">
<xsl:copy-of select="child::Shot"/>
</xsl:when>
<xsl therwise>
<xsl:copy-of select="."/>
</xsl therwise>
</xsl:choose>
</xsl:template>
Russell Jones
Sr. Web Development Editor
DevX
"adk" <adk@adv.sonybpe.com> wrote in message
news:3ab5f6db$1@news.devx.com...
>
> Hi. I have an XML document that I want to perform an XSL transformation on
> and I'm not sure how to get the results I want. Let me give an example of
> the original and desired documents.
>
> Original XML:
> <Material_Description Material_ID="MyVideo">
> <Volume Material_ID="MyVideo">
> <Label>MyVideoFootage</Label>
> <Shot Material_ID="Shot1">
> <In_Frame />
> <Out_Frame />
>
> <Shot Material_ID="SubShot1a">
> <In_Frame />
> <Out_Frame />
> </Shot>
>
> <Shot Material_ID="SubShot1b">
> <In_Frame />
> <Out_Frame />
> </Shot>
> </Shot>
> <Shot Material_ID="Shot2">
> <In_Frame />
> <Out_Frame />
> </Shot>
> <Volume>
> </Material_Description>
>
> I'd like to use an XSL transformation to create an EXACT COPY of the
original
> XML document plus a modification in the XML node hierarchy so that if a
"SubShot"
> exists, it replaces its "Shot" parent node.
>
> Desired XML:
> <Material_Description Material_ID="MyVideo">
> <Volume Material_ID="MyVideo">
> <Label>MyVideoFootage</Label>
> <Shot Material_ID="SubShot1a">
> <In_Frame />
> <Out_Frame />
> </Shot>
> <Shot Material_ID="SubShot1b">
> <In_Frame />
> <Out_Frame />
> </Shot>
> <Shot Material_ID="Shot2">
> <In_Frame />
> <Out_Frame />
> </Shot>
> <Volume>
> </Material_Description>
>
> Everything I read about XSL transformations indicates that this should be
> possible, but I can't work out the syntax to achieve the desired result.
> I believe I could solve this using a DOM, but I'm very new to XSL and I
> suspect the XSL solution would be more elegant and extensible. Humble
cries
> for help from an XSL rookie.
>
> Best regards,
>
> ADK.
-
Re: Changing an XML document using XSL transformations
Hi, sincerest thanks to Russell Jones for his help. His suggestion does a
fantastic job of promoting the "Sub-Shot" nodes replacing the parent node
as necessary. I'm trying to get the template to work alongside another (mutually
exclusive) template which looks after the functionality for the "EXACT COPY"
of the other nodes from the original xml document. I cannot get the code
to apply Russell's template when necessary. Any suggestions as to what I'm
doing wrong, or an elegant solution?
<xsl:template match="/">
<xsl:choose>
<xsl:when test="name()='Shot'">
<xsl:apply-templates select="node()" mode="process_shots"/>
</xsl:when>
<xsl therwise>
<xsl:copy-of select="."/>
</xsl therwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Shot" mode="process_shots">
<xsl:choose>
<xsl:when test="count(child::Shot) > 0">
<xsl:copy-of select="child::Shot"/>
</xsl:when>
<xsl therwise>
<xsl:copy-of select="."/>
</xsl therwise>
</xsl:choose>
</xsl:template>
Best regards,
ADK.
"Russell Jones" <arj1@northstate.net> wrote:
>You need to test the Shot nodes to see if they contain child nodes using
the
>xsl:if or (more likely) the xsl:choose commands. To get the name of the
>context node, use the name() function, for example:
> <xsl:template match="Shot">
> <xsl:choose>
> <xsl:when test="count(child::Shot) > 0">
> <xsl:copy-of select="child::Shot"/>
> </xsl:when>
> <xsl therwise>
> <xsl:copy-of select="."/>
> </xsl therwise>
></xsl:choose>
></xsl:template>
>
>Russell Jones
>Sr. Web Development Editor
>DevX
>
>"adk" <adk@adv.sonybpe.com> wrote in message
>news:3ab5f6db$1@news.devx.com...
>>
>> Hi. I have an XML document that I want to perform an XSL transformation
on
>> and I'm not sure how to get the results I want. Let me give an example
of
>> the original and desired documents.
>>
>> Original XML:
>> <Material_Description Material_ID="MyVideo">
>> <Volume Material_ID="MyVideo">
>> <Label>MyVideoFootage</Label>
>> <Shot Material_ID="Shot1">
>> <In_Frame />
>> <Out_Frame />
>>
>> <Shot Material_ID="SubShot1a">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>>
>> <Shot Material_ID="SubShot1b">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>> </Shot>
>> <Shot Material_ID="Shot2">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>> <Volume>
>> </Material_Description>
>>
>> I'd like to use an XSL transformation to create an EXACT COPY of the
>original
>> XML document plus a modification in the XML node hierarchy so that if
a
>"SubShot"
>> exists, it replaces its "Shot" parent node.
>>
>> Desired XML:
>> <Material_Description Material_ID="MyVideo">
>> <Volume Material_ID="MyVideo">
>> <Label>MyVideoFootage</Label>
>> <Shot Material_ID="SubShot1a">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>> <Shot Material_ID="SubShot1b">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>> <Shot Material_ID="Shot2">
>> <In_Frame />
>> <Out_Frame />
>> </Shot>
>> <Volume>
>> </Material_Description>
>>
>> Everything I read about XSL transformations indicates that this should
be
>> possible, but I can't work out the syntax to achieve the desired result.
>> I believe I could solve this using a DOM, but I'm very new to XSL and
I
>> suspect the XSL solution would be more elegant and extensible. Humble
>cries
>> for help from an XSL rookie.
>>
>> Best regards,
>>
>> ADK.
>
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|