Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XSLForum: Main > XSL Coding General

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-17-2008, 08:25 AM
Siegfried Heintze
 
Posts: n/a

Default How to extract parent's attributes? How to use patterns to parse attribute's string value?



Here is what is in my source xml:

<g transform="translate( 40.00, 59.55)"><rect id="line 1277 rect"
title="0002 0001 scan 001 ` ~" width="12.5" height="12.5"
class="keyoutline" /> <text x=" 6" y="11" class="tch">巷</text><text
x=" 6" y="11" class="ru-up">Ȅ</text><text x=" 6" y="11"
class="ru-lo">ȅ</text>
</g>

How do I extract the 40.00 and 59.55 from the translate attribute for each
child that has an attribute [class='tch']?

Here is my attempt -- it is almost working. I get the text(), the @x and the
@y but I cannot get the sibling or parent attributes! What am I doing wrong?
I'm using saxon.

I guess this is a two part question. After I get the parent's transform
attribute, I want to extract the two numerical values from the translation.

Thanks!
Siegfried


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svg="http://www.w3.org/2000/svg"
version="2.0">

<xslutput indent="yes"/>
<xsl:template match="/">
<keys>
<xsl:apply-templates select="//svg:text[@class='tch']"/>
</keys>
</xsl:template>

<xsl:template match="//*[@class='tch']">
<xsl:element name="key">
<xsl:attribute name="x"><xsl:value-of select="@x"/></xsl:attribute>
<!-- works!-->
<xsl:attribute name="title"><xsl:value-of
select="../../svg:g[@title]"/></xsl:attribute> <!-- does not
work! -->
<xsl:attribute name="width"><xsl:value-of
select="../svg:text[@svg:width]"/></xsl:attribute> <!-- does not
work! -->
<xsl:attribute name="pos"><xsl:value-of
select="position()"/></xsl:attribute> <!-- this seems to give the
grandchild position and not the child position, I want the child
position -->

<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>


Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-17-2008, 12:15 PM
Martin Honnen
 
Posts: n/a

Default Re: How to extract parent's attributes? How to use patterns to parseattribute's string value?

Siegfried Heintze wrote:
> Here is what is in my source xml:
>
> <g transform="translate( 40.00, 59.55)"><rect id="line 1277 rect"
> title="0002 0001 scan 001 ` ~" width="12.5" height="12.5"
> class="keyoutline" /> <text x=" 6" y="11" class="tch">巷</text><text
> x=" 6" y="11" class="ru-up">Ȅ</text><text x=" 6" y="11"
> class="ru-lo">ȅ</text>
> </g>
>
> How do I extract the 40.00 and 59.55 from the translate attribute for each
> child that has an attribute [class='tch']?
>
> Here is my attempt -- it is almost working. I get the text(), the @x and the
> @y but I cannot get the sibling or parent attributes! What am I doing wrong?
> I'm using saxon.



> <xsl:template match="//*[@class='tch']">
> <xsl:element name="key">
> <xsl:attribute name="x"><xsl:value-of select="@x"/></xsl:attribute>
> <!-- works!-->
> <xsl:attribute name="title"><xsl:value-of
> select="../../svg:g[@title]"/></xsl:attribute> <!-- does not
> work! -->


It is not clear what you want to achieve above. Your select expression
.../../svg:g[@title] looks at svg:g children of the grandparent element
having a title attribute. If you want to output the title attribute
value then you need ../../svg:g[@title]/@title.
As your SVG sample above does not have any g element with a title
attribute I can't tell which expression you are looking for.

Also note that in XSLT 2.0 the xsl:attribute element allows a select
attribute so you can shorten e.g.
<xsl:attribute name="foo">
<xsl:value-of select="bar"/>
</xsl:attribute>
to e.g.
<xsl:attribute name="foo" select="bar></xsl:attribute>

> <xsl:attribute name="width"><xsl:value-of
> select="../svg:text[@svg:width]"/></xsl:attribute> <!-- does not
> work! -->


Again it is not clear what you want to achieve. The select expression
.../svg:text[@svg:width] select svg:text children of the parent which
have an svg:width attribute. As attributes in SVG are in no namespace I
think you rather want
../svg:text[@width]
or assuming you want to output an attribute value you want
../svg:text[@width]/@width

> <xsl:attribute name="pos"><xsl:value-of
> select="position()"/></xsl:attribute> <!-- this seems to give the
> grandchild position and not the child position, I want the child
> position -->


position() gives the position in the currently processed nodes. As your
xsl:apply-templates has select="//svg:text[@class='tch']" the currently
processed nodes are svg:text elements at all levels and position()
refers to the position in that sequence.
Look into xsl:number to get a different value:
http://www.w3.org/TR/xslt20/#numberi...ed-on-position


As for extracting the numbers in translate( 40.00, 59.55) you could use
xsl:analyze-string for that: http://www.w3.org/TR/xslt20/#analyze-string



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
  #3 (permalink)  
Old 10-17-2008, 05:51 PM
Siegfried Heintze
 
Posts: n/a

Default Re: How to extract parent's attributes? How to use patterns to parse attribute's string value?


>It is not clear what you want to achieve above. Your select expression
>../../svg:g[@title] looks at svg:g children of the grandparent element
>having a title attribute. If you want to output the title attribute value
>then you need ../../svg:g[@title]/@title.


.../../svg:g[@title]/@title

Does not work!
But
...//@title

Does work. Oops, I see the problem: I got my sibling mixed up with the
grand parent! Please forgive me: it was late at night!

Now this works:

.../svg:rect/@title

Great!

Now what about this transform attribute? This works:

.../@transform

It produces

<key x=" 6" y="11" title="0002 0003 scan 003 2 @"
transform="translate( 78.10, 59.05)"
width="12.5"
pos="5">牛</key>

Now how do I extract that 78.10 and 59.05 out of the translate function in
the transform attribute?

Thanks! You have been very helpful so far!

Siegfried


Reply With Quote
  #4 (permalink)  
Old 10-17-2008, 06:33 PM
Martin Honnen
 
Posts: n/a

Default Re: How to extract parent's attributes? How to use patterns to parseattribute's string value?

Siegfried Heintze wrote:

> Now how do I extract that 78.10 and 59.05 out of the translate function in
> the transform attribute?


As suggested, you can try to parse out those value with a regular
expression and xsl:analyze-string.

Here is an example that defines a function to be used:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs mf"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:mf="http://example.com/2008/mf"
version="2.0">

<xslutput indent="yes"/>
<xsl:template match="/">
<keys>
<xsl:apply-templates select="//svg:text[@class='tch']"/>
</keys>
</xsl:template>

<xsl:template match="*[@class='tch']">
<xsl:element name="key">
<xsl:variable name="parsed-translate"
select="mfarse-translate(parent::svg:g/@transform)"/>
<xsl:attribute name="x" select="$parsed-translate[1]"/>
<xsl:attribute name="y" select="$parsed-translate[2]"/>

<xsl:value-of select="text()"/>
</xsl:element>
</xsl:template>

<xsl:function name="mfarse-translate" as="xs:string*">
<xslaram name="translate" as="xs:string"/>
<xsl:variable name="dp" select="'[+\-]?\d+(\.\d+)?'"/>
<xsl:variable name="pattern" select="concat('translate\s*\(\s*(',
$dp, '),\s*(', $dp, ')\)')"/>
<xsl:message terminate="no" select="concat('pattern is &quot;',
$pattern, '&quot;')"/>
<xsl:analyze-string select="$translate"
regex="{$pattern}">
<xsl:matching-substring>
<xsl:sequence select="(regex-group(1), regex-group(3))"/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
</xsl:stylesheet>

Here is a link to the documentation again:
http://www.w3.org/TR/xslt20/#analyze-string


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
  #5 (permalink)  
Old 10-17-2008, 06:33 PM
Martin Honnen
 
Posts: n/a

Default Re: How to extract parent's attributes? How to use patterns to parseattribute's string value?

Martin Honnen wrote:

> <xsl:function name="mfarse-translate" as="xs:string*">
> <xslaram name="translate" as="xs:string"/>
> <xsl:variable name="dp" select="'[+\-]?\d+(\.\d+)?'"/>
> <xsl:variable name="pattern" select="concat('translate\s*\(\s*(',
> $dp, '),\s*(', $dp, ')\)')"/>
> <xsl:message terminate="no" select="concat('pattern is &quot;',
> $pattern, '&quot;')"/>


Remove that xsl:message if you use the code, it is just in there for
debugging the regular expression.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 12:05 PM.
Style Developed by Epic Designz