簡體   English   中英

具有mode屬性的XSLT 1.0模板

[英]XSLT 1.0 template with mode attribute

輸入XML:

<?xml version="1.0" encoding="utf-8"?>
<Root>
   <text>
      <body>
         <head>title1</head>
         <div n="1">
            <head>title2</head>
            <div n="1.1">
               <head>title3</head>
               <p>xyz</p>
               <p>xyz</p>
            </div>
            <div n="1.2">
               <head>title4</head>
               <p>xyz</p>
               <p>xyz</p>
               <div n="1.2.1">
                  <p>xyz</p>
                  <p>xyz</p>
               </div>
            </div>
         </div>
      </body>
   </text>
</Root>

我想的屬性“ID = 1”添加到所有<div>元素和屬性“電平= 0”到<div>不具有<div>子。

這是我實際的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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

<xsl:template match="body//div">
    <xsl:copy>
        <xsl:apply-templates select="node()[not(descendant::div)]" mode="level"/>
        <xsl:apply-templates select="node()[descendant::div]" mode="id"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="body//div[not(descendant::div)]" mode="level">
    <xsl:copy>
        <xsl:for-each select=".">
            <xsl:attribute name="id">1</xsl:attribute>
            <xsl:attribute name="level">0</xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="body//div[descendant::div]" mode="id">
    <xsl:copy>
        <xsl:for-each select=".">
            <xsl:attribute name="id">1</xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

帶有mode =“ id”的模板應匹配具有<div>子元素的<div>元素(添加屬性“ id”),具有mode =“ level”的模板應匹配不具有<div>子元素的元素(添加屬性“ id”和“水平”)。 但是由於某種原因,我得到了一些<div> (n = 1和n = 1.2.1的那些)沒有被任何模板處理。

實際輸出XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <text>
      <body>
         <head>title1</head>
         <div>
            title2
            <div id="1" level="0" n="1.1">
               <head>title3</head>
               <p>xyz</p>
               <p>xyz</p>
            </div>

            <div id="1" n="1.2">
               <head>title4</head>
               <p>xyz</p>
               <p>xyz</p>
               <div>
                  xyz
                  xyz
               </div>
            </div>
         </div>
      </body>
   </text>
</Root>

我很確定自己犯了一個奇怪的明顯錯誤(也許是在模式下使用模板?),但是到目前為止還沒有發現。 任何建議將不勝感激。 謝謝你們。

我不確定自己為什么首先使用模式。 下面的解決方案僅使用“通用”模板即可實現所需的功能。

具有模式的模板專門用於多次迭代樹,因此兩次訪問和處理節點兩次或更多次。

通常,很少中斷身份轉換。 這就是為什么下面的樣式表匹配

  • div子元素的div元素( div[div]
  • 沒有它們( div[not(div)] ),

應用最小的更改,然后使用apply-templates將其余的節點再次返回到身份轉換。

注意:這適合您顯示的輸入XML。 但是,要使其與您的實際輸入配合使用,您可能需要進行更改

<xsl:template match="div[div]">

<xsl:template match="div[descendant::div]">

取決於您的輸入結構。

樣式表

<?xml version="1.0" encoding="utf-8"?>

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

<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="div[div]">
  <xsl:copy>
     <xsl:attribute name="id">1</xsl:attribute>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="div[not(div)]">
  <xsl:copy>
     <xsl:attribute name="id">1</xsl:attribute>
     <xsl:attribute name="level">0</xsl:attribute>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

產量

<?xml version="1.0" encoding="UTF-8"?>
<Root>
 <text>
  <body>
     <head>title1</head>
     <div id="1">
        <head>title2</head>
        <div id="1" level="0">
           <head>title3</head>
           <p>xyz</p>
           <p>xyz</p>
        </div>
        <div id="1">
           <head>title4</head>
           <p>xyz</p>
           <p>xyz</p>
           <div id="1" level="0">
              <p>xyz</p>
              <p>xyz</p>
           </div>
        </div>
     </div>
  </body>
 </text>
</Root>

暫無
暫無

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

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