从title标签中提取文本使用XSLT

时间:2017-02-07 12:01:37

标签: xml xslt

是否有可能从任务中的title标签中提取文本,并将div标签id作为id添加为小字母空格 -

我的输入xml:

<?xml version="1.0" encoding="UTF-8"?>
<topic>
<task>
<title class="- topic/title ">Working with new XML</title>
<taskbody class="- topic/body  task/taskbody ">
<section class="- topic/section ">
<title class="- topic/title ">Settings IVN</title>
<p class="- topic/p ">Repository location http:/google.com/heights</p>
</section>  
</taskbody>
</task>
<task>
<title class="- topic/title ">Clearing the Content</title>
<taskbody class="- topic/body  task/taskbody ">
<section class="- topic/section ">
<title class="- topic/title ">Delete the contents</title>
<p class="- topic/p ">From local directory</p>
</section>  
</taskbody>
</task>
</topic>

我使用过XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"
              encoding="UTF-8"
              indent="no"
              />
<xsl:variable name="convert" select="//task/title[@class='- topic/title ']"/>
<xsl:variable name="inputval" select="translate($convert, ' ', '_')"/>
<xsl:variable name="convert1" select="//section/title[@class='- topic/title ']"/>
<xsl:variable name="inputval1" select="translate($convert1, ' ', '_')"/>
<xsl:template match="task">
<xsl:text>
</xsl:text>
<div class="topic" id="{$inputval}">
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="task/title">
<h1>
<xsl:apply-templates/></script></h1>
</xsl:template>
<xsl:template match="topic">
<html>
<body>
<xsl:apply-templates/>
<body>
</html>
</xsl:template>
<xsl:template match="section">
<div>
<div class="section" id="{$inputval1}">
<xsl:apply-templates/>
</div>
</div>
</xsl:template>
<xsl:template match="title">
<h2 class="sectiontitle">
<xsl:apply-templates/>
</h2>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:apply-templates/>
</p>    
</xsl:template>

</xsl:stylesheet>

输出我得到第一个标题ID格式重复,id产生为:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
<div class="topic" id="Working_with_new_XML">
<h1>Working with new XML</h1>
<div>
<div class="section" id="Settings_IVN">
<h2 class="sectiontitle">Settings IVN</h2>
<p>Repository location http:/google.com/heights</p>
</div>
</div>
</div>
<div class="topic" id="Working_with_new_XML">
<h1>Clearing the Content</h1>
<div>
<div class="section" id="Settings_IVN">
<h2 class="sectiontitle">Delete the contents</h2>
<p>From local directory</p>
</div>
</div>
</div>
</body>
</html>

要求输出作为id作为特定标题,小写字母和空格作为 - 而id不重复,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
<div class="topic" id="working_with_new_xml">
<h1>Working with new XML</h1>
<div>
<div class="section" id="settings_ivn">
<h2 class="sectiontitle">Settings IVN</h2>
<p>Repository location http:/google.com/heights</p>
</div>
</div>
</div>
<div class="topic" id="clearing_the_content">
<h1>Clearing the Content</h1>
<div>
<div class="section" id="delete_the_contents">
<h2 class="sectiontitle">Delete the contents</h2>
<p>From local directory</p>
</div>
</div>
</div>
</body>
</html>

请帮助我。

1 个答案:

答案 0 :(得分:0)

将两个变量定义为:

<xsl:variable name="source-chars" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ '"/>
<xsl:variable name="target-chars" select="'abcdefghijklmnopqrstuvwxyz_'"/>

并将它们用作:

<xsl:template match="task">
    <div class="topic" id="{translate(title, $source-chars, $target-chars   )}">
        <xsl:apply-templates/>
    </div>
</xsl:template>

<xsl:template match="section">
    <div>
        <div class="section" id="{translate(title, $source-chars, $target-chars )}">
            <xsl:apply-templates/>
        </div>
    </div>
</xsl:template>

P.S。我认为您不需要样式表中的4个变量。