 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 2nd, 2003, 01:39 PM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SQL "like" equivalent in XSLT
I would like to apply the same template for Heading1, Heading2, Heading3, Heading4, Heading5
Is there a way of matching these that is similar to the "LIKE" syntax in SQL?
For example:
In SQL I would use: WHERE nodeName LIKE "Heading%"
In XSLT I would like to use: <xsl:template match="Heading%">
Is this possible other than stringing all of the names together with OR?
Thanks in advance for any help!
Aaron
|
|

December 3rd, 2003, 01:38 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Unfortunately, it's impossible. It's also impossible to use contains() function, since in patterns you can use only key() and id() functions. However, the expressiveness of patterns is powerful enough, so try to use anything which is common for these nodes(not only the node types as you want); for example, may be they are children of nodes having the same node type? Try to find any commonality for them from the structural point of view. You may send us your XML source and we'll try to find a concrete solution to your problem.
Regards,
Armen
|
|

December 3rd, 2003, 09:50 AM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Below is a snippet of the source XML I am working with. This is basically the native XML that OpenOffice.org saves text documents in. What I need to to is transform the OOo XML into our own XML. Right now I am trying to tackle a specific rule: "Headings always start a new section. Everything after a heading is part of that section." Since I am very limited on how much I can manipulate the XML that OOo creates, I am force to match between sibling nodes( http://p2p.wrox.com/topic.asp?TOPIC_ID=6884). I have successfully done this, but now I need to make it work for any headings that are encountered.
This is the template that I currently have. It works beautifully, but I need it to match any heading, not just Heading1:
Code:
<xsl:template match="text:p[@text:style-name = 'Dog_Heading1']">
<Dog_Section>
<Dog_ID/>
<Dog_Heading1><xsl:value-of select="."/></Dog_Heading1>
<xsl:apply-templates select="following-sibling::*[generate-id(following-sibling::text:p[@text:style-name = 'Dog_Heading1'][1]) = generate-id(current()/following-sibling::text:p[@text:style-name = 'Dog_Heading1'][1])]"/>
</Dog_Section>
</xsl:template>
Code:
<office:body>
<text:p text:style-name="Dog_ChapterNumber">1</text:p>
<text:p text:style-name="Dog_ChapterTitle">The 2002 Election Surprise</text:p>
<text:section text:style-name="Sect1" text:name="Introduction">
<text:p text:style-name="Dog_IntroductionLabel">Introduction</text:p>
<text:p text:style-name="Dog_InlineHeading">Peverill Squire</text:p>
<text:p text:style-name="Dog_Normal">The news media ...</text:p>
<text:p text:style-name="Dog_Normal">These characterizations ...</text:p>
<text:p text:style-name="Dog_Normal">The ...</text:p>
<text:p text:style-name="Dog_Normal">But are the...</text:p>
</text:section>
<text:p text:style-name="Dog_Heading1">Electoral Prospects</text:p>
<text:p text:style-name="Dog_Normal">Most observers expected ...</text:p>
<text:p text:style-name="Dog_Heading2">The Senate</text:p>
<text:p text:style-name="Dog_Normal">Heading into the ...</text:p>
<text:p text:style-name="Dog_Normal">Democrats believed ...</text:p>
<text:p text:style-name="Dog_Normal">Republicans had ...</text:p>
<text:p text:style-name="Dog_Heading2">The House of Representatives</text:p>
<text:p text:style-name="Dog_Normal">At first glance, the ...</text:p>
<text:p text:style-name="Dog_Normal">In fact, however,...</text:p>
<text:p text:style-name="Dog_Normal">Why would ...</text:p>
<text:p text:style-name="Dog_Normal">More important,...</text:p>
<text:p text:style-name="Dog_Normal">The outcome of ...</text:p>
<text:p text:style-name="Dog_Normal">Ironically, the ...</text:p>
<text:p text:style-name="Dog_Normal">The net effect ...</text:p>
<text:p text:style-name="Dog_Heading2">State Houses</text:p>
<text:p text:style-name="Dog_Normal">Gubernatorial ...</text:p>
<text:p text:style-name="Dog_Heading3">The Issues</text:p>
<text:p text:style-name="Dog_Normal">Factors such as ...</text:p>
<text:p text:style-name="Dog_Normal">On the other hand,...</text:p>
<text:p text:style-name="Dog_Normal">In the end, neither party ...</text:p>
<text:p text:style-name="Dog_Heading1">Election Results</text:p>
<text:p text:style-name="Dog_Normal">What the 2002 elections ...</text:p>
<text:p text:style-name="Dog_Heading2">An Overview</text:p>
<text:p text:style-name="Dog_Normal">On one level, the 2002 ...</text:p>
<text:p text:style-name="Dog_Normal">On another level, the ...</text:p>
<text:p text:style-name="Dog_Normal">Perhaps the biggest ...</text:p>
</office:body>
|
|

December 3rd, 2003, 11:04 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, your XML doc is flat indeed, so it's hard to find structural commonalities. Let's make a trick!
Code:
...
<xsl:apply-templates select="/office:body/text:p[starts-with(@text:style-name, 'Dog_Heading')]" mode="headings"/>
...
<xsl:template match="*" mode="headings">
<xsl:variable name="curr-name" select="@text:style-name"/>
<Dog_Section>
<Dog_ID/>
<xsl:element name="{$curr-name}"><xsl:value-of select="."/></xsl:element>
<xsl:apply-templates select="following-sibling::*[generate-id(following-sibling::text:p[@text:style-name = $curr-name][1]) = generate-id(current()/following-sibling::text:p[@text:style-name = $curr-name][1])]" mode="default"/>
</Dog_Section>
</xsl:template>
I didn't test it, please make necessary changes and test it: I think it should work.
Regards,
Armen
|
|

December 3rd, 2003, 11:27 AM
|
|
Authorized User
|
|
Join Date: Nov 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Armen,
I think you indirectly answerd the original question. starts-with() can be used as a "LIKE" equivalent. I was able to do what I wanted with this template:
Code:
<xsl:template match="text:p[starts-with(@text:style-name, 'Dog_Heading')]">
<xsl:variable name="curr-name" select="@text:style-name"/>
<Dog_Section>
<Dog_ID/>
<xsl:element name="{$curr-name}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:apply-templates select="following-sibling::*[generate-id(following-sibling::text:p[starts-with(@text:style-name, 'Dog_Heading')][1]) = generate-id(current()/following-sibling::text:p[starts-with(@text:style-name, 'Dog_Heading')][1])]"/>
</Dog_Section>
</xsl:template>
Thanks again for you help, I would be lost without your expertise!
Aaron
|
|

December 4th, 2003, 02:11 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You've found the simplest solution :)
Yes of course, we are free to use any function in predicate expressions (restriction is only on the pattern location paths).
By the way, you also can use contains() function which is like "WHERE field LIKE '%Heading%'" clause in SQL.
Regards,
Armen
|
|
 |