你如何处理XSLT中的嵌入式XML标签? [英] How can you deal with embedded XML tags in XSLT?

查看:132
本文介绍了你如何处理XSLT中的嵌入式XML标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XSLT将XML转换为HTML。我无法弄清楚如何处理嵌入式XML节点的格式。例如,假设我有XML元素:

< favoriteMovie>< i>星球大战< / i>然而,在XLST期间,< i> < / code>标签被忽略,所以星球大战在HTML输出中不用斜体。有没有一个相对简单的方法来解决这个问题?



test.xml:

 <?xml version =1.0encoding =utf-8?> 
<?xml-stylesheet type =text / xslhref =test.html.xsl?>
< favoriteMovies>
< favoriteMovie>< i>星球大战< / i>佐贺< / favoriteMovie>
< / favoriteMovies>

test.html.xsl:

 <?xml version =1.0encoding =utf-8?> 
< xsl:output method =htmlomit-xml-declaration =yes/>
< xsl:template match =/>
< html>
< head />
< body>
< ul>
< li>< xsl:value-of select =。 />< /锂>
< / xsl:for-each>
< / ul>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>


解决方案


< i> 标记会忽略
,所以在HTML输出中,星球大战不是
斜体。是
有一个相对简单的方法来解决这个

你的问题在这里 strong>:


 < ul> 
< li>< xsl:value-of select =。/>< / li>
< / xsl:for-each>
< / ul>


< xsl:value-of> 指令用于创建文本节点。在这样做的时候,它复制到在这个XSLT指令的选择属性中指定的XPath表达式的字符串值。一个元素的字符串值是所有文本节点后代的串联。



所以这就是你如何得到报告的输出。



解决方案

使用 < xsl:copy-of> 指令,它复制在中选择属性中指定的所有节点:

 < UL> 
< li>< xsl:copy-of select =node()/>< / li>
< / xsl:for-each>
< / ul>

另一个解决方案,更符合XSLT原则避免使用<$

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

< xsl:template match =/>
< html>
< head />
< body>
< xsl:apply-templates />
< / body>
< / html>
< / xsl:template>

< xsl:template match =/ *>
< ul>
< xsl:apply-templates />
< / ul>
< / xsl:template>

< xsl:template match =favoriteMovie>
< li>< xsl:copy-of select =node()/>< / li>
< / xsl:template>
< / xsl:stylesheet>

当上述两个解决方案中的任何一个应用于提供的XML文档

 < favoriteMovies> 
< favoriteMovie>
< i>星球大战< / i> saga
< / favoriteMovie>
< / favoriteMovies>

想要的,正确的结果是


$ b

 < html> 
< head />
< body>
< ul>
< i>
< i>星球大战< / i>传奇
< / li>
< / ul>
< / body>
< / html>


I am using XSLT to convert XML to HTML. I am having trouble figuring out how to deal with embedded XML nodes for formatting. For example, let's say I have the XML element:

<favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>

However, during XLST, the <i> tag gets ignored, so "Star Wars" is not italicized in the HTML output. Is there a relatively simple way to fix this?

test.xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.html.xsl"?>
<favoriteMovies>
    <favoriteMovie>the <i>Star Wars</i> saga</favoriteMovie>
</favoriteMovies>

test.html.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes" />
    <xsl:template match="/">
      <html>
        <head />
          <body>
            <ul>
                <xsl:for-each select="favoriteMovies/favoriteMovie">
                    <li><xsl:value-of select="." /></li>
                </xsl:for-each>
            </ul>
          </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

解决方案

However, during XLST, the <i> tag gets ignored, so "Star Wars" is not italicized in the HTML output. Is there a relatively simple way to fix this?

Your problem is here:

<ul>
  <xsl:for-each select="favoriteMovies/favoriteMovie">
    <li><xsl:value-of select="."/></li>
  </xsl:for-each>
</ul>

The <xsl:value-of> instruction is used to create a text node. In doing so it copies to the output the string value of the XPath expression specified in the select attribute of this XSLT instruction. The string value of an element is the concatenation of all its text-node descendents.

So this is how you get the reported output.

Solution:

Use the <xsl:copy-of> instruction, which copies all the nodes that are specified in its select attribute:

<ul>
  <xsl:for-each select="favoriteMovies/favoriteMovie">
    <li><xsl:copy-of select="node()"/></li>
  </xsl:for-each>
</ul>

Another solution, more alligned with the principles of XSLT avoids using <xsl:for-each> at all:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <html>
    <head />
    <body>
     <xsl:apply-templates/>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="/*">
  <ul>
   <xsl:apply-templates/>
  </ul>
 </xsl:template>

 <xsl:template match="favoriteMovie">
  <li><xsl:copy-of select="node()"/></li>
 </xsl:template>
</xsl:stylesheet>

When any of the two solutions defined above are applied to the provided XML document:

<favoriteMovies>
    <favoriteMovie>the 
        <i>Star Wars</i> saga
    </favoriteMovie>
</favoriteMovies>

the wanted, correct result is produced:

<html>
    <head/>
    <body>
        <ul>
            <li>the 
                <i>Star Wars</i> saga
            </li>
        </ul>
    </body>
</html>

这篇关于你如何处理XSLT中的嵌入式XML标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆