[英]Move certain nodes that are sibling of select node into a new parent node
我在這里做了一些搜索,發現了一些與我的問題有關的問題,但我仍然遇到麻煩...(希望我可以添加一個新問題,而不是對現有問題進行評論......)
<?xml version="1.0"?>
<section>
<title>Main Section</title>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
我希望將/ section / para放在新創建的注釋節點中,如下所示:
<?xml version="1.0"?>
<section>
<title>Main Section</title>
<comment>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
</comment>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
我嘗試了一些我發現搜索stackoverflow的建議,最接近的是在這里。
這是我正在使用的樣式表:
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- paras that are children of a section that has direct para children and dircect section children -->
<xsl:template match="section[para][section]/para[1]">
<comment>
<xsl:apply-templates select="../para" mode="commentPara"/>
</comment>
</xsl:template>
<xsl:template match="*" mode="commentPara">
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match="section[para][section]/para"/>
</xsl:stylesheet>
它正在輸出:
<?xml version='1.0' ?>
<section>
<title>Main Section</title>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
只需刪除我要包含在注釋標記中的段落。 我嘗試在我鏈接到的問題中通過樣式表逐行進行...任何想法? 謝謝,bp
這是分組鄰道para
元素。
問題是,在解決沖突的模板規則 :因為兩者para
匹配規則具有相同的優先級進口和相同的計算優先級,錯誤恢復機制可能會導致應用最后一個。
您需要在“first para
child”規則中明確定義@priority
,如:
<xsl:template match="section[para][section]/para[1]" priority="1">
</
通過這樣的修改,輸出是:
<?xml version="1.0" encoding="UTF-16"?>
<section>
<title>Main Section</title>
<comment>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
</comment>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
我會這樣做 :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kPreceding" match="para"
use="generate-id(following-sibling::section[1])"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="section[preceding-sibling::para]">
<comment>
<xsl:apply-templates mode="copy"
select="key('kPreceding', generate-id())"/>
</comment>
<xsl:call-template name="identity"/>
</xsl:template>
<xsl:template match=
"para[following-sibling::section]"/>
<xsl:template match="para" mode="copy">
<xsl:call-template name="identity"/>
</xsl:template>
</xsl:stylesheet>
當此轉換應用於提供的XML文檔時:
<section>
<title>Main Section</title>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
產生了想要的正確結果:
<section>
<title>Main Section</title>
<comment>
<para>Here is some text that is a child of a main section.</para>
<para>Some more text.</para>
<para>When a section has subsections, it should not have loose paragraphs before the first sub section. Those loose paras should be placed inside a comment element.</para>
</comment>
<section>
<title>This is my subsection</title>
<para>Text that is inside of the sub-section</para>
<para>And some more sub section text.</para>
</section>
</section>
說明 :
身份規則(模板)“按原樣”復制每個節點。
為壓倒一切的模板section
有前置兄弟(S) para
包裝所有這些兄弟姐妹與comment
元素,然后調用自身的身份轉換。
為方便起見,我們定義了一個鍵,該鍵匹配section
元素之前的所有para
元素和給定的generate-id()
。
具有以下兄弟section
的para
元素通過覆蓋模板從身份規則的操作中排除,該模板根本不執行任何操作。
最后,當在包裝器comment
中輸出時,這樣的para
元素在模式copy
中處理,模式copy
只是調用身份規則來進行復制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.