I’m trying to parse out a FlowDocument that has a couple specific template match cases that I’m having trouble with.
So, for the Xaml document below (this was created by opening Word, creating the text, then copying into a WPF richtextbox and extracting the 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>
Using the following Xslt, I’m trying to match the Span and Run tags that have a child node “TextDecoration” that has an attribute value “Location=Underline”.
<?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>
I realize the above statement will not work, and if it actually did it would probably select the child node, not the parent node that I’m looking to get to.
The output I’m trying to get is the following:
<html xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<body>
<u>List Item 2</u>
<u>List Item 3</u>
</body>
</html>
I’m also having issues just matching “p:Run/Run.TextDecorations” element. Using Visual Studio, and stepping through the execution, it never finds the node I’m trying to find.
Any hints are welcome! Thanks!
There are a couple of ways you could match them. For example, you could specify the full path to descendant node you want to check for
Or if you want to be less explicitly, and just check there was descendant matching at some level, you could do the following
Do note the use of the namespace prefix on all elements in the XPath condition.
So, given the following XSLT
When applied to your sample XML, the following is output: