简体   繁体   English

使用XSLT解析XML?

[英]Parsing XML using XSLT?

How can I parse a XML document using XSLT? 如何使用XSLT解析XML文档? May be I am wrong, Is it possible to parse a XML document using XSLT? 可能是我错了,是否可以使用XSLT解析XML文档?

Here is XML document: 这是XML文档:

<?xml version="1.0" encoding="utf-8" ?>
<cart>
  <item id="1">
    <name>Tyres</name>
    <cost>20</cost>
  </item>
  <item id="2">
    <name>Front Glass</name>
    <cost>30</cost>
  </item>
  <item id="3">
    <name>Vanity Mirror</name>
    <cost>10</cost>
  </item>
  <item id="4">
    <name>Brake Pads</name>
    <cost>50</cost>
  </item>
  <item id="5">
    <name>Brake Oil</name>
    <cost>40</cost>
  </item>
</cart>

and the XSLT page: 和XSLT页面:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="shopping_cart">
      <root>
        <xsl:apply-templates select="cart"/>
      </root>        
    </xsl:template>
  <xsl:template match="cart">
    <cart>
      <xsl:attribute name="name">
        <xsl:value-of select="name"/>
      </xsl:attribute>
      <xsl:attribute name="cost">
        <xsl:value-of select="cost"/>
      </xsl:attribute>

    </cart>
  </xsl:template>
</xsl:stylesheet>

Any way to do that? 有什么办法吗? Please guide as I don't know much about parsing concept. 请指导,因为我对解析概念了解不多。

If there is only one XML document used by the stylesheet, the normal mechanism is to make it the "principal input document". 如果样式表仅使用一个XML文档,则通常的机制是将其设置为“主要输入文档”。 Depending on the API to your processor, you can typically supply the source document in either parsed or unparsed form; 根据处理器的API,通常可以以已解析或未解析的形式提供源文档。 if you supply it in unparsed form (for example as an InputStream) it will automatically be parsed and presented to the stylesheet as a tree of nodes. 如果以未解析的形式(例如,以InputStream的形式)提供它,它将被自动解析并以节点树的形式呈现给样式表。

If your stylesheet uses multiple documents, you can access (and parse) the secondary input documents using the document() function. 如果样式表使用多个文档,则可以使用document()函数访问(并解析)辅助输入文档。

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

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