Solved

xml / parse embedded tags ..

Posted on 2014-09-23
6
119 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xslt copy xml document for each occurance of a repeating node 4 13
How to convertan Excel file into XML 16 33
Viewing XML as a table on a Mac 3 89
ASP.NET reading ATOM 2 51
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…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now