Solved

xml / parse embedded tags ..

Posted on 2014-09-23
6
109 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 33
SVG, jquery, menu 14 60
[PHP] Hash a XML file/string 3 57
Trouble parsing soap xml result 3 26
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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

12 Experts available now in Live!

Get 1:1 Help Now