我有一个大的xml文件,其中包含许多自闭标签.如何使用XSLT删除所有这些.
例如.
<?xml version="1.0" encoding="utf-8" ?>
<Persons>
<Person>
<Name>user1</Name>
<Tel />
<Mobile>123</Mobile>
</Person>
<Person>
<Name>user2</Name>
<Tel>456</Tel>
<Mobile />
</Person>
<Person>
<Name />
<Tel>123</Tel>
<Mobile />
</Person>
<Person>
<Name>user4</Name>
<Tel />
<Mobile />
</Person>
</Persons>
Run Code Online (Sandbox Code Playgroud)
我期待结果:
<?xml version="1.0" encoding="utf-8" ?>
<Persons>
<Person>
<Name>user1</Name>
<Mobile>123</Mobile>
</Person>
<Person>
<Name>user2</Name>
<Tel>456</Tel>
</Person>
<Person>
<Tel>123</Tel>
</Person>
<Person>
<Name>user4</Name>
</Person>
</Persons>
Run Code Online (Sandbox Code Playgroud)
注意:有数千种不同的元素,如何以编程方式删除所有自闭标签.另一个问题是如何删除空元素等<name></name> .
谁可以帮我这个事?非常感谢.
我在使用XSL模板时遇到了一些奇怪的情况.它的大部分输出都很好,但是每个循环都会导致我出现问题.
这是XML:
<area>
<feature type="Hall">
<Heading><![CDATA[Hall]]></Heading>
<Para><![CDATA[Communal gardens, pathway leading to PVCu double glazed communal front door to]]></Para>
</feature>
<feature type="Entrance Hall">
<Heading><![CDATA[Communal Entrance Hall]]></Heading>
<Para><![CDATA[Plain ceiling, centre light fitting, fire door through to inner hallway, wood and glazed panelled front door to]]></Para>
</feature>
<feature type="Inner Hall">
<Heading><![CDATA[Inner Hall]]></Heading>
<Para><![CDATA[Plain ceiling with pendant light fitting and covings, security telephone, airing cupboard housing gas boiler serving domestic hot water and central heating, telephone point, storage cupboard housing gas and electric meters, …Run Code Online (Sandbox Code Playgroud) 这个问题实际上有些不同.请参阅@ Tomalak的回答以了解OP真正想要的内容.:(
有没有办法在一个数组中的for-each循环期间存储变量/ param,并在另一个模板中使用它,即<xsl:template match="Foundation.Core.Classifier.feature">.classname应存储for-each期间出现的所有值.你会如何在XSLT中实现它?这是我目前的代码.
<xsl:for-each select="Foundation.Core.Class">
<xsl:for-each select="Foundation.Core.ModelElement.name">
<xsl:param name="classname">
<xsl:value-of select="Foundation.Core.ModelElement.name"/>
</xsl:param>
</xsl:for-each>
<xsl:apply-templates select="Foundation.Core.Classifier.feature" />
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
这是classname应该使用参数的模板.
<xsl:template match="Foundation.Core.Classifier.feature">
<xsl:for-each select="Foundation.Core.Attribute">
<owl:DatatypeProperty rdf:ID="{Foundation.Core.ModelElement.name}">
<rdfs:domain rdf:resource="$classname" />
</owl:DatatypeProperty>
</xsl:for-each>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
是否有任何应用程序从XML文件生成XSL样式表?我有一个非常复杂的XML文件,它是一个结构良好的SVG文件.
有没有将XML转换为XSL的软件应用程序?
我已经在stackflow上查找了解决方案,但它们似乎都不适合我.这是我的问题.可以说我有以下文字:
资源:
<greatgrandparent>
<grandparent>
<parent>
<sibling>
Hey, im the sibling .
</sibling>
<description>
$300$ <br/> $250 <br/> $200! <br/> <p> Yes, that is right! <br/> You can own a ps3 for only $200 </p>
</description>
</parent>
<parent>
... (SAME FORMAT)
</parent>
... (Several more parents)
</grandparent>
</greatgrandparent>
Run Code Online (Sandbox Code Playgroud)
输出:
<newprice>
$300$ <br/> $250 <br/> $200! <br/> Yes, that is right! <br/> You can own a ps3 for only $200
</newprice>
Run Code Online (Sandbox Code Playgroud)
我似乎找不到办法做到这一点.
目前的XSL:
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="greatgrandparents">
<xsl:apply-templates /> …Run Code Online (Sandbox Code Playgroud) XML文件包含以下数据:
<AddtlStsRsnInf>/00000002/Level 2 Reject</AddtlStsRsnInf>
<AddtlStsRsnInf>The Transaction Reference Number is</AddtlStsRsnInf>
<AddtlStsRsnInf>not unique.</AddtlStsRsnInf>
Run Code Online (Sandbox Code Playgroud)
如何将来自所有三个标签的数据连接成一个变量?
感谢致敬,
基兰
我想获得以下输出:
<name><![CDATA[ SomeNameHere ]]></name>
Run Code Online (Sandbox Code Playgroud)
使用以下xslt:
<xsl:element name="name">
<xsl:cdata>
<xsl:value-of select="name"/>
</xsl:cdata>
</xsl:element>
Run Code Online (Sandbox Code Playgroud)
但是在编译时我得到以下异常:
'xsl:cdata' cannot be a child of the 'xsl:element' element.
Run Code Online (Sandbox Code Playgroud)
如何在xml节点中插入cdata元素呢?
谢谢.
基本上,我现在正在做的是运行XSLT,然后在Visual Studio中打开结果并执行查找和替换一个单词 - 对于此示例,我想将"bar"的所有实例更改为"myBar".可以假设"bar"的所有实例都在文本元素中,如下所示:
<someElement>bar.whatever</someElement>
Run Code Online (Sandbox Code Playgroud)
这将转变为:
<someElement>myBar.whatever</someElement>
Run Code Online (Sandbox Code Playgroud)
但需要注意的是,我还在运行其他转换,例如重命名或移动元素.有没有什么方法可以将这两个操作(转换和查找和替换)组合成一个XSLT?这可能是多次查找和替换吗?
所有帮助表示赞赏,并提前致谢!
编辑:来自评论
我应该指出我确实使用XSLT 2.0.我正在阅读那篇文章并试图找出如何使用replace()
我一直在尝试使用XSL迭代XML文件,但是由于唯一的第一语言被识别,我遇到了重大麻烦.救命!
<?xml version="1.0"?>
<languages count="1">
<language>English (UK)</language>
<language>Spanish</language>
<language>Arabic</language>
</languages>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<xsl:for-each select="languages">
<th>
<xsl:value-of select="language"/>
</th>
</xsl:for-each>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud) 好吧,我碰巧经历了一些代码片段,并且当使用css完成相同的事情时,无法理解xsl的需要.例如
<?xml-stylesheet type = "text/css" href = "6a.css" ?>
<ENG-COLLEGE>
<XYZ>
<USN>145456</USN>
<NAME>ABC</NAME>
<COLLEGE>TYUIT</COLLEGE>
<BRANCH>ISE</BRANCH>
<YEAR>2003</YEAR>
<EMAIL>abc@gmail.com</EMAIL>
</XYZ>
<ENG-COLLEGE>
Run Code Online (Sandbox Code Playgroud)
用于样式化的等效CSS代码
USN{font-family:'sans serif';color:orange;font-size:15pt;}
NAME{font-family:'arial';color:red;font-size:15pt;}
COLLEGE{font-family:'Times New Roman';color:lime;font-size:15pt;}
BRANCH{font-family:'Comic Sans MS';color:gray;font-size:15pt;}
YEAR{font-family:'Century Gothic';color:blue;font-size:15pt;}
EMAIL{font-family:'Georgia';color:green;font-size:15pt;}
Run Code Online (Sandbox Code Playgroud)
虽然使用xsl可以实现相同的样式,但涉及更多代码行
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns = "">
<xsl:template match = "ENG-COLLEGE">
<html><head><title>Test XSL</title>
</head><body bgcolor="lightgreen">
<center><h2>INFORMATION </h2>
<table border="1">
<tr><td>USN</td><td>NAME</td><td>COLLEGE</td><td>BRANCH</td><td>YEAR</td> <td>EMAIL</td></tr>
<xsl:for-each select ="VTU">
<tr>
<td> <xsl:value-of select = "USN" /></td>
<td><xsl:value-of select = "NAME" /></td>
<td><xsl:value-of select = "COLLEGE" /></td>
<td> <xsl:value-of select …Run Code Online (Sandbox Code Playgroud)