标签: xml-namespaces

XSLT应用于具有xmlns属性的XML文档

我正在将XSLT样式表应用于以下XML文件:

<top xmlns="http://www.foo.com/bar">
    <elementA />
    <elementB />
    <contents>
        <contentitem>
                <id>3</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>2</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
        <contentitem>
                <id>1</id>
                <moretags1 />
                <moretags2 />
        </contentitem>
    </contents>
</top>
Run Code Online (Sandbox Code Playgroud)

这是我当前的XSLT文件(执行简单排序):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:doc="http://www.foo.com/bar">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
<!--                                                -->
 <xsl:strip-space elements="*"/>
<!--                                                -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
<!--                                                -->
  <xsl:template match="contents">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="contentitem">
        <xsl:sort select="id" data-type="number"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

问题是,我不知道如何使用'doc:'命名空间前缀与xsl:template和xsl:apply-templates标记.

现在,XML文档按原样复制,所以我相信正在应用第一个xsl:template块.但是,这些项目是未排序的,所以我认为问题在于第二个xsl:template.

我应该注意,如果我从两个文件中删除xmlns属性,转换工作正常.

有什么建议?

(问题基于这个例子)

xml sorting xslt xml-namespaces

5
推荐指数
1
解决办法
7163
查看次数

RDFa产品的内容实施

我是噩梦中的膝盖深处,这是产品信息的RDFa实施,如果有人能提供一些见解我很好奇.

谷歌表示,不是为了向机器提供数据而隐藏信息,除非它是特定于机器的信息.为了提供这些数据,我找不到任何有关空元素的信息.

如果你看一下商业的GoodRelations RDFa生成器,你会得到一堆嵌套的div,你被告知放在你的项目页面的底部.我将使用我最喜欢的网站之一:

<div xmlns="http://www.w3.org/1999/xhtml"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
  xmlns:gr="http://purl.org/goodrelations/v1#"
  xmlns:foaf="http://xmlns.com/foaf/0.1/">

  <div typeof="gr:Offering" about="#offering">
    <div rev="gr:offers" resource="#company"></div>
    <div property="rdfs:label" content="Alpinestars S-MX Plus Racing Boots" xml:lang="en"></div>
    <div property="rdfs:comment" content="Alpinestars’ S-MX Plus racing boot raises performance and safety to new and unmatched levels with its innovative design, structural protection and comfort." xml:lang="en"></div>
    <div property="gr:hasEAN_UCC-13" content="0000000000000" datatype="xsd:string"></div>
    <div rel="foaf:depiction" resource="http://www.motorcycle-superstore.com/ProductImages/300/g17268.jpg"></div>
    <div rel="gr:hasBusinessFunction" resource="http://purl.org/goodrelations/v1#Sell"></div>
    <div rel="gr:hasPriceSpecification">
      <div typeof="gr:UnitPriceSpecification">
        <div property="gr:hasCurrency" content="USD" datatype="xsd:string"></div>
        <div property="gr:hasCurrencyValue" content="349.95" datatype="xsd:float"></div>
        <div property="gr:hasUnitOfMeasurement" content="C62" datatype="xsd:string"></div>
      </div>
    </div>
    <div rel="gr:acceptedPaymentMethods" …
Run Code Online (Sandbox Code Playgroud)

xml rdf xml-namespaces rdfa

5
推荐指数
1
解决办法
649
查看次数

如何使用DOM/PHP声明XML名称空间前缀?

我试图通过DOM/PHP5生成以下XML:

<?xml version="1.0"?>
<root xmlns:p="myNS">
  <p:x>test</p:x>
</root>
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的事情:

$xml = new DOMDocument('1.0');
$root = $xml->createElementNS('myNS', 'root');
$xml->appendChild($root);
$x = $xml->createElementNS('myNS', 'x', 'test');
$root->appendChild($x);
echo $xml->saveXML();
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

<?xml version="1.0"?>
<root xmlns="myNS">
  <x>test</x>
</root>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如何使这个前缀工作?

php xml dom xml-namespaces

5
推荐指数
1
解决办法
8273
查看次数

如何从XML根元素中删除命名空间?

是否有一种从XML根元素中删除命名空间的简单方法.我试过了

[XmlRootAttribute("MCP", Namespace = "", IsNullable = false)]    
Run Code Online (Sandbox Code Playgroud)

在序列化类上.但没用.仍然得到相同的结果.

样本班

[Serializable]
[XmlRootAttribute("MCP", Namespace = "", IsNullable = false)]    
public class BINDRequest
{
    public BINDRequest()
    {

    }
    [XmlAttribute]
    public string CLIENT_REQUEST_ID { get; set; }

    public BINDRequestBody BIND { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

结果xml

<?xml version="1.0" encoding="utf-8"?>
<MCP xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" CLIENT_REQUEST_ID="1">
  <BIND CLIENT_ID="test" PASSWORD="test" />
</MCP>
Run Code Online (Sandbox Code Playgroud)

我不明白那么在XmlRootAttribute中使用指定namsespace是什么意思?

c# xml-namespaces xmlserializer

5
推荐指数
1
解决办法
9158
查看次数

名称空间中的URL引用,它们如何工作?

我理解常规的XML命名空间,例如:

 xmlns:myExample="clr-namespace:WindowsApp.MyNamespace;assembly=MyAssembly"
Run Code Online (Sandbox Code Playgroud)

但我经常看到表单的命名空间:

 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
Run Code Online (Sandbox Code Playgroud)

这些URL名称空间意味着什么?当我在浏览器中输入URL时,URL不起作用; 有谁知道这是如何工作的?提前致谢.

c# xml wpf xaml xml-namespaces

5
推荐指数
1
解决办法
1846
查看次数

.NET中的XmlSerializer与XmlSchemaForm.Unqualified

鉴于以下代码:

using System;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace XmlSerializationTest
{
    [XmlType(Namespace = "http://www.test.com")]
    public class Element
    {
        [XmlElement]
        public int X;
    }

    [XmlRoot(Namespace = "http://www.test.com")]
    public class Root
    {
        [XmlElement(Form = XmlSchemaForm.Unqualified)]
        public Element Element;
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            var root = new Root { Element = new Element { X = 1 } };
            var xmlSerializer = new XmlSerializer(typeof(Root));
            xmlSerializer.Serialize(Console.Out, root);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

<?xml version="1.0" encoding="ibm852"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" …
Run Code Online (Sandbox Code Playgroud)

.net c# xml-serialization xml-namespaces wscf

5
推荐指数
1
解决办法
7111
查看次数

Javascript Xpath和默认名称空间

我有一些JavaScript/Xpath,它没有像我期望的那样工作.(在jsfiddle上可用)似乎我在使用XML命名空间时出错了,阻止我通过节点(标记)名称查询我的元素.

如果我尝试查询当前节点的所有子节点,我发现该元素myElement没有问题:

    var xpathResult = xmlDoc.evaluate( "child::*", rootElement, nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    var queryEl;
    if(queryEl = xpathResult.iterateNext()) {
        alert("child::* found element " + queryEl.nodeName);
    }
    else {
        alert("child::* found nothing!");
    }
Run Code Online (Sandbox Code Playgroud)

...但是如果我专门针对具有myElement节点(标记)名称的节点,我得不到任何结果:

    /* Now try getting only those children with nodeName `myElement` */
   xpathResult = xmlDoc.evaluate( "child::myElement", rootElement, nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
    var queryEl;
    if(queryEl = xpathResult.iterateNext()) {
        alert("child::myElement found element " + queryEl.nodeName);
    }
    else {
        alert("child::myElement found nothing!");
    }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

javascript xpath xml-namespaces

5
推荐指数
1
解决办法
1615
查看次数

将xmlns属性添加到根元素

我有C#程序生成RDL文件用于报告服务中的whitch Show报告.我使用Linq到Xml来生成Xml.

当我尝试将xmlns XAttribute 添加到Report Element时,我遇到了几个问题.

我测试以下方法:

第一:

        XDocument d = new XDocument(
           new XDeclaration("1.0", "utf-8", "yes"),
           new XElement("Report",
               new XAttribute(XNamespace.Xmlns + "rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"),
               new XAttribute(XNamespace.Xmlns + "cl", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"),
               new XAttribute("xmlns", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition"),
                   new XElement("DataSources", ""),
                   new XElement("DataSets", ""),
                   new XElement("ReportSections",
Run Code Online (Sandbox Code Playgroud)

这是我的代码中的一部分,显示如何生成xml:

第二:

XNamespace reportDef = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition";
        XDocument d = new XDocument(
           new XDeclaration("1.0", "utf-8", "yes"),
           new XElement(reportDef + "Report",
               new XAttribute(XNamespace.Xmlns + "rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"),
               new XAttribute(XNamespace.Xmlns + "cl", "http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition"),
                   new XElement("DataSources", ""),
                   new XElement("DataSets", ""),
                   new XElement("ReportSections",...
Run Code Online (Sandbox Code Playgroud)

第一种方法返回Error,第二种方法将属性xmlns …

c# xml linq linq-to-xml xml-namespaces

5
推荐指数
1
解决办法
2万
查看次数

从JAXB Generics中删除xsi:type,xmlns:xs和xmlns:xsi

使用JAXB时,我想在使用Generics时从我的XML元素中删除多余的命名空间/类型.我怎么能这样做或我做错了什么?我想使用泛型,所以我只需要编写一段代码.

示例代码:

public static void main(String[] args) {
    try {
        TestRoot root = new TestRoot();
        root.name.value = "bobby";
        root.age.value = 102;
        root.color.value = "blue";

        JAXBContext context = JAXBContext.newInstance(root.getClass());
        Marshaller marsh = context.createMarshaller();
        marsh.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
        marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);

        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        marsh.marshal(root,pw);
        System.out.println(sw.toString());
    }
    catch(Throwable t) {
        t.printStackTrace();
    }
}

@XmlRootElement
static class TestRoot {
    @XmlElement public TestGeneric<String> name = new TestGeneric<String>(true);
    @XmlElement public TestGeneric<Integer> age = new TestGeneric<Integer>(true);
    @XmlElement public TestWhatIWouldLike color = new TestWhatIWouldLike(true);
} …
Run Code Online (Sandbox Code Playgroud)

java generics jaxb xml-namespaces xsi

5
推荐指数
1
解决办法
2万
查看次数

ASP.NET Web API中的XML命名空间

我目前正在从事一个项目,该项目需要我从其端点与JSON一起输出XML。我有以下模型:

[DataContract(Namespace="http://www.yale.edu/tp/cas")]
[XmlType("serviceResponse")]
[XmlRoot(Namespace="http://www.yale.edu/tp/cas")]
public class ServiceResponse
{
    [XmlElement("authenticationSuccess")]
    public AuthenticationSuccess Success { get; set; }

    [XmlElement("authenticationFailure")]
    public AuthenticationFailure Failure { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

成功不为空时,输出如下:

<serviceResponse>
<authenticationSuccess />
</serviceResponse>
Run Code Online (Sandbox Code Playgroud)

现在,我可以很明显地看到,我没有为分配给名称空间的前缀分配前缀。我的问题是我找不到使用媒体格式化程序在MVC4中添加名称空间前缀的位置。我的global.asax中包含以下内容:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse)));
Run Code Online (Sandbox Code Playgroud)

我创建了一个基于XmlSerializer的自定义序列化程序,以尝试拦截写入请求并在此处添加名称空间列表。这种方法的问题是,现在我在每个可重写方法中都有断点,并且在序列化时都没有断点,这使我相信没有使用我的序列化器。

是否有一些内置的方法来完成我想做的事情,还是我不得不重新实现XmlMediaTypeFormatter以在序列化对象时传递名称空间?

c# xml xml-serialization xml-namespaces asp.net-mvc-4

5
推荐指数
1
解决办法
1817
查看次数