对 XML 应用 XSLT 时是否可以在 XML 中保留注释?
示例(来源):
<rootNode>
<!-- My comment --><childElement>5</childElement>
</rootNode>
Run Code Online (Sandbox Code Playgroud)
转换后的样本结果为:
<newRoot>
<!-- My comment --><newChildElement>5</newChildElement>
</newRoot>
Run Code Online (Sandbox Code Playgroud)
你会如何编写样式表?
如何转换此 XML 文件:
<file>
<text ID="201" date="2014-05-04">
<user_name>user_11</user_name>
<message> HELLO </message>
</text>
</file>
Run Code Online (Sandbox Code Playgroud)
使用 XSLT 1.0 进入这个 XML 文件:
<doc>
<user name="user_11">
<text id="201" date="2014-05-04"> HELLO </text>
</user>
</doc>
Run Code Online (Sandbox Code Playgroud)
另外,如果您有关于该主题的任何资源,请发布,因为我有更多的 XSLT 文件要编码。谢谢
I need to create links using dynamic values for both the URL and the anchor text.
I have column called URL, which contains URL's, i.e., http://stackoverflow.com, and I have a column called Title, which contains the text for the anchor, i.e., This Great Site.
I figure maybe all I need to do is nest this tags, along with a little HTML to get my desired result:
<a href=">
<xsl:value-of select="@URL">">
<xsl:value-of select="@Title"/>
</xsl:value-of>
</a>
Run Code Online (Sandbox Code Playgroud)
In my …
我正在使用 xslt 文件来转换一组 xml 文件,xml 中的一些标签是在另一个命名空间中定义的。
xslt 所做的是从 AddBooks 元素标签首先复制所有内容,除了 bookattach 元素,其中属性 bookname = 'AA12' 然后构造一个新元素 bookattach where bookname="AA12" 在所有情况下,并将 bookattach 的所有子项复制到其中 bookname= “AA12”和书名=“A12”。
好吧,xslt 按预期工作,但问题是当它复制元素 bookattach 时,属性 bookname = 'AA12' 在那里添加了名称空间条目。例如,它将条目复制为:
<bookattach xmlns:mynamespace="http://myorg.com/mymodel/Library" bookname="AA12">
<book bookname="bacbook1" description="This is test book1"/>
</bookattach>
Run Code Online (Sandbox Code Playgroud)
我不想在转换后的 xml 中复制这个命名空间条目(我的源 xml 也不包含 bookattach 标记中的命名空间条目),那么我怎样才能摆脱这个特定标记中的这个命名空间条目?
我的 xslt 如下:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mynamespace="http://myorg.com/mymodel/Library"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:output method="xml"/>
<xsl:key name="bookbyid" match="mynamespace:bookattach" use="@bookname"/>
<xsl:template match="mynamespace:AddBooks">
<xsl:copy>
<xsl:apply-templates select="@*|node() [ not( (self::mynamespace:bookattach and @bookname = …Run Code Online (Sandbox Code Playgroud) 寻找这个问题的解决方案有一段时间了,因为文档并不是很清楚。
我最终使用了下面的方法,并认为我会分享回来。
有没有办法将字符串转换为二进制 base64?我看过很多参考资料,但最终没有奏效。例如我有这个输入文件:
<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
<Binary>
<RawData>This element should convert string to binary base64.</RawData>
</Binary>
</Data>
</RootElement>
Run Code Online (Sandbox Code Playgroud)
我需要生成:
<RootElement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Data>
<Binary>
<RawData>VGhpcyBlbGVtZW50IHNob3VsZCBjb252ZXJ0IHN0cmluZyB0byBiaW5hcnkgYmFzZTY0Lg==</RawData>
</Binary>
</Data>
Run Code Online (Sandbox Code Playgroud)
我创建了一个 xslt 并使用了我在网上看到的命名空间:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dp="http://www.datapower.com/extensions">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RawData">
<xsl:element name="RawData">
<xsl:value-of select="dp:encode(., 'base-64')"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
谢谢你。
查看地址下的 XSLT 代码http://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_apply ... 下面你会找到这段代码的第一部分(也是我的问题的决定性部分) ):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
如果您现在只更改行
<xsl:apply-templates/>
Run Code Online (Sandbox Code Playgroud)
到
<xsl:apply-templates select="cd"/>
Run Code Online (Sandbox Code Playgroud)
转换不再起作用......(代码现在如下所示:)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select="cd"/> <!--ONLY LINE OF CODE THAT WAS CHANGED-->
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么更改会破坏代码?在我看来,这两种情况的逻辑是一样的:
更新:
整个xslt代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p> …Run Code Online (Sandbox Code Playgroud) 我知道我必须使用 ,xsl:import但我不知道如何调用name模板的 。
我该怎么做?
我有以下 XML:
<root><super-head>Text ?? and "more" ??</super-head></root>
Run Code Online (Sandbox Code Playgroud)
还有一些实体(实际超过400件):
? = ☆
? = &heart;
" = "
? = ?
- = ‐
Run Code Online (Sandbox Code Playgroud)
现在我想用它们的实体替换列表中的所有字符。最初我尝试使用正则表达式来做到这一点,但它不起作用。所以我假设必须使用Java或XSLT(我这里只能使用1.0)。
在 Java 中,我尝试了以下操作:
public void replaceStringForNode(Node node, Map<String, String> map) {
// replace for all attributes
NamedNodeMap attributes = node.getAttributes();
for (int i = 0, l = attributes.getLength(); i < l; i++) {
Node attr = attributes.item(i);
String content = attr.getNodeValue();
for (Entry<String, String> entry : map.entrySet()) {
content = content.replace(entry.getKey(), entry.getValue());
}
attr.setNodeValue(content);
}
// …Run Code Online (Sandbox Code Playgroud)