我有一个字符串,表示我想要打印的非缩进XML.例如:
<root><node/></root>
Run Code Online (Sandbox Code Playgroud)
应成为:
<root>
<node/>
</root>
Run Code Online (Sandbox Code Playgroud)
语法突出显示不是必需的.为了解决这个问题,我首先转换XML以添加回车符和空格,然后使用pre标签输出XML.为了添加新行和空格,我编写了以下函数:
function formatXml(xml) {
var formatted = '';
var reg = /(>)(<)(\/*)/g;
xml = xml.replace(reg, '$1\r\n$2$3');
var pad = 0;
jQuery.each(xml.split('\r\n'), function(index, node) {
var indent = 0;
if (node.match( /.+<\/\w[^>]*>$/ )) {
indent = 0;
} else if (node.match( /^<\/\w/ )) {
if (pad != 0) {
pad -= 1;
}
} else if (node.match( /^<\w[^>]*[^\/]>.*$/ )) {
indent = 1;
} else {
indent = 0;
}
var padding = …Run Code Online (Sandbox Code Playgroud) 有一个HTML文件(我不控制其内容)有几个input元素都具有相同的固定id属性"search_query".文件的内容可以更改,但我知道我总是希望获得input带有id属性的第二个元素"search_query".
我需要一个XPath表达式来执行此操作.我尝试//input[@id="search_query"][2]但是这不起作用.以下是此查询失败的示例XML字符串:
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
<div>
<form>
<input id="search_query" />
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
请记住,上面只是一个示例,其他HTML代码可能完全不同,input元素可以出现在任何地方,没有一致的文档结构(除了我保证总会有至少两个inputid属性为"search_query").
什么是正确的XPath表达式?
在XSLT中有
<xsl:value-of select="expression"/>
Run Code Online (Sandbox Code Playgroud)
获取元素的值,但是有什么东西可以选择元素的标记名吗?
在这种情况下:
<person>
<!-- required stuff -->
<name>Robert</name>
<!-- optional stuff, free form for future extension.
Using XMLSchema's xsd:any -->
<profession>programmer</profession>
<hobby>photography</hobby>
</person>
<xsl:for-each select="person">
<xsl:tag-of select="."/> : <xsl:value-of select="."/>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
要获得这样的输出:
Run Code Online (Sandbox Code Playgroud)name : Robert profession : programmer hobby : photography
当然上面的XSLT不会编译因为
<xsl:tag-of select="expression"/>
Run Code Online (Sandbox Code Playgroud)
不存在.但是怎么办呢?
我知道有一些在线正则表达式评估器..非常有用,实时匹配.它们就像RegexBuddy的Web应用程序.
我想知道xPath选择器是否有类似的东西?我只是在学习它们,这对我来说很有价值.
是否有一个在线测试器,允许您输入XML,然后是xPath选择器和匹配(现场会更好,但我怀疑有人编写了JavaScript解释器吗?)吗?
谢谢
我是XSLT的新手,无法解决以下代码出错的问题.
<xsl:variable name="var" select="boolean('false')"/>
<xsl:if test="$var'">variable is true</xsl:if>
Run Code Online (Sandbox Code Playgroud)
当它意味着虚假时,它总是返回真实.为什么?
嗨,我已经执行了一个转换,如果它是null,则删除一个标记.
我想检查我的转换是否正常工作,所以不是手动检查,而是编写了一个XSLT代码,只检查OUTPUT XML中是否存在该特定标记,如果它为null,则第二个XSLT应该输出一个文字"发现".(我实际上并不需要一些XML类型的输出,但我只是使用XSLT进行搜索.)
当我尝试使用这个XSL代码::
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/SiebelMessage//SuppressCalendar[.!='']">
FOUND
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
它输出XML文件中存在的所有TEXT DATA,
为了避免这种情况,我不得不写下这段代码::
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/SiebelMessage//SuppressCalendar[.!='']">
FOUND
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
为什么以前的代码输出TEXT,为什么我要坚持XSL忽略所有其他文本?是所有XML解析器的行为或仅我自己的行为(我正在使用msxml解析器).
在
<xsl:template name="temp_name" mode="mode">
Run Code Online (Sandbox Code Playgroud)
是什么意思mode?我搜索了很多资源,但我找不到这方面的例子.那么有人可以用一个例子解释一下吗?提前致谢.
我有很多XML文件,它们具有以下形式:
<Element fruit="apple" animal="cat" />
Run Code Online (Sandbox Code Playgroud)
我想从文件中删除.
使用XSLT样式表和Linux命令行实用程序xsltproc,我该怎么做?
到目前为止,在脚本中我已经有了包含我想要删除的元素的文件列表,因此单个文件可以用作参数.
编辑:这个问题原本缺乏意图.
我想要实现的是删除整个元素"元素",其中(fruit =="apple"&& animal =="cat").在同一文件中有许多名为"元素"的元素,我希望这些元素保留下来.所以
<Element fruit="orange" animal="dog" />
<Element fruit="apple" animal="cat" />
<Element fruit="pear" animal="wild three eyed mongoose of kentucky" />
Run Code Online (Sandbox Code Playgroud)
会成为:
<Element fruit="orange" animal="dog" />
<Element fruit="pear" animal="wild three eyed mongoose of kentucky" />
Run Code Online (Sandbox Code Playgroud) 我有一个XML文档,我想更改其中一个属性的值.
首先,我使用以下方法复制从输入到输出的所
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
现在我想"type"在任何名为的元素中更改属性的值"property".
我只是在学习XML以及如何使用XSL文件.在XSL文件中,我发现了以下术语:
xsl:template match="/"
Run Code Online (Sandbox Code Playgroud)
这代表什么?我可以用什么而不是/?我可以写table或任何其他HTML标签而不是/?