我有一个我创建的XSLT来处理特定的xml文档.但是,现在名称空间正在某些文档中引入,但并非所有文档都引入了名称空间.我想对这些文档使用相同的XSLT,但是我无法将样式表修改为名称空间无关.
之前曾建议修改我的xpath *[local-name()="ElementName"],但考虑到我们已经制作了样式表,这是非常耗费人力的.
另外,我知道我可以设置xpath-default-namespace到特定的命名空间,但如前所述,我不能简单地将其设置为#all或可能的命名空间列表.我正在寻找更有效的解决方案,并且可以使用Saxon处理器的扩展功能.有任何想法吗?谢谢.
我有一个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)
如何强制命名空间声明到结果的根标记?!
我想使用XmlWriter写这样的东西(所有在一个命名空间中):
<Root xmlns="http://tempuri.org/nsA">
<Child attr="val" />
</Root>
Run Code Online (Sandbox Code Playgroud)
但我能得到的最接近的是:
<p:Root xmlns:p="http://tempuri.org/nsA">
<p:Child p:attr="val" />
</p:Root>
Run Code Online (Sandbox Code Playgroud)
使用此代码:
using System;
using System.Text;
using System.Xml;
namespace ConsoleApplication1
{
internal class Program
{
private const string ns = "http://tempuri.org/nsA";
private const string pre = "p";
private static void Main(string[] args)
{
var sb = new StringBuilder();
var settings = new XmlWriterSettings
{
NamespaceHandling = NamespaceHandling.OmitDuplicates,
/* ineffective */
Indent = true
};
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
writer.WriteStartElement(pre, "Root", ns);
writer.WriteStartElement(pre, "Child", ns); …Run Code Online (Sandbox Code Playgroud) 我试图将XmlnsDefinitionAttributes添加到我的程序集.我使用TeamCity来管理我的项目.我的构建配置中有一个NuGet Package Pack步骤.
以下是失败的构建日志的相关部分.
[16:42:08]: Starting: C:\Windows\system32\cmd.exe /c C:\BuildAgent\tools\NuGet.CommandLine.1.5.20905.5.nupkg\tools\NuGet.exe pack C:\BuildAgent\work\5f0e65f22ca1527\MyProject\MyProject.csproj -OutputDirectory \\server\Packages -BasePath C:\BuildAgent\work\5f0e65f22ca1527 -Verbose -Version 1.0.97.1034 -Symbols -Properties Configuration=Release
[16:42:08]: in directory: C:\BuildAgent\work\5f0e65f22ca1527
[16:42:08]: Attempting to build package from 'MyProject.csproj'.
[16:42:09]: Packing files from 'C:\BuildAgent\work\5f0e65f22ca1527\MyProject\Bin\Release'.
[16:42:09]: WARNING: Unable to extract metadata from 'MyProject.dll'.
[16:42:09]: Using 'MyProject.nuspec' for metadata.
[16:42:09]: The replacement token 'title' has no value.
[16:42:09]: Process exited with code 1
Run Code Online (Sandbox Code Playgroud)
成功构建的相同部分如下所示.
[16:42:54]: Starting: C:\Windows\system32\cmd.exe /c C:\BuildAgent\tool\NuGet.CommandLine.1.5.20905.5.nupkg\tools\NuGet.exe pack C:\BuildAgent\work\5f0e65f22ca1527\MyProject\MyProject.csproj -OutputDirectory \\server\Packages -BasePath C:\BuildAgent\work\5f0e65f22ca1527 -Verbose -Version 1.0.98.1035 -Symbols …Run Code Online (Sandbox Code Playgroud) 我想对这个文件做一个XPath查询(摘录显示):
<?xml version="1.0" encoding="UTF-8"?>
<!-- MetaDataAPI generated on: Friday, May 25, 2007 3:26:31 PM CEST -->
<ModelClass xmlns="http://xml.sap.com/2002/10/metamodel/webdynpro" xmlns:IDX="urn:sap.com:WebDynpro.ModelClass:2.0">
<ModelClass.Parent>
<Core.Reference package="com.test.mypackage" name="ModelName" type="Model"/>
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码的片段:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(new File(testFile));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
xpath.setNamespaceContext( new NamespaceContext() {
public String getNamespaceURI(String prefix) {
...
String result = xpath.evaluate(xpathQueryString, document);
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当在XPath查询中引用默认命名空间时,不会调用getNamespaceURI方法来解析它.例如,此查询不提取任何内容:
//xmlns:ModelClass.Parent/xmlns:Core.Reference[@type=\"Model\"]/@package
现在我尝试通过替换xmlns伪造的前缀来"欺骗"解析器d,然后相应地编写getNamespaceURI方法(以便http://xml.sap.com/2002/10/metamodel/webdynpro在d遇到时返回).在这种情况下,getNamespaceURI调用它,但XPath表达式求值的结果始终为空字符串.
如果我从文件和XPath查询表达式中删除命名空间,我可以获得我想要的字符串(com.test.mypackage).
有没有办法使用默认命名空间使事情正常工作?
我正在构建一个自托管的WCF服务.我正在构建一个特殊的数据结构,以实现非常灵活的数据传输.到目前为止,我测试我的结构是否可以使用DataContractSerializer进行序列化.这工作正常,我很高兴,但有些东西让我烦恼:
在我的XML输出中有许多重新定义的xmlns属性,例如:
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema"
Run Code Online (Sandbox Code Playgroud)
这应该在根元素中更好地定义一次,以便可以简单地优化字节.有没有办法将自定义命名空间信息添加到根元素?
这是一个更大的例子来证明我的意思:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Data xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
<Data xmlns:a="...">...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud)
我想要的是这样的:
<DataObject xmlns="http://schemas.datacontract.org/2004/07/Test"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"
xmlns:b="http://www.w3.org/2001/XMLSchema">
<Data>
<a:KeyValueOfstringanyType>
<a:Key>ID</a:Key>
<a:Value i:type="b:int">1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>Value</a:Key>
<a:Value i:type="b:int">42</a:Value>
</a:KeyValueOfstringanyType>
</Data>
<Data>...</Data>
<Data>...</Data>
<Data>...</Data>
</DataObject>
Run Code Online (Sandbox Code Playgroud) 我的XSLT是带有xmlns:x="http://something"属性的一些标签...如何避免这个冗余属性?输出XML从不使用,既不在x:tag,也不在x:attribute.
XML示例:
<root><p>Hello</p><p>world</p></root>
Run Code Online (Sandbox Code Playgroud)
XSL的例子:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output encoding="UTF-8" method="xml" version="1.0" indent="no"/>
<xsl:template match="root"><foo>
<xsl:for-each select="p">
<p><xsl:value-of select="." /></p>
</xsl:for-each></foo>
<xsl:for-each select="x">
<link xlink:href="{x}" />
</xsl:for-each></foo>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
XML输出示例:
<foo>
<p xmlns:xlink="http://www.w3.org/1999/xlink">Hello</p>
<p xmlns:xlink="http://www.w3.org/1999/xlink">world</p>
</foo>
Run Code Online (Sandbox Code Playgroud)
这xmlns:xlink是一个开销,它没有被使用!
XSLT必须使用命名空间但输出不是的典型情况:
<xsl:value-of select="php:function('regFunction', . )" />
Run Code Online (Sandbox Code Playgroud) 这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
从Android官方培训中剔除
在第二行中,AndroidStudio显示此警告:
Namespace declaration is never used
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 ×4
xslt ×3
java ×2
xpath ×2
.net-4.0 ×1
android ×1
c# ×1
namespaces ×1
nuget ×1
output ×1
php ×1
silverlight ×1
simplexml ×1
spring ×1
validation ×1
xmlwriter ×1