Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

xml / parse embedded tags ..

Posted on 2014-09-23
6
128 Views
Last Modified: 2014-09-24
The problem is best described with an attachment which is attached.

Many Thanks ..

Chris Coleman.
ee0.pdf
0
Comment
Question by:ChrisColeman
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40339385
replace the
<xsl:value-of select="."/>
in your named template

with
<xsl:copy-of select="node()" />
0
 

Author Comment

by:ChrisColeman
ID: 40340380
Well yes in principle, but perhaps I did not explain the problem particularly well.

In my attached XML the  nodes are <p>zzzzz<a href=''xx" ><xxxx<a>ssssss</p> etc, and your solution would certainly work for that.

But in the real XML of which there is a lot some of the paragraphs take the following format -

<z>zzzzz<a href=''xx" ><xxxx<a>ssssss</z>

i.e. They are not delimited with <p>  --- </p> there fore the delimiters will not translate into paragraphs.

I have found a 'dirty' way to do it by putting a marker (!sa!,!esa!)around the anchors and parsing out the string outside of the anchors and then reassembling the string to include the anchor tag.

<z>zzzzz!sa!<a href=''xx" ><xxxx<a>!ea!ssssss</z>

But that means editing all of my anchor tags which can be done but  its messy.

If there is a way to find the position of the anchor tag without using the markers I would be very happy?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 40340470
The problem is that you are using value-of on a node() that has child elements
You flatten the entire structure as a string.

Of course you don't need to put markers and all that. XSLT is sufficiently powerfull to rebuild the structure as it originally were

I made explicit templates for each node, and put a reconstructive (identity copy) template in a mode "inside-p"
Just make sure that you do an apply-templates in that mode for all nodes you want to keep the structure

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:template match="root">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="treatments">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="b0">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="p0">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="p">
        <xsl:element name="p">
            <!-- ID -->
            <xsl:attribute name="id"><xsl:value-of select="./@id"/></xsl:attribute>
            <!-- Name -->
            <xsl:attribute name="name"><xsl:value-of select="./@name"/></xsl:attribute>
            <!-- CLASS -->
            <xsl:attribute name="class"><xsl:value-of select="./@class"/></xsl:attribute>
            <!-- style -->
            <xsl:attribute name="style"><xsl:value-of select="./@style"/></xsl:attribute>
            <xsl:apply-templates select="node()" mode="inside-p"></xsl:apply-templates>
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="node()" mode="inside-p">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()" mode="inside-p"/>
        </xsl:copy>
    </xsl:template>
    
    
    
</xsl:stylesheet>

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40340472
And yes, we could help you a lot better if you asked precise questions
0
 

Author Comment

by:ChrisColeman
ID: 40341571
Yes my request was imprecise.

But your solution was impeccable and i am very grateful for that. I have been grappling with XSL for some time now bot find it a bit irksome; However the ''mode()" directive (?) has solved my problem and will allow me to do a lot more.

Many thanks.

<xsl:template name="paraBetter">
    <xsl:param name="do_paras" />
    <xsl:for-each select="$do_paras/p">
      <xsl:element name="p">
        <!-- ID -->
        <xsl:if test="./@id">
          <xsl:attribute name="id"><xsl:value-of select="./@id"/></xsl:attribute>
        </xsl:if>
        <!-- CLASS -->
        <xsl:if test="./@class">
          <xsl:attribute name="class"><xsl:value-of select="./@class"/></xsl:attribute>
        </xsl:if>
        <!-- Style -->
        <xsl:if test="./@style">
          <xsl:attribute name="style"><xsl:value-of select="./@style"/></xsl:attribute>
        </xsl:if>
        <!--
	        Possible Embedded Anchor.
	    -->
        <xsl:choose>
          <xsl:when test="./a">
            <xsl:apply-templates select="node()" mode="inside-p">
            </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="." />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:element>
    </xsl:for-each>
  </xsl:template>
  <!-- Allow Embedded Anchors ..  -->
  <xsl:template match="node()" mode="inside-p">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()" mode="inside-p"/></xsl:copy>
  </xsl:template>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 40341699
welcome
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question