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(., ' ', '')) = '' 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(., ' ', '')) = '' 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 />
沒有成功。
當你說「它不工作」,你的意思是你得到一個錯誤,或者它沒有輸出你所期望的?無論哪種情況,你能顯示當前的輸出嗎?謝謝! – 2013-04-29 11:10:03