Solved

xml / parse embedded tags ..

Posted on 2014-09-23
6
131 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
[X]
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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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