Matching children with namespace prefix
Here should go questions about transforming XML with XSLT and FOP.
Matching children with namespace prefix
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,' ',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,' ',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'	',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'"','^')"></xsl:variable>
<xsl:if test="string-length(normalize-space($vDesc)) > 0">
<xsl:value-of select="author"></xsl:value-of>
<xsl:text>	</xsl:text>
<xsl:value-of select="$vDesc"></xsl:value-of>
<xsl:text>	</xsl:text>
<xsl:apply-templates select="m:text"></xsl:apply-templates>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match = "m:text">
<xsl:text>	</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
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,' ',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,' ',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'	',' ')"></xsl:variable>
<xsl:variable name="vDesc" select="replace($vDesc,'"','^')"></xsl:variable>
<xsl:if test="string-length(normalize-space($vDesc)) > 0">
<xsl:value-of select="author"></xsl:value-of>
<xsl:text>	</xsl:text>
<xsl:value-of select="$vDesc"></xsl:value-of>
<xsl:text>	</xsl:text>
<xsl:apply-templates select="m:text"></xsl:apply-templates>
<xsl:text> </xsl:text>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match = "m:text">
<xsl:text>	</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
Re: Matching children with namespace prefix
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
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
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Re: Matching children with namespace prefix
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
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
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service