Matching children with namespace prefix

Here should go questions about transforming XML with XSLT and FOP.
kirkilj
Posts: 110
Joined: Fri May 14, 2010 12:14 am

Matching children with namespace prefix

Post by kirkilj »

Hi all,

I'm trying to parse an Adobe Acrobat PDF annotation file. This is my first transform, so forgive the ugliness. I'm just trying to get something to work.

Everything is working fine except for trying to match a particular child element that has a namespace prefix in its name.

Here's an excerpt of the source:

<item>
<title>Text Annotation by John Smith</title>
<guid>41-d4d32de6-0d69-4a60-afa2-9a987bfe9a88</guid>
<author>John Smith</author>
<description>yada yada</description>
<m:text xmlns:m="http://ns.adobe.com/xfdf/" color="#ffff00" creationdate="D:20100311155826-06'00'" date="D:20100311155850-06'00'" flags="print" icon="Comment" intent="Text" name="d4d32de6-0d69-4a60-afa2-9a987bfe9a88" opacity="1.000000" page="41" rect="464.074768,581.562805,484.074768,599.562805" reftype="R" subject="Sticky Note">
<m:popup open="yes" rect="612.000000,479.562805,792.000000,599.562805"/>
</m:text>
</item>

I'm pulling everything I need except for the "page" attribute which is part of the "m:text" element.

Here's my xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:m="http://ns.adobe.com/Acrobat/RSS/Reviews/">

<xsl:output method="text"></xsl:output>

<xsl:template match="/rss/channel">
<xsl:apply-templates select="item"></xsl:apply-templates>
</xsl:template>

<xsl:template match="item">
<xsl:if test="starts-with(title,'Text') or starts-with(title,'Highlight')">
<xsl:variable name="vDesc" select="substring-before(substring(description,287),'<')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'&#13;&#13;',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'&#13;',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'&#9;',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'&quot;','^')"></xsl:variable>

<xsl:if test="string-length(normalize-space($vDesc)) > 0">
<xsl:value-of select="author"></xsl:value-of>
<xsl:text>&#9;</xsl:text>
<xsl:value-of select="$vDesc"></xsl:value-of>
<xsl:text>&#9;</xsl:text>
<xsl:apply-templates select="m:text"></xsl:apply-templates>
<xsl:text>&#10;</xsl:text>
</xsl:if>
</xsl:if>
</xsl:template>

<xsl:template match = "m:text">
<xsl:text>&#9;</xsl:text>
<xsl:value-of select="@page"></xsl:value-of>
</xsl:template>

</xsl:stylesheet>

When I single-step in oXygen's debugger, 'select="m:text" is not executing its corresponding match template. Does it matter than the m:text element has no actual value, just attributes?

I know that I'm brute-forcing this, but elegance must come at a later date. Any suggestions, elegant or otherwise will be greatly appreciated.

Regards,

John
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Matching children with namespace prefix

Post by adrian »

Hello,

So far I see the wrong namespace:
XML: <m:text xmlns:m="http://ns.adobe.com/xfdf/" ...>
XSL: xmlns:m="http://ns.adobe.com/Acrobat/RSS/Reviews/"

Should be the same namespace if you want the match="m:text" to work.

And it also depends on the result of:
<xsl:if test="string-length(normalize-space($vDesc)) > 0">
I had to comment this because $vDesc was always empty.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
kirkilj
Posts: 110
Joined: Fri May 14, 2010 12:14 am

Re: Matching children with namespace prefix

Post by kirkilj »

Adrian,

Thanks so much. It works great now. I'm both glad and embarrassed that it was that simple. That's what a blind copy and paste will do to ya.

Moving on: The "description" element has HTML embedded within it as a value, replete with entities. I cheated by skipping over most of it by noticing the the actual semantic content started at location 287:

substring-before(substring(description,287),'<')

Would there have been a better way to accomplish the same thing by directly parsing and dealing with the entities?

John
Post Reply