XML为XML的XSLT(删除,增加,改变)(XML to XML with XSLT (Remov

2019-10-31 23:45发布

我必须从一个XML(XHTML)文件转换到另一个使用XSLT。 转型的规则是:

  1. <div id="ta12" class="bl" style="dis:bl">必须与被替换<div class="pass" value="50">
  2. ID =“T0B”和“T1B”的值必须被使用id =“ta0b8”和“ta3b8”分别替换。
  3. <input type="radio" name="o0" id="t0"/>具有与被替换<input type="radio" name="key0b8" value="0" id="ta0q" class="block" />同样地在该文件)

输入文件:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>  
    <body>
      <div class="iDev">
      <div id="ta12" class="bl" style="dis:bl"></div>

        <div class="q">
          <div id="t0b" class="block">1<span style="color">TEXT1</span>
          </div><br />
          T <input type="radio" name="o0" id="t0"/> 
          F <input type="radio" name="op0" id="f0"/>
          <div id="sfb"></div>
        </div><br />

        <div class="q">
          <div id="t1b" class="block">2<span style="color">TEXT2</span>
          </div><br />
          T <input type="radio" name="o1" id="t1" /> 
          F <input type="radio" name="op1" id="f1" />
          <div id="sfb"></div>
        </div>
      </div>
    </body>
    </html>

输出文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8" />
</head>
<body>
  <div class="iDev">
  <div class="pass" value="50"></div>

    <div class="q">
      <div id="ta0b8" class="block">1<span style="color">TEXT1</span>
      </div><br />
      T<input type="radio" name="key0b8" value="0" id="ta0q" />
      F<input type="radio" name="key0b8" value="1" id="ta1q" />
      <div id="sfb"></div>
    </div><br />

    <div class="q">
      <div id="ta3b8" class="block">2 <span style="color">TEXT2</span>
      </div><br />
      T<input type="radio" name="key3b8" value="0" id="ta0q3" />
      F<input type="radio" name="key3b8" value="1" id="ta1q3" />
      <div id="sfb"></div>
    </div>
  </div>
</body>
</html>

在我的XSLT我已经创建了一个身份模板,其中包括整个输入文件,然后我试图做必要的修改。 我能够做的第一任务逐

<xsl:template match="xhtml:div[@id='ta12']">
  <xsl:attribute name="class">pa</xsl:attribute>
  <xsl:attribute name="value">10</xsl:attribute>
</xsl:template>

在输出它产生所需的Div标签但它消除了<div class="iDev">标记。 谁能告诉我要生产从给定输入所需的输出解决方案。 感谢您!

Answer 1:

我只是要解决您的第一条规则,因为这似乎是你的问题的焦点。 如果您需要规则2和3的帮助,让他们单独的问题,请。

一般来说,溶液XSLT 1.0图案复制的元件(非深)将如图以下。 通过非深,我的意思是将任何子节点。

清单1

<xsl:template match="some-element-pattern">
  <xsl:copy>
   <xsl:copy-of select="@*" />
   <xsl:attribute name="my-attrib-to-set">value-to-set</xsl:attribute>
  </xsl:copy>
</xsl:template>

或者,你可以更换的xsl:复制与XSL:申请模板,如果有其他的处理是直接拷贝一些潜在的其他属性。 XSL:申请模板是比较通用的形式。

因此,应用此解决方案模式,以你的情况,你想要的模板...

清单2

<xsl:template match="xhtml:div[@id='ta12']">
  <xsl:copy>
   <xsl:copy-of select="@*" />
   <xsl:attribute name="class">pa</xsl:attribute>
   <xsl:attribute name="value">10</xsl:attribute>
  </xsl:copy>
</xsl:template>

NB。 在以前的解决方案,我可以给你这种低效的模板...

清单3

<xsl:template match="xhtml:div[@id='ta12']">
  <xsl:copy>
   <xsl:apply-templates select="@*[not(@class)]" />
   <xsl:attribute name="class">pa</xsl:attribute>
  </xsl:copy>
</xsl:template>

我现在认识到这是错误的。 谓词不(@class)是没有意义的,并始终返回true,如当聚焦产品的属性的属性::轴总是返回一个空序列。 后面列表3中的思想,是消除在箱子类属性,其中匹配的输入div元素具有类属性的多余的处理。 如果你真的想获得这样的效率在上市2,在XSLT 1.0中,你可以做如清单4,但代码一些,什么丑,我不知道这是值得的。

清单4

<xsl:template match="xhtml:div[@id='ta12']">
  <xsl:copy>
   <xsl:apply-templates select="@*[local-name()!='class']
                                  [local-name()!='value']" />
   <xsl:attribute name="class">pa</xsl:attribute>
   <xsl:attribute name="value">10</xsl:attribute>
  </xsl:copy>
</xsl:template>

清单4只适用于类和值在空命名空间之中。 如果有问题的属性命名空间中的一些轮换是必需的。

这里是你最后的选择。 如果你准备牺牲的XSL一般性:副本,想吮吸属性值模板的多汁骨髓,你可以使用清单5.清单5,可以取代“啪”和“弗吉尼亚”与属性值模板, 按要求。 该解决方案的另一个缺点是,它现在已经成为必要的,不仅是一个效率的措施,以确保@class和@value属性不是孩子XSL内处理:应用模板,否则将过写意文字值。

清单5

<xsl:template match="xhtml:div[@id='ta12']">
  <xhtml:div class="pa" value="va">
   <xsl:apply-templates select="@*[local-name()!='class']
                                  [local-name()!='value']" />
  </xhtml:div>
</xsl:template>

最后,我知道你是只在XSLT 1.0感兴趣,但只是有点好玩,我会提到的解决方案的一般模式为XSLT 2.0。 这示于清单6。

清单6

<xsl:template match="some-element-pattern">
  <xsl:copy>
   <xsl:apply-templates select="@* except @my-attrib-to-set" />
   <xsl:attribute name="my-attrib-to-set">value-to-set</xsl:attribute>
  </xsl:copy>
</xsl:template>


文章来源: XML to XML with XSLT (Remove, add, alter)