2012-02-03 113 views
0

我试图分析出那有我有麻烦了几个特定的​​模板匹配情况下的FlowDocument属性的节点。XSLT匹配,其中一个孩子的孩子有一个值

所以,下面的XAML文档(这是通过打开Word,创建文本,然后复制到一个WPF的RichTextBox和提取的FlowDocument XAML中创建)。

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
<Span xml:lang="en-us"> 
    <Span.TextDecorations> 
    <TextDecoration Location="Underline" /> 
    </Span.TextDecorations> 
    List Item 2 
</Span> 
<Run> 
    <Run.TextDecorations> 
    <TextDecoration Location="Underline" /> 
    </Run.TextDecorations> 
    List Item 3 
</Run> 
</FlowDocument> 

使用下面的XSLT,我想匹配的有有一个属性值“位置=下划线”子节点“TextDecoration”的跨度和运行的标签。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    exclude-result-prefixes="msxsl"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <body> 
     <xsl:apply-templates /> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="p:FlowDocument"> 
    <xsl:apply-templates /> 
    </xsl:template> 
    <xsl:template match="p:Run/Run.TextDecorations/TextDecoration[@Location='Underline']"> 
    <u> 
     <xsl:apply-templates /> 
    </u> 
    </xsl:template> 
</xsl:stylesheet> 

我知道上面的语句将无法正常工作,如果它实际上没有它可能会选择子节点,而不是说我希望得到的父节点。

我试图让输出如下:

<html xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
<body> 
<u>List Item 2</u> 
<u>List Item 3</u> 
</body> 
</html> 

我也有问题,只是匹配“号码:运行/ Run.TextDecorations”元素。使用Visual Studio,并逐步执行,它永远不会找到我试图找到的节点。

任何提示的欢迎!谢谢!

回答

0

有一对夫妇,你可以匹配他们的方式。例如,您可以指定要检查

<xsl:template 
    match="p:Run[p:Run.TextDecorations/p:TextDecoration/@Location='Underline']"> 

或者,如果你想少明确,只是检查有后代匹配在一定程度上,你可以做以下

的完整路径后代节点
<xsl:template 
    match="p:Span[descendant::p:TextDecoration/@Location='Underline']"> 

请注意在XPath条件中的所有元素上使用名称空间前缀。

因此,鉴于以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation" exclude-result-prefixes="msxsl p"> 
    <xsl:output method="html" indent="yes"/> 
    <xsl:template match="/"> 
     <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="p:Run[p:Run.TextDecorations/p:TextDecoration/@Location='Underline']"> 
     <u> 
     <xsl:apply-templates/> 
     </u> 
    </xsl:template> 
    <xsl:template match="p:Span[descendant::p:TextDecoration/@Location='Underline']"> 
     <u> 
     <xsl:apply-templates/> 
     </u> 
    </xsl:template> 
    <xsl:template match="p:FlowDocument"> 
     <xsl:apply-templates/> 
    </xsl:template> 
</xsl:stylesheet> 

当适用于您的示例XML,下面是输出:

<html> 
    <body> 
     <u> List Item 2 </u> 
     <u> List Item 3 </u> 
    </body> 
</html> 
+0

AHAH!命名空间的东西更有意义。这绝对是我错过的!谢谢! – Bryan 2012-02-03 18:20:39

相关问题