Add preliminary XML namespace selectors
Getty Ritter
7 years ago
142 | 142 | -- matched. |
143 | 143 | data Selector |
144 | 144 | = SelByName String |
145 | | SelByNS String | |
146 | | SelBoth Selector Selector | |
145 | 147 | | SelAny |
146 | 148 | deriving (Eq, Show) |
149 | ||
150 | instance Monoid Selector where | |
151 | mempty = SelAny | |
152 | mappend = SelBoth | |
147 | 153 | |
148 | 154 | instance GHC.IsString Selector where |
149 | 155 | fromString = SelByName |
152 | 158 | toPred SelAny _ = True |
153 | 159 | toPred (SelByName n) el = |
154 | 160 | XML.showQName (XML.elName el) == n |
161 | toPred (SelByNS n) el = | |
162 | case XML.qPrefix (XML.elName el) of | |
163 | Nothing -> False | |
164 | Just p -> p == n | |
165 | toPred (SelBoth s1 s2) el = | |
166 | toPred s1 el && toPred s2 el | |
155 | 167 | |
156 | 168 | -- | Find an attribute on the current focus element and parse it to a |
157 | 169 | -- value of type @t@. If the parse function fails, then this will fail |
224 | 236 | byTag :: String -> Selector |
225 | 237 | byTag = SelByName |
226 | 238 | |
239 | -- | Creates a 'Selector' which expects a specific namespace | |
240 | byNamespace :: String -> Selector | |
241 | byNamespace = SelByNS | |
242 | ||
227 | 243 | -- | Creates a 'Selector' which matches any possible child element. |
228 | 244 | any :: Selector |
229 | 245 | any = SelAny |