简体   繁体   English

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

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

I am using xsltproc to process XSLT 1.0 transform on OS X Yosemite; 我正在使用xsltproc在OS X Yosemite上处理XSLT 1.0转换; input is HTML, and output is XML. 输入是HTML,输出是XML。

The idea is that the templates below matching h1[@class='page-header'] and div[@class='mixins'] actually work, but the problem is wrapping them in a custom parent XML element (here called dye ). 这个想法是,下面匹配h1[@class='page-header']div[@class='mixins']的模板实际上可以工作,但是问题在于将它们包装在自定义父XML元素(这里称为dye )中。

I realize my template matching * is broken; 我意识到我的模板匹配*已损坏; it's there simply to illustrate the kind of structure I would like to output. 它只是在这里说明我想输出的结构类型。

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

Thanks for your interest! 谢谢你的关注!

I believe this should work: 我相信这应该有效:

<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