The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> XML Programming
|
Text Between Elements
Discuss Text Between Elements in the XML Programming forum on Dev Shed. Text Between Elements XML Programming forum discussing XML and related technologies, including XUL and XSL. XML is a self-describing file format, designed for maximum compatibility between applications.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 15th, 2011, 08:32 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 4
Time spent in forums: 1 h 58 sec
Reputation Power: 0
|
|
|
Text Between Elements
Hi all,
This is my first post here...so please forgive my delinquencies. 
My problem is like this:
<parent>
The member is ordered to or retained on active duty, active service, or active state service under 10
<child1/>
<child2Start/>
USC
<child2End/>
688, 12301(a), 12301(g), 12302, 12304, or 12305, or under 14
<child1/>
<child2Start/>
USC
<child2End/>
331, 332, 359, 360, 367, or 712.
</parent>
This is the input XML. Now i have to get the text between 'child2Start' and 'child2End' and put it into a new element in the target.
Since, these two are different elements, which are closing there itself, the effect is that the text between them belongs to the element 'parent'.
Please suggest any solutions for this...as i have to do it in XSL. I tried using
<xsl:for-each select="parent::node()">
<xsl:value-of select="text()[1]"/>
but could not get the exact text 'between' two elements i.e. 'child2Start' and 'child2End' .
Thank you,
Sanket
|

June 19th, 2011, 05:15 AM
|
|
|
Code:
<?xml version="1.0"?>
<parent>The member is ordered to or retained on active duty, active service, or active state service under 10
<child1/>
<child2Start/>USC
<child2End/>688, 12301(a), 12301(g), 12302, 12304, or 12305, or under 14
<child1/>
<child2Start/>USC
<child2End/>331, 332, 359, 360, 367, or 712.</parent>
not use for-each think in template
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="parent"/>
</root>
</xsl:template>
<xsl:template match="parent">
<!--
<xsl:for-each select="node()">
<xsl:if test="text()">
<item>
<xsl:value-of select="concat(local-name(.),' ',.)"/>
</item>
</xsl:if>
</xsl:for-each>
-->
<xsl:apply-templates select="text()" mode="text"/>
</xsl:template>
<xsl:template match="text()" mode="text">
<!--
<nodebefore>
<xsl:value-of select="local-name(preceding-sibling::node()[1])"/>
</nodebefore>
-->
<xsl:if test="local-name(preceding-sibling::node()[1])='child2Start' and local-name(following-sibling::node()[1])='child2End'">
<text>
<xsl:value-of select="normalize-space(.)"/>
</text>
</xsl:if>
<!--
<nodeafter>
<xsl:value-of select="local-name(following-sibling::node()[1])"/>
</nodeafter>
-->
</xsl:template>
</xsl:stylesheet>
result
Code:
<?xml version='1.0' ?>
<root>
<text>USC</text>
<text>USC</text>
</root>
__________________
Helmut Hagemann Germany
fallen to the bottom of the facts?
I reach my hand and we go together
wer lesen und google kann ist klar im Vorteil
who read and google is able is clear in the advantage
|

June 19th, 2011, 07:59 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 4
Time spent in forums: 1 h 58 sec
Reputation Power: 0
|
|
|
thanks...it worked wonders!!
|

July 29th, 2011, 04:10 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 4
Time spent in forums: 1 h 58 sec
Reputation Power: 0
|
|
Thanks for the solution!!!
Now, i have little extra things to be captured
<parent>
The member is ordered to or retained on active duty, active service, or active state service under 10
<child1/>
<child2Start/>
USC
<child3>
<child4>234</child4>
<child5>234</child5>
</child3>
<child6Start/>
njdsnjvndj
<child6End/>
<child2End/>
688, 12301(a), 12301(g), 12302, 12304, or 12305, or under 14
<child1/>
<child2Start/>
USC
<child2End/>
331, 332, 359, 360, 367, or 712.
</parent>
i wanna capture the elements b/w <child2Start/> <child2End/>
i.e
1.<child3>
<child4>234</child4>
<child5>234</child5>
</child3>
2.<child6Start/>
njdsnjvndj
<child6End/>
Previous solution :
inside the template <xsl:template match="text()" mode="text">,
<xsl:apply-templates select="child::*"/> will not work
as it will have only the text of current node.
Please help me in this.
|

August 2nd, 2011, 12:34 AM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 4
Time spent in forums: 1 h 58 sec
Reputation Power: 0
|
|
|
a gentle reminder...
please help!!!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|