I have an Xml File as below:
我有一个Xml文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ea:Stories ea:WWVersion="2.0" xmlns:aic="http://ns.adobe.com/AdobeInCopy/2.0" xmlns:ea="urn:SmartConnection_v3">
<ea:Story ea:GUID="D8BEFD6C-AB31-4B0E-98BF-7348968795E1" pi0="style="50" type="snippet" readerVersion="6.0" featureSet="257" product="8.0(370)" " pi1="SnippetType="InCopyInterchange"">
<ea:StoryInfo>
<ea:SI_EL>headline</ea:SI_EL>
<ea:SI_Words>4</ea:SI_Words>
<ea:SI_Chars>20</ea:SI_Chars>
<ea:SI_Paras>1</ea:SI_Paras>
<ea:SI_Lines>1</ea:SI_Lines>
<ea:SI_Snippet>THIS IS THE HEADLINE</ea:SI_Snippet>
<ea:SI_Version>AB86A3CA-CEBC-49AA-A334-29641B95748D</ea:SI_Version>
</ea:StoryInfo>
</ea:Story>
</ea:Stories>
As you can see all elements have "ea:" which is a namespace prefix.
如您所见,所有元素都有“ea:”,这是一个名称空间前缀。
I'm writing an XSLT file to show the SI_Snippet text which is "THIS IS THE HEADLINE".
我正在写一个XSLT文件来显示SI_Snippet文本,这是“这是标题”。
How to write the xpath in the XSLT file? Should it contain the namespace or should it be excluded?
如何在XSLT文件中编写xpath?它应该包含命名空间还是应该被排除?
//ea:Story[ea:SI_EL='headline']/ea:SI_Snippet or
//Story[SI_EL='headline']/SI_Snippet
Actually both fail in the online tool I used: http://xslt.online-toolz.com/tools/xslt-transformation.php
实际上我在使用的在线工具中都失败了:http://xslt.online-toolz.com/tools/xslt-transformation.php
So there should be another way?
那应该有另一种方式吗?
If later, how does it know which namespace to look at? Should I be passing the namespace to the XslTransformer at runtime?
如果以后,它如何知道要查看哪个命名空间?我应该在运行时将命名空间传递给XslTransformer吗?
2 个解决方案
#1
1
You should declare the namespace in your XSLT and then use the prefix that you give it:
您应该在XSLT中声明命名空间,然后使用您提供的前缀:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ea="urn:SmartConnection_v3">
<xsl:template match="/">
<xsl:value-of select="//ea:Story[ea:SI_EL='headline']/ea:SI_Snippet" />
</xsl:template>
<!-- ... -->
</xsl:stylesheet>
Note the xmlns:ea="urn:SmartConnection_v3"
in the root element. This is important.
请注意根元素中的xmlns:ea =“urn:SmartConnection_v3”。这个很重要。
#2
0
Try to use XDocument
?
尝试使用XDocument?
var xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<ea:Stories ea:WWVersion=""2.0"" xmlns:aic=""http://ns.adobe.com/AdobeInCopy/2.0"" xmlns:ea=""urn:SmartConnection_v3"">
<ea:Story ea:GUID=""D8BEFD6C-AB31-4B0E-98BF-7348968795E1"" pi0=""style="50" type="snippet" readerVersion="6.0" featureSet="257" product="8.0(370)" "" pi1=""SnippetType="InCopyInterchange""">
<ea:StoryInfo>
<ea:SI_EL>headline</ea:SI_EL>
<ea:SI_Words>4</ea:SI_Words>
<ea:SI_Chars>20</ea:SI_Chars>
<ea:SI_Paras>1</ea:SI_Paras>
<ea:SI_Lines>1</ea:SI_Lines>
<ea:SI_Snippet>THIS IS THE HEADLINE</ea:SI_Snippet>
<ea:SI_Version>AB86A3CA-CEBC-49AA-A334-29641B95748D</ea:SI_Version>
</ea:StoryInfo>
</ea:Story>
</ea:Stories>";
XDocument xdoc = XDocument.Parse(xml.ToString());
XElement v = xdoc.Descendants().FirstOrDefault(x => x.Name.LocalName == "SI_Snippet");
EDIT
编辑
XPathNavigator navigator = xmldDoc.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
ns.AddNamespace("ea", "urn:SmartConnection_v3");
var v = xmlDoc.SelectSingleNode("//ea:SI_Snippet", ns);
#1
1
You should declare the namespace in your XSLT and then use the prefix that you give it:
您应该在XSLT中声明命名空间,然后使用您提供的前缀:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ea="urn:SmartConnection_v3">
<xsl:template match="/">
<xsl:value-of select="//ea:Story[ea:SI_EL='headline']/ea:SI_Snippet" />
</xsl:template>
<!-- ... -->
</xsl:stylesheet>
Note the xmlns:ea="urn:SmartConnection_v3"
in the root element. This is important.
请注意根元素中的xmlns:ea =“urn:SmartConnection_v3”。这个很重要。
#2
0
Try to use XDocument
?
尝试使用XDocument?
var xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<ea:Stories ea:WWVersion=""2.0"" xmlns:aic=""http://ns.adobe.com/AdobeInCopy/2.0"" xmlns:ea=""urn:SmartConnection_v3"">
<ea:Story ea:GUID=""D8BEFD6C-AB31-4B0E-98BF-7348968795E1"" pi0=""style="50" type="snippet" readerVersion="6.0" featureSet="257" product="8.0(370)" "" pi1=""SnippetType="InCopyInterchange""">
<ea:StoryInfo>
<ea:SI_EL>headline</ea:SI_EL>
<ea:SI_Words>4</ea:SI_Words>
<ea:SI_Chars>20</ea:SI_Chars>
<ea:SI_Paras>1</ea:SI_Paras>
<ea:SI_Lines>1</ea:SI_Lines>
<ea:SI_Snippet>THIS IS THE HEADLINE</ea:SI_Snippet>
<ea:SI_Version>AB86A3CA-CEBC-49AA-A334-29641B95748D</ea:SI_Version>
</ea:StoryInfo>
</ea:Story>
</ea:Stories>";
XDocument xdoc = XDocument.Parse(xml.ToString());
XElement v = xdoc.Descendants().FirstOrDefault(x => x.Name.LocalName == "SI_Snippet");
EDIT
编辑
XPathNavigator navigator = xmldDoc.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
ns.AddNamespace("ea", "urn:SmartConnection_v3");
var v = xmlDoc.SelectSingleNode("//ea:SI_Snippet", ns);