Solved

xml / parse embedded tags ..

Posted on 2014-09-23
6
129 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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