簡體   English   中英

將選擇節點的兄弟節點移動到新的父節點

[英]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>

說明

  1. 身份規則(模板)“按原樣”復制每個節點。

  2. 為壓倒一切的模板section有前置兄弟(S) para包裝所有這些兄弟姐妹與comment元素,然后調用自身的身份轉換。

  3. 為方便起見,我們定義了一個鍵,該鍵匹配section元素之前的所有para元素和給定的generate-id()

  4. 具有以下兄弟sectionpara元素通過覆蓋模板從身份規則的操作中排除,該模板根本不執行任何操作。

  5. 最后,當在包裝器comment中輸出時,這樣的para元素在模式copy中處理,模式copy只是調用身份規則來進行復制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM