繁体   English   中英

XSLT 1.0:如何在其他模板周围包装XML元素?

[英]XSLT 1.0: how to wrap XML element around other templates?

我正在使用xsltproc在OS X Yosemite上处理XSLT 1.0转换; 输入是HTML,输出是XML。

这个想法是,下面匹配h1[@class='page-header']div[@class='mixins']的模板实际上可以工作,但是问题在于将它们包装在自定义父XML元素(这里称为dye )中。

我意识到我的模板匹配*已损坏; 它只是在这里说明我想输出的结构类型。

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

  <xsl:output indent="yes" method="xml"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|style|script"/>

  <xsl:template match="*">
    <xsl:element name="dye">
      <xsl:apply-templates select="h1[@class='page-header']"/>
      <xsl:apply-templates select="div[@class='mixins']"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="h1[@class='page-header']">
    <xsl:element name="color">
      <xsl:value-of select="text()"/>
    </xsl:element>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

  <xsl:template match="div[@class='mixins']">
    <xsl:element name="tone">
      <xsl:value-of select="p/a[@class='tone']/@href"/>
    </xsl:element>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

谢谢你的关注!

我相信这应该有效:

<xsl:template match="*">
  <dye>
    <xsl:apply-templates select="h1[@class='page-header']"/>
    <xsl:apply-templates select="div[@class='mixins']"/>
  </dye>
</xsl:template>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM