我在一篇技术论文中读过,使用XSLT在XML文件中进行修改比使用像DOM,JDOM,SAX,JSoup等XML解析器更有效,更可靠.所以我想知道XSLT是哪些参数比XML解析器更有效,更可取.(XSLT是否提供了更通用,更简单的解决方案?)感谢您.
例如:如果我有一个XML文件:
<node1>
<node2> TEXT </node2>
</node1>
Run Code Online (Sandbox Code Playgroud)
我需要输出为:
<node1>
<node2> TEXT </node2>
<script src="xyz.js"></srcipt>
</node>
Run Code Online (Sandbox Code Playgroud)
然后,我可以为此编写一个XSLT以获得所需的输出,或者我可以使用XML解析器(DOM,SAX等)编写Java程序以在所需位置插入所需元素并获得所需的输出.因此,我已经了解到XSLT被认为更有用和更有效.
用于计算xml缩进深度的最简单的xpath 2.0是什么?我的变体还不聪明:
<xsl:param name="maxdepth" select="number(substring(concat(
'16'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'15'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'14'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'13'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'12'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'11'[count(current()/*/*/*/*/*/*/*/*/*/*/*/*)>0],
'10'[count(current()/*/*/*/*/*/*/*/*/*/*)>0],
'09'[count(current()/*/*/*/*/*/*/*/*/*)>0],
'08'[count(current()/*/*/*/*/*/*/*/*)>0],
'07'[count(current()/*/*/*/*/*/*/*)>0],
'06'[count(current()/*/*/*/*/*/*)>0],
'05'[count(current()/*/*/*/*/*)>0],
'04'[count(current()/*/*/*/*)>0],
'03'[count(current()/*/*/*)>0],
'02'[count(current()/*/*)>0],
'01'[count(current()/*)>0])
,1,2)
)"/>
Run Code Online (Sandbox Code Playgroud) 这是XML文档.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:v="urn:schemas-microsoft-com:vml">
<w:body>
<w:p>para1</w:p>
<w:tbl>table data 1</w:tbl>
<w:p>para2</w:p> <!-- have to check whether this node is w:tbl or not with respect to the context node below-->
<w:tbl>table data 2</w:tbl> <!-- Assume this is context node -->
</w:body>
</w:document>
Run Code Online (Sandbox Code Playgroud)
所以,我想要查询的第一前同辈[ <w:p>para2</w:p>]所述的上下文节点[ <w:tbl>table data 2</w:tbl>]是<w:tbl>或没有.
请指导我摆脱这个问题......
我想在XML文件中使用html标签,所以在我的xsd文件中,我允许html格式化:
<xs:element name="description">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="lax"
minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
在我的XML文件中
<description>
<p xmlns="http://www.w3.org/1999/xhtml"> This course is an introduction to the information technologies required for secure, practical information systems for electronic commerce.
Topics will be chosen from areas such as document representation (XML, DTDs, XML Schema, XSLT, CSS),
</p> security (encryption, public key, symmetric key,
PKI, authentication); kinds of attack and vulnerabilities, electronic trading (spontaneous, deliberative, auctions), electronic document management
(metadata, search, digital libraries, management …Run Code Online (Sandbox Code Playgroud) 仅在Internet Explorer 10中,我收到转换从AJAX响应返回的XML的错误:
Object不支持属性或方法'transformNode'
这是我的代码:
function transformXML(xmlUrl, xsl) {
$.ajax({
type: 'GET',
url: xmlUrl,
success: function (xml, status, xhr) {
// cross-browser logic omitted for simplicity
xml.transformNode(xsl);
},
dataType: 'xml'
});
}
Run Code Online (Sandbox Code Playgroud)
这适用于IE7 - IE9.IE10有什么问题?
我正在学习xslt.我试图了解一些xslt代码,但我没有得到以下代码行的含义:
<xsl:variable name="Product" select="document('ProductList.xml')/node()[1]/node()[2]/node()[2]/node()[2]"/>
Run Code Online (Sandbox Code Playgroud)
我可以理解变量将是"$ Product",但是我无法理解文档函数之后select属性的值,'/'和nodes()[].我有c#和java的编码知识,我不熟悉这种语法.我想知道这些'/'在选择值中意味着什么.
出于某种原因,我的下面的代码给出了例外: javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
public String removePrettyPrint(String xml) throws TransformerException, TransformerFactoryConfigurationError {
String result = "";
TransformerFactory factory = TransformerFactory.newInstance();
String source = "<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"> <xsl:output indent=\"no\" /> <xsl:template match=\"@*|node()\"> <xsl:copy> <xsl:apply-templates select=\"@*|node()\"/> </xsl:copy> </xsl:template></xsl:stylesheet>";
Source xslt = new StreamSource(source);
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(xml);
transformer.transform(text, new StreamResult(result));
return result;
}
Run Code Online (Sandbox Code Playgroud)
这有什么问题?
在我的XML中,我有章节的章节,我需要生成一个目录,以便:
<chapter id="1"><title>Chapter 1</title><p>text</p></chapter>
<chapter id="2"><title>Chapter 2</title><p>text</p></chapter>
Run Code Online (Sandbox Code Playgroud)
转换为
<!-- Table Of Contents -->
<div class="contents">
<ul>
<li><a href="#1">Chapter 1</a></li>
<li><a href="#2">Chapter 2</a></li>
</ul>
</div>
<!-- Actual Content -->
<div class="chapter" id="1"><p>text</p></div>
<div class="chapter" id="2"><p>text</p></div>
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试使用xsl:for-each生成目录时,实际的章节似乎从输出中消失了.我该如何解决这个问题?
我有一个表示日期的字符串,格式如此
2010-12-03 ="yyyy-mm-dd"
我想使用XSLT并找到一种方法来提取月份并获得其月份缩写.
改造后,它应该是" DEC ",因为12月是第12个月.
谢谢!
我正在写一个xpath,我编写了xpath,它匹配文档树中的一些节点
但是我想要匹配所有其他元素,所以我想在整个文档的上下文中反转选择
所以我们假设我们有以下xpath
//*[@id='menu']//*
Run Code Online (Sandbox Code Playgroud)
和文件
<body>
<div>
<h1>title</h1>
</div>
<div>
<div id="menu">
<UL>
<LI>home</LI>
<LI>about</LI>
</UL>
</div>
<div id="sidebar">
<ul>
<li>one</li>
<li>two</li>
</ul>
</div>
<div>
</body>
Run Code Online (Sandbox Code Playgroud)
所以上层节点是匹配的,但我想要实现的是标记所有小写节点.
谢谢你的帮助