带模式属性的XSLT 1.0模板

时间:2014-01-15 13:50:36

标签: xml xslt xslt-1.0

输入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>

我想向所有<div>元素添加属性“id = 1”,为<div>添加属性“level = 0”且没有<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>

模板=“id”的模板应与<div>元素匹配<div>子元素(添加属性“id”),模板模式=“level”应匹配不带<div>子元素的元素(添加attribut“id”和“level”)。但由于某种原因,我得到了一些<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>

我很确定我犯了一些奇怪的错误(也许是在使用带模式的模板?),但到目前为止还没有发现它。任何建议将非常感谢。 谢谢大家。

1 个答案:

答案 0 :(得分:4)

我不完全确定为什么你首先使用模式。以下解决方案仅使用“常用”模板即可实现您的目标。

模式模板专门用于多次迭代树,因此访问和处理节点两次或更多次。

通常,尽可能中断身份转换。这就是下面的样式表匹配

的原因
  • 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>
相关问题