简体   繁体   English

使用XSLT转换内联XML元素

[英]Transform inline XML elements with XSLT

Does anyone know what XSLT to use to transform the following inline XML elements into the corresponding HTML? 有谁知道使用什么XSLT将以下内联XML元素转换为相应的HTML?

XML: XML:

<line><b c="foo1" /> bar <b c="foo2" /> bar <b c="foo3" /> bar</line>

HTML: HTML:

<p><span class="x">foo1</span> bar <span class="x">foo2</span> bar <span class="x">foo3</span> bar </p>
  1. 'line' becomes 'p' '线'变成'p'
  2. each 'b' becomes a 'span' with a class 'x' that I will provide 每个“ b”将成为一个“跨度”,我将提供一个“ x”类
  3. the contents of each span will be the 'c' attribute of the corresponding 'b' 每个范围的内容将是相应“ b”的“ c”属性

I can iterate over each 'line' in my file, and I can iterate over each 'b' in each line, however in the output the entire text content of 'line' is output then the attributes are appended after the text. 我可以遍历文件中的每个“行”,也可以遍历每行中的每个“ b”,但是在输出中将输出“ line”的整个文本内容,然后将属性附加在文本之后。 Here is the code I am using. 这是我正在使用的代码。 I understand why the following code does not do what I want. 我了解以下代码为何无法满足我的要求。 I just don't know how to write the XSLT to do what I want. 我只是不知道如何编写XSLT来完成我想要的事情。

<xsl:for-each select=".../line">
  <p>
    <xsl:value-of select="text"/>
    <xsl:for-each select="text/b">
      <span class="x">
        <xsl:value-of select="@c"/>
      </span>
    </xsl:for-each>
  </p>
</xsl:for-each> 
<xsl:template match="line">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match="b[@c]">
  <span class="x">
    <xsl:value-of select="@c"/>
  </span>
</xsl:template>

should suffice (as text nodes are copied by the built-in templates). 应该足够了(因为文本节点是由内置模板复制的)。

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

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