English 中文(简体)
• 如何在xslt的刑期中为每一方的首封信建立联系?
原标题:how to create link for the first letter of each string in a sentence in xslt?

Say i have a variable <wish>Hi jony</wish> I have to traverse the wish element and i have to create link for first letter of the string in the element. output should be<a href="#H">H</a> <a href="#j">j</a>.

问题回答

我认为,这是你为:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="wish">
        <xsl:copy>
            <xsl:call-template name="link-first-letters">
                <xsl:with-param name="text" select="."/>
            </xsl:call-template>            
        </xsl:copy>
    </xsl:template>

    <xsl:template name="link-first-letters">
        <xsl:param name="text"/>

        <xsl:variable name="first-letter" select="substring($text, 1, 1)"/>

        <a href="#{$first-letter}"><xsl:value-of select="$first-letter"/></a>


        <xsl:if test="contains($text,    )">
            <xsl:text> </xsl:text>
            <xsl:call-template name="link-first-letters">
                <xsl:with-param name="text" select="substring-after($text,    )"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

Applied to this input document

<?xml version="1.0"?>
<root>
    <wish>Hi jony</wish>
</root>

它产生以下产出:

<root>
    <wish><a href="#H">H</a> <a href="#j">j</a></wish>
</root>

You need a recursive template to split the string, then build the elements. Here are the templates you can apply:

  <xsl:template match="Wish">
    <xsl:call-template name="links">
      <xsl:with-param name="text" select="." />
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="links">
    <xsl:param name="text" />
    <xsl:variable name="newtext" select="concat(normalize-space($text),    )" />
    <xsl:variable name="first" select="substring-before($newtext,    )" />
    <xsl:variable name="remaining" select="normalize-space(substring-after($newtext,    ))" />
    <xsl:element name="a">
      <xsl:attribute name="href">#<xsl:value-of select="substring($first, 1, 1)"/></xsl:attribute>
      <xsl:value-of select="substring($first, 1, 1)"/>
    </xsl:element>
    <xsl:if test="$remaining !=   ">
      <xsl:call-template name="links">
        <xsl:with-param name="text" select="$remaining" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>  

你走在正确的轨道上。 简明扼要地看待这一点:

<xsl:for-each select="tokenize($keyword,   )">
    <xsl:variable name="letter" select="substring(.,1,1)"/>
    <a href="{concat( # ,$letter)}">
        <xsl:value-of select="$letter"/>
    </a>
</xsl:for-each>

根据您的感受,我假定你正在使用2.0号特别提款法,而这一办法排除了沙波复方解决办法的需要。

www.un.org/spanish/ecosoc The XSLT 2.0 transformation:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="wish">
     <xsl:for-each select="tokenize(.,  W+ )">
      <xsl:variable name="vFirst" select="substring(.,1,1)"/>
      <a href="#{$vFirst}"><xsl:value-of select="$vFirst"/></a>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在应用XML文件时:

<wish>Hi jony</wish>

<>produces of the Hope result:

<a href="#H">H</a>
<a href="#j">j</a>





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签