如何将xmlns:*属性与XSLT 1.0相匹配?我尝试使用RDF文档:
<xs:template match="rdf:RDF">
(...)
<xsl:for-each select="@*">
<xsl:value-of select="."/>
</xsl:for-each>
(...)
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
但它似乎不适用于xmlns属性.
谢谢.
我正在尝试使用lxml的ElementTree etree在我的xml文档中查找特定标记.标签如下所示:
<text:ageInformation>
<text:statedAge>12</text:statedAge>
</text:ageInformation>
Run Code Online (Sandbox Code Playgroud)
我希望使用etree.find('text:statedAge'),但该方法不喜欢'text'前缀.它提到我应该在前缀地图中添加"文本",但我不确定如何做到这一点.有小费吗?
编辑:我希望能够写入hr4e前缀标签.以下是该文件的重要部分:
<?xml version="1.0" encoding="utf-8"?>
<greenCCD xmlns="AlschulerAssociates::GreenCDA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hr4e="hr4e::patientdata" xsi:schemaLocation="AlschulerAssociates::GreenCDA green_ccd.xsd">
<header>
<documentID root="18c41e51-5f4d-4d15-993e-2a932fed720a" />
<title>Health Records for Everyone Continuity of Care Document</title>
<version>
<number>1</number>
</version>
<confidentiality codeSystem="2.16.840.1.113883.5.25" code="N" />
<documentTimestamp value="201105300211+0800" />
<personalInformation>
<patientInformation>
<personID root="2.16.840.1.113883.3.881.PI13023911" />
<personAddress>
<streetAddressLine nullFlavor="NI" />
<city>Santa Cruz</city>
<state nullFlavor="NI" />
<postalCode nullFlavor="NI" />
</personAddress>
<personPhone nullFlavor="NI" />
<personInformation>
<personName>
<given>Benjamin</given>
<family>Keidan</family>
</personName>
<gender codeSystem="2.16.840.1.113883.5.1" code="M" />
<personDateOfBirth value="NI" />
<hr4e:ageInformation>
<hr4e:statedAge>9424</hr4e:statedAge>
<hr4e:estimatedAge>0912</hr4e:estimatedAge>
<hr4e:yearInSchool>1</hr4e:yearInSchool>
<hr4e:statusInSchool>attending</hr4e:statusInSchool>
</hr4e:ageInformation>
</personInformation>
<hr4e:livingSituation>
<hr4e:homeVillage>Putney</hr4e:homeVillage> …Run Code Online (Sandbox Code Playgroud) 我有一个Document像这样建模XML 的对象:
<RootNode xmlns="http://a.com/a" xmlns:b="http://b.com/b">
<Child />
</RootNode>
Run Code Online (Sandbox Code Playgroud)
使用Java DOM,我需要获取<Child>节点并将其序列化为XML,但保留根节点名称空间.这是我目前拥有的,但它没有序列化命名空间:
public static void main(String[] args) throws Exception {
String xml = "<RootNode xmlns='http://a.com/a' xmlns:b='http://b.com/b'><Child /></RootNode>";
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
Node childNode = doc.getFirstChild().getFirstChild();
// serialize to string
StringWriter sw = new StringWriter();
DOMSource domSource = new DOMSource(childNode);
StreamResult streamResult = new StreamResult(sw);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.transform(domSource, streamResult);
String serializedXML = sw.toString();
System.out.println(serializedXML);
}
Run Code Online (Sandbox Code Playgroud)
当前输出:
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud) 我似乎无法解决这个问题.我想从RSS文件(http://feeds.bbci.co.uk/news/rss.xml)获取媒体:缩略图.
我做了一些研究,并尝试将来自/sf/ask/469512081/ 和其他来源的见解纳入其中 .
这就是我得到的:
$source_link = "http://feeds.bbci.co.uk/news/rss.xml";
$source_xml = simplexml_load_file($source_link);
$namespace = "http://search.yahoo.com/mrss/";
foreach ($source_xml->channel->item as $rss) {
$title = $rss->title;
$description = $rss->description;
$link = $rss->link;
$date_raw = $rss->pubDate;
$date = date("Y-m-j G:i:s", strtotime($date_raw));
$image = $rss->attributes($namespace);
print_r($image);
}
Run Code Online (Sandbox Code Playgroud)
当我运行脚本时,我看到的只是一个白页.如果我回显或print_r任何其他变量,那么它就像一个魅力.它只是造成问题的$ image.为什么这不起作用?感谢任何帮助!
假设我有一个带有2个带有前缀的名称空间声明的XML文档foo,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:foo="http://www.foo.com">
<one>
<!-- children nodes here -->
</one>
<two>
<!-- children nodes here -->
</two>
<three xmlns:foo="http://www.foo.com">
<!-- children nodes here -->
</three>
</root>
Run Code Online (Sandbox Code Playgroud)
我想评估一个XPath表达式(在Java中),该表达式将返回具有此命名空间声明的元素的NodeList,即root和three节点.我不是在寻找这个命名空间在范围内的所有节点,而只是寻找具有命名空间声明的节点.
这是我计划使用的Java:
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression xPathExpression = null;
NodeList nodeList = null;
boolean theExpressionWasCompiled = true;
xPathExpression = xPath.compile(xPathStatement); // XPath goes here!
nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
Run Code Online (Sandbox Code Playgroud)
我应该使用什么的XPath(该值xPathStatement的compile()方法)?
编辑:XPath 1或2确定.
最终编辑:事实证明,XPath不能完全符合我的要求(如果你想要细节,请参阅下面的Dimitre的解释).我能做的最好的事情是多次评估XPath(每个命名空间声明一次),以找到具有命名空间声明的每个元素.我碰巧已经知道每个命名空间的声明次数,因此知道要评估多少次对我来说不是问题.不是超级高效,但确实有效.这是我使用的XPath,它与Dimitre提出的非常类似(见下文):
//*[namespace::*[local-name() = 'foo']] …Run Code Online (Sandbox Code Playgroud) 我有以下XML文件,目前我使用minidom命名,我得到的例子中documentElement的tagName作为存在xyz:widget,告诉我,它忽略了!ENTITY定义,因此!DOCTYPE参考.
哪个XML解析器支持文档类型定义,以便 不会忽略!ENTITY定义和!DOCTYPE引用:
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE widget [
<!ENTITY widgets-ns "http://www.w3.org/ns/widgets">
<!ENTITY pass "pass&.html">
]>
<xyz:widget xmlns:xyz="&widgets-ns;">
<xyz:content src="&pass;"/>
<xyz:name>bv</xyz:name>
</xyz:widget>
Run Code Online (Sandbox Code Playgroud)
因此,对于上面的示例,您可以使用XML等效的python :
<widget xmlns="http://www.w3.org/ns/widgets">
<content src="pass&.html"/>
<name>bv</name>
</widget>
Run Code Online (Sandbox Code Playgroud)
或者得到一个DOM有作为documentElement的widget和它childNodes的content和name,widget属性作为xmlns与价值http://www.w3.org/ns/widgets等
我可能没有使用正确的术语,但我希望在上面的例子的帮助下我明白了.
这是我的输入XML文档:
<test xmlns="http://www.example.com/v1">
<qnameValue xmlns:foo="http://foo.example.com/">foo:bar</qnameValue>
</test>
Run Code Online (Sandbox Code Playgroud)
我想使用XSLT(2.0)将此文档的命名空间更改为v2,即所需的输出是:
<test xmlns="http://www.example.com/v2">
<qnameValue xmlns:foo="http://foo.example.com/">foo:bar</qnameValue>
</test>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用此样式表:
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:previous='http://www.example.com/v1'>
<xsl:output encoding='UTF-8' indent='yes' method='xml'/>
<!-- Identity transform -->
<xsl:template match='@*|node()'>
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
<!-- Previous namespace -> current. No other changes required. -->
<xsl:template match='previous:*'>
<xsl:element name='{local-name()}' namespace='http://www.example.com/v2'>
<xsl:apply-templates select='@* | node()' />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
不幸的是输出结果如下:
<test xmlns="http://www.example.com/v2">
<qnameValue>foo:bar</qnameValue>
</test>
Run Code Online (Sandbox Code Playgroud)
即qnameValue上的关键命名空间绑定已经消失.有没有办法强制所有命名空间绑定的副本到输出?
首先,我想强调一下,我确实知道如何在我的XAML文件中正确定义XML命名空间.这个重复的错误不是由我的代码中的错误引起的.问题并不能阻止我的项目成功构建,或者运行并且提到的Converter类完美地工作.
当我构建项目时,错误消失了.但是,设计人员会显示熟悉的错误消息:

当我点击"重新加载设计器"链接时,错误将返回.这个问题最糟糕的部分是我在这些视图上丢失了所有Intellisense.
错误突出显示了这些命名空间:

但是您可以清楚地看到它们已被正确声明:

我确信之前已经找到了解决这个问题的方法,但是在刚刚进行了广泛的搜索之后,我只能找到与此错误相关的一百万个帖子,其中用户实际上已经犯了错误.另请注意,这是一个全新的WPF应用程序,不能从互联网或网络驱动器下载.
我正在尝试习惯XSLT,我理解命名空间的原因,但我只是想将本地XML文件转换为本地应用程序使用.
我正在尝试转换此处的文件:http://uscodebeta.house.gov/download/releasepoints/us/pl/113/31/xml_usc01@113-31.zip
使用此代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" name="xml"/>
<xsl:template match="//title">
<xsl:for-each select="section">
<xsl:variable name="href"><xsl:value-of select="ancestor::title/num/@value" />-<xsl:value-of select="ancestor::chapter/num/@value" />-<xsl:value-of select="num/@value" />.xml</xsl:variable>
<xsl:result-document href="$href">
<xsl:element name="structure">
<xsl:element name="unit">
<xsl:attribute name="label">title</xsl:attribute>
<xsl:attribute name="identifier">
<xsl:value-of select="ancestor::title/num/@value" />
</xsl:attribute>
<xsl:attribute name="order_by">
<xsl:value-of select="ancestor::title/num/@value" />
</xsl:attribute>
<xsl:attribute name="level">1</xsl:attribute>
<xsl:value-of select="ancestor::title/num" /> <xsl:value-of select="ancestor::title/heading"/>
</xsl:element>
</xsl:element>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
到这里找到的XML示例:https: //github.com/statedecoded/statedecoded/wiki/XML-Format-for-Parser
这只是第一个元素的转换,但是当在命令行上运行Saxon时,我收到警告:
Warning: SXXP0005: The source document is in namespace http://xml.house.gov/schemas/uslm/1.0, but all the template rules …Run Code Online (Sandbox Code Playgroud) 我尝试使用自定义命名空间序列化对象.这就是这个类的样子:
[XmlRoot("Root", Namespace = "myNamespace")]
public partial class MyClass
{
public MyClass()
{
this.Xmlns = new XmlSerializerNamespaces();
this.Xmlns.Add(string.Empty, "myNamespace");
}
[XmlNamespaceDeclarations()]
public XmlSerializerNamespaces Xmlns = null;
}
Run Code Online (Sandbox Code Playgroud)
以下是序列化它的代码:
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
serializer.Serialize(xmlWriter, obj);
Run Code Online (Sandbox Code Playgroud)
预期的结果是
<Root xmlns="myNamespace" />
Run Code Online (Sandbox Code Playgroud)
但是它仍然有xmlns:xsi和xmlns:xsd属性:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="myNamespace" />
Run Code Online (Sandbox Code Playgroud)
当我序列化使用时serializer.Serialize(xmlWriter, obj, obj.Xmlns),结果是正确的.
为什么序列化程序会忽略该XmlNamespaceDeclarations属性?它不应该自动从它获取命名空间声明吗?如何在序列化类中定义名称空间?
提前致谢!