我有一个XSL文件来传输另一个XSL文件.我希望命名空间声明在根标记上,而不是在每个元素上重复!
这是我的样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mynamespace="somenamespace" version="2.0">
<xsl:output method="xml" omit-xml-declaration="no" standalone="yes" indent="yes" />
<xsl:template match="myMatchedNode">
<mynamespace:tag>Some text i want inserted into the xsl</mynamespace:tag>
<xsl:copy>
<xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
它输出这样的东西:
....
<mynamespace:tag xmlns:mynamespace="somenamespace">Some text i want inserted into the xsl</mynamespace:tag>
....
Run Code Online (Sandbox Code Playgroud)
如何强制命名空间声明到结果的根标记?!
有点懒,我很高兴发现我可以通过New - > Servlet来创建一个新的servlet源代码,而不是去New - > Class然后将类编辑成一个servlet.
但是,我发现每次在Eclipse中创建一个新的servlet时,Eclipse都会修改我的web.xml.
具体来说,它将top元素修改为:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:javaee="http://java.sun.com/xml/ns/javaee"
xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID" version="2.4">
Run Code Online (Sandbox Code Playgroud)
(我的线路爆炸.)
这似乎并不一定是坏事,但它通过在其名称前面加上"javaee:"来修改各种子元素,以指示这些元素属于该命名空间.
例如,它会改变
<display-name>ShowLifecycles</display-name>
Run Code Online (Sandbox Code Playgroud)
至
<javaee:display-name>ShowLifecycles</javaee:display-name>
Run Code Online (Sandbox Code Playgroud)
之后,eclipse会抱怨它修改的所有元素,给我一些符号:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'javaee:display-name'. One of '{"http://
java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name, "http://java.sun.com/xml/ns/
j2ee":icon, "http://java.sun.com/xml/ns/j2ee":distributable, "http://java.sun.com/xml/ns/j2ee":context-param, "http://
java.sun.com/xml/ns/j2ee":filter, "http://java.sun.com/xml/ns/j2ee":filter-mapping, "http://java.sun.com/xml/ns/
j2ee":listener, "http://java.sun.com/xml/ns/j2ee":servlet, "http://java.sun.com/xml/ns/j2ee":servlet-mapping, "http://
java.sun.com/xml/ns/j2ee":session-config, "http://java.sun.com/xml/ns/j2ee":mime-mapping, "http://java.sun.com/xml/ns/
j2ee":welcome-file-list, "http://java.sun.com/xml/ns/j2ee":error-page, "http://java.sun.com/xml/ns/j2ee":jsp-config, "http://
java.sun.com/xml/ns/j2ee":security-constraint, "http://java.sun.com/xml/ns/j2ee":login-config, "http://java.sun.com/xml/ns/
j2ee":security-role, "http://java.sun.com/xml/ns/j2ee":env-entry, "http://java.sun.com/xml/ns/j2ee":ejb-ref, "http://
java.sun.com/xml/ns/j2ee":ejb-local-ref, "http://java.sun.com/xml/ns/j2ee":service-ref, "http://java.sun.com/xml/ns/
j2ee":resource-ref, "http://java.sun.com/xml/ns/j2ee":resource-env-ref, "http://java.sun.com/xml/ns/j2ee":message-
destination-ref, "http://java.sun.com/xml/ns/j2ee":message-destination, "http://java.sun.com/xml/ns/j2ee":locale-
encoding-mapping-list}' is expected.
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,当我使用find和replace将所有内容删除到"javaee:"中时,Eclipse仍会抱怨这些文件,即使它们不再存在.我必须将整个剩余文件复制并粘贴到自身之上,以避免这些投诉.
我确信Eclipse正在努力发挥作用,期待这个命名空间的一些愿望或需求.我怎样才能做以下两件事之一: …
我见过类似的问题,但我还不清楚.我不希望"n1"命名空间出现在输出文件中节点的属性中.但我必须在xslt文件中创建"n1"命名空间才能使xpath正常工作.谢谢.
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n1="http://www.spicefactory.org/parsley"
xmlns="http://www.spicefactory.org/parsley"
>
<xsl:output method="xml" indent="no"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="n1:object[@type='mytype1']">
<object type="mytype2">
<xsl:apply-templates select="node()"/>
</object>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
从OUTPUT XML文件中获取:
<object type="mytype2" xmlns:n1="http://www.spicefactory.org/parsley">
Run Code Online (Sandbox Code Playgroud) 我正在处理一些XML数据,这些数据在每个文件的某些位置重新定义了命名空间.我试图从文档中提取特定类型的所有标记,而不管标记位于XML中的活动名称空间是什么.
我正在findall('.//{namespace}Tag')寻找我正在寻找的元素.但是永远不知道{namespace}文件中任何给定点的内容是什么,会让它命中或遗漏我是否会返回所有请求的标签.
有没有办法返回所有Tag元素,无论{namespace}它们属于什么?有什么东西沿着findall('.//{wildcard}Tag')?
一个简单的XHTML文档,具有最少的必需标记:和xmlns属性具有http链接.
如果我正在使用localhost,如果我没有Internet连接,我不能使用XHTML吗?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用XmlNamespaceManager将命名空间添加到XmlDocument.这是当前的xml:
<?xml version="1.0"?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
我希望它转换为这个xml(使用XmlNamespaceManager):
<?xml version="1.0"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
但我无法更改xml.这是代码,我知道它应该是一个简单的修复:
public void addXmlns()
{
string xml = @"<?xml version=""1.0""?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>";
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
//Add the namespaces
nsmgr.AddNamespace("", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
nsmgr.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string message;
message = xmldoc.InnerXml;
MessageBox.Show(message); // still shows the original xml
}
Run Code Online (Sandbox Code Playgroud)
先谢谢你
更新#1 - 试过这个,但它也不会改变XML: …
如果我指定命名空间,使用以下代码我希望能够搜索目标标记.
import xml.etree.ElementTree as ET
xml = """<?xml version="1.0" encoding="UTF-8"?>
<xyz2:outer xmlns:xyz1="http://www.company.com/url/common/v1"
xmlns:xyz2="http://www.company.com/app/v2"
version="9.0"
something="false">
<xyz2:inner>
<xyz2:target>
<xyz1:idType>name</xyz1:idType>
<xyz1:id>A Name Here</xyz1:id>
</xyz2:target>
</xyz2:inner>
</xyz2:outer>"""
tree = ET.fromstring(xml)
print tree[0][0]
# <Element '{http://www.company.com/app/v2}target' at 0x7f3c294374d0>
tree.find('{http://www.company.com/app/v2}target')
# None
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都无法找到目标标签?
我已经尝试了各种ElementTree实现,包括lxml,其中涉及{*}名称空间.没有骰子?
从doc,我发现如下所示使用命名空间:
@namespace foo url(http://www.example.com);
foo|h1 { color: blue }
Run Code Online (Sandbox Code Playgroud)
但我想更多地了解这一点.我们为什么用它?
我有以下spring xml配置头:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
....
Run Code Online (Sandbox Code Playgroud)
当我在想法中打开文件时,我看到红色错误:1.xmlns:p="http://www.springframework.org/schema/p -
URI未注册

但它运作良好.
如何避免红色错误?
我的xml配置中有以下片段:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud) 这个问题旨在作为回答一个特别常见的问题的参考,这个问题可能有不同的形式:
如果您的问题已作为此问题的副本关闭,则可能与这些示例不同,但此页面应告诉您需要了解的内容.
这是一个说明性的例子:
$xml = '
<?xml version="1.0" encoding="utf-8"?>
<document xmlns="http://example.com" xmlns:ns2="https://namespaces.example.org/two" xmlns:seq="urn:example:sequences">
<list type="short">
<ns2:item seq:position="1">A thing</ns2:item>
<ns2:item seq:position="2">Another thing</ns2:item>
</list>
</document>
';
$sx = simplexml_load_string($xml);
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用 ; 为什么不?
foreach ( $sx->list->ns2:item as $item ) {
echo 'Position: ' . $item['seq:position'] . "\n";
echo 'Item: ' . (string)$item . "\n";
}
Run Code Online (Sandbox Code Playgroud)
第一个问题是->ns2:item语法无效; 但将其更改为此也不起作用:
foreach ( $sx->list->{'ns2:item'} as $item ) { ... }
Run Code Online (Sandbox Code Playgroud)
为什么不呢,你应该用什么呢?
xml-namespaces ×10
xml ×3
elementtree ×2
java ×2
xslt ×2
c# ×1
css ×1
eclipse ×1
html ×1
php ×1
python ×1
python-2.7 ×1
python-3.x ×1
servlets ×1
simplexml ×1
spring ×1
validation ×1
web.xml ×1
xhtml ×1
xml-parsing ×1
xmldocument ×1