2012-02-03 115 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