如何使用 XSL 从 XML 创建 XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1255372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:41:06  来源:igfitidea点击:

How create XML from XML using XSL?

xmlxsltasp-classic

提问by Alex

How create XML from XML using XSL ?

如何使用 XSL 从 XML 创建 XML?

I try like this.. but i not get a result

我像这样尝试..但我没有得到结果

Test.xml

测试文件

<Address>
  <name> Alex</name>
  <lastname>Mathew</lastname>
</Address>

Test.xsl

测试.xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<Address>
      <FirstName><xsl:value-of select="name" /></FirstName>
      <LastName><xsl:value-of select="lastname" /></LastName>
</Address>
</xsl:template>

</xsl:stylesheet>

I need out put like this

我需要像这样

<Address>
  <FirstName> Alex</FirstName>
  <LastName>Mathew</LastName>
</Address>

I try to convert in my asp page (test.asp)

我尝试在我的 asp 页面(test.asp)中进行转换

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("Test.xml"))

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("Test.xsl"))

'Response.Write(xml.transformNode(xsl))
'Response.ContentType = "text/plain; charset=UTF-8"

 Set doc = Server.CreateObject("Msxml2.DOMDocument.3.0")   
 doc.async = False  
 doc.loadXML(xml.transformNode(xsl))  

response.write xml.transformNode(xsl)

response.write doc.getElementsByTagName("FirstName").item(0).text
%>

Plz help me solve this problem

请帮我解决这个问题

回答by Tomalak

The problem is that "/"is the root, not the root element(or "document element").
Hierarchically, "/"is one level abovethe document element (<Address>, in yor case). So this:

问题是这"/",而不是根元素(或“文档元素”)。
在层次上,"/"是文档元素之上的一个级别(<Address>在你的情况下)。所以这:

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

  <xsl:template match="/Address">
    <Address>
      <FirstName><xsl:value-of select="name" /></FirstName>
      <LastName><xsl:value-of select="lastname" /></LastName>
    </Address>
  </xsl:template>
</xsl:stylesheet>

would actually work. Note the tiny little difference? Nicerwould be this:

实际上会起作用。注意到细微的差别了吗?更好的将是这样:

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

  <!-- the identity template (copies your input verbatim) -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- special templates only for things that need them -->
  <xsl:template match="name">
    <FirstName><xsl:value-of select="." /></FirstName>
  </xsl:template>

  <xsl:template match="lastname">
    <LastName><xsl:value-of select="." /></LastName>
  </xsl:template>

</xsl:stylesheet>

回答by mkoeller

You might also want to add an output directive in your stylesheet:

您可能还想在样式表中添加输出指令:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/Address">
  <Address>
    <FirstName><xsl:value-of select="name" /></FirstName>
    <LastName><xsl:value-of select="lastname" /></LastName>
  </Address>
</xsl:template>

</xsl:stylesheet>

This causes the output to have a leading xml declaration:

这会导致输出具有前导 xml 声明:

<?xml version="1.0" ?>

回答by Robert Rossney

Just to expand and clarify a bit on what Tomalak posted: the rootof an XML document is, in the DOM hierarchy, above the top-level element. It's exceptionally common to see the two confused. Consider this XML document:

只是为了扩展和澄清 Tomalak 发表的内容:XML 文档的在 DOM 层次结构中,位于顶级元素之上。看到两者混淆是非常常见的。考虑这个 XML 文档:

<!-- This is a node - yes, comments are nodes.  -->
<root>
   <child/>
</root>
<!-- This is also a node.  -->

The root of this document has three child nodes: a comment node, an element node, and another comment node. The top-level element is named root, because that's what everyone who creates XML instance documents does in order to perpetuate the confusion between the document root and the top-level element. (Especially if they're still at the point in their XML education where they use "node" when they mean "element.")

这个文档的根有三个子节点:一个注释节点、一个元素节点和另一个注释节点。顶级元素被命名为root,因为这就是创建 XML 实例文档的每个人所做的,以便使文档根和顶级元素之间的混淆永久存在。(特别是如果他们仍然在他们的 XML 教育中,当他们的意思是“元素”时,他们会使用“节点”。)

This gets us to one of the reasons that the template that Tomalak describes as "nicer" is nicer. If you extend the identity transform, the only thing that the XSLT will change in your document are the elements that you've built templates for. Every other node in the document is copied unchanged. So if your input document has comments around the top-level element, as in the above example, they won't get stripped out of the output, as they would if you simply implemented a template matching the Addresselement.

这让我们明白了 Tomalak 描述为“更好”的模板更好的原因之一。如果您扩展身份转换,那么 XSLT 将在您的文档中更改的唯一内容是您为其构建模板的元素。文档中的每个其他节点都被原样复制。因此,如果您的输入文档在顶级元素周围有注释,如上例所示,它们不会从输出中删除,就像您只是实现与Address元素匹配的模板一样。

Of course, if you wantyour output to exclude comments, that's easily accomplished too; just don't implement the identity transform.

当然,如果您希望您的输出排除注释,那也很容易实现;只是不要实现身份转换。