2013-04-29 204 views
0

UPDATEXSLT将子女<span>注入<a>文字?

我已经修改了我的XSL是:

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:h="http://www.w3.org/1999/xhtml"> 
<output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></output> 
    <template match="/ | node() | @*"> 
     <copy> 
      <apply-templates select="node() | @*"></apply-templates> 
     </copy> 
    </template> 
    <template match="@class[.='cta-button-secondary']"> 
     <attribute name="class">cta-button secondary</attribute> 
     <element name="span" namespace="http://www.w3.org/1999/xhtml"> 
      <value-of select=".." /> 
     </element> 
    </template> 
</stylesheet> 

几乎达到我的需求除了<a>文字被复制。我想要在新子<span>中复制的锚文本,并且想要丢弃剩余的重复值。

有什么建议吗?


如何使用XSLT到<a>元素中注入周围的文本<span>

XLST:

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"> </output> 

    <template match="/ | node() | @*"> 
    <copy> 
     <apply-templates select="node() | @*"></apply-templates> 
    </copy> 
    </template> 

    <template match="*[  (self::br or self::p or self::div)  and  normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;  and  not(@*)  and  not(processing-instruction())  and  not(comment())  and  not(*[not(self::br) or @* or * or node()])  and  not(following::node()[not(  (self::text() or self::br or self::p or self::div)  and   normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos;  and   not(@*)  and   not(processing-instruction())  and   not(comment())  and   not(*[not(self::br) or @* or * or node()])  )])  ]"> 
    <!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks --> 
    </template> 

    <template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]"> 
    <!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space --> 
    <text> </text> 
    </template> 

    <template match="@class[.='cta-button-secondary']"> 
    <attribute name="class">cta-button cta-button-secondary</attribute> 
    </template> 

    <template match="a[@class(contains('cta-button'))]"> 
    <span> 
     <copy-of select="."/> 
    </span> 
    </template> 

</stylesheet> 

XML:

<Content xmlns="uuid:3f71252b-6e99-47f2-8906-ff4488c188a1"> 
    <step_title>Expand our impact</step_title> 
    <heading_line_1>Expand our impact</heading_line_1> 
    <title_emphasis>Line 1</title_emphasis> 
    <intro_text>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labasdsadore et dolore magna 
    aliqua.c</intro_text> 
    <expand_button_label>More about this</expand_button_label> 
    <body> 
    <h3 xmlns="http://www.w3.org/1999/xhtml">How we spent it</h3> 
    <ol xmlns="http://www.w3.org/1999/xhtml" class="ordered"> 
     <li>ordered list item 1</li> 
     <li>ordered list item 2</li> 
     <li>ordered list item 3</li> 
     <li>ordered list item 4</li> 
     <li>ordered list item 5</li> 
     <li>ordered list item 6</li> 
     <li>ordered list item 7</li> 
     <li>ordered list item 8</li> 
     <li>ordered list item 9</li> 
     <li>ordered list item 10</li> 
     <li>ordered list item 11</li> 
     <li>ordered list item 12</li> 
    </ol> 
    <p xmlns="http://www.w3.org/1999/xhtml">Thanks to the hard work of our supporters we increased what we spent on cancer services to a 
     record £105.9 million in 2011. That's £10 million more than in 2010.</p> 
    <p xmlns="http://www.w3.org/1999/xhtml">For a full breakdown of these charts, take a look at our <a href="#">Annual report and accounts 
     2011</a> or <a href="#" class="cta-button-secondary">Our 2011 achievements.</a></p> 
    </body> 
    <quote xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-13343" xlink:title="Quote2"/> 
    <right_column_image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="tcm:5-13350" 
    xlink:title="Wise with money graph"/> 
</Content> 

我希望能够有一个<span><copy-of select="."/></span>为这个孩子<template match="" >但它似乎并没有工作。

我已经研究过,但XSLT不是我熟悉的东西,并尝试了几种方法来实现这个我卡住了。我试过<apply-templates />,<value-of /><copy-of />没有成功。

+0

当你说“它不工作”,你的意思是你得到一个错误,或者它没有输出你所期望的?无论哪种情况,你能显示当前的输出吗?谢谢! – 2013-04-29 11:10:03

回答

0

样式表你已经书面声明了一个缺省名称空间,其URI为http://www.w3.org/1999/XSL/Transform,并且没有其他名称空间,因此您无法引用源数据中任何名称空间中的任何元素。

基本上,您需要指定样式表中使用的所有名称空间,否则您正在使用的元素名称将不会被识别。 a例如有一个命名空间

有三个命名空间的源文件中,一个在样式表(用于XSLT本身),所以您的转换应该像

<xsl:stylesheet version="1.0" 
    xmlns:src="uuid:3f71252b-6e99-47f2-8906-ff4488c188a1" 
    xmlns:htm="http://www.w3.org/1999/xhtml" 
    xmlns:xlink="http://www.w3.org/1999/xlink" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

启动之后,你可以参考HTML通过使用htm:div等与其他节点分开。

我试图整理XSLT,但代码很糟糕,我无法理解模板中应该包含的巨大match模式。我只想说,你应该能够通过使用

<xsl:template match="htm:a[contains(@class, 'cta-button')]"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*"/> 
    <htm:span> 
     <xsl:value-of select="."/> 
    </htm:span> 
    </xsl:copy> 
</xsl:template> 

我希望这有助于你想影响a元素相匹配。

+0

在那里我有XML为 'Anchor Text' 我想 'Anchor Text'。 此解决方案似乎在一个范围内移动了整个''元素,但我想要在''范围内但''值之外的''。 – 2013-04-29 11:33:19

+0

以上XSL几乎让我有,但锚元素缺少href属性和类别属性等,否则这工作。为什么不包含属性? – 2013-04-29 12:20:16

0

看起来你有点混淆命名空间。 你尽量避免XLST前缀与

xmlns="http://www.w3.org/1999/XSL/Transform" 

我不会做,但任何方式。
<a>标签是因为

<p xmlns="http://www.w3.org/1999/xhtml"> 
在XML

形式XHTML命名空间。

如果您尝试使用xslt作为默认命名空间,则必须为xhtml添加xhtml的命名空间前缀。

xmlns:h="http://www.w3.org/1999/xhtml" 

现在你可以用你的标签相匹配:

<template match="h:a"> 

而且跨度不能没有命名空间使用(因为它没有XLST)。您可以使用 :

<element name="span" namespace="http://www.w3.org/1999/xhtml"> 
     <copy-of select="."/> 
</element> 

但我会建议使用一个命名空间前缀的XSLT。

更新: 要让跨度大约只有一台带类属性,它包含了 'CTA-按钮' 使用:

<template match="h:a[@class[contains(.,'cta-button')]]"> 
    <element name="span" namespace="http://www.w3.org/1999/xhtml"> 
     <copy-of select="."/> 
    </element> 
</template> 

样式表应该是这样的:

<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" 
      xmlns:h="http://www.w3.org/1999/xhtml" 
      exclude-result-prefixes="h" 
      version="1.0"> 
+0

一旦你定义'xmlns:h',你可以使用'' – Borodin 2013-04-29 11:15:22

+0

'[@class [contains(。,'cta-button')]]'通常写成'[contains(@ class,'cta-按钮')]' – Borodin 2013-04-29 11:16:29

+0

@Borodin:谢谢,但使用''我无法抑制'h:'前缀。 – 2013-04-29 11:25:38