trying to use Tag Soup - fromAttrib

I have the following TagOpen [TagOpen "a" [("href","/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWebForwards.do")]] I would like to get the attribute resourceId=4 from that. My understanding is that fromAttrib is the right thing to use. But I'm having difficulty understanding the type signature fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str I'm sure if I did, the way to use it would be clear. Could someone help me make sense of this?

On Tuesday 02 November 2010 22:17:48, Michael Litchard wrote:
I have the following TagOpen [TagOpen "a" [("href","/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWeb Forwards.do")]]
I would like to get the attribute resourceId=4 from that. My understanding is that fromAttrib is the right thing to use. But I'm having difficulty understanding the type signature fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
I'm sure if I did, the way to use it would be clear. Could someone help me make sense of this?
In most uses, str will be one of - String - ByteString - Text substitute the one you're using for str. I've no idea why there's a Show constraint (except perhaps it's needed for a failure message).

Daniel,
Thank you for your reply. I'm still confused.
When I see a code sample like this
main = do
posts <- liftM parseTags (readFile "posts.xml")
print $ head $ map (fromAttrib "Id") $
filter (~== ("<row OwnerUserId=" ++ userid ++ ">"))
posts
I have no idea how to match that up with what you said. The usage of
fromAttrib here doesn't match up with what I htink the type signature
is saying.
fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
seems to say "fromAttrib takes two parameters (I know it doesn't
literally take two), one str (with the constraints in parenthesis to
the left) and one str of type Tag, giving back a str. Then I look at
the above code sample and can't match the two up.
On Tue, Nov 2, 2010 at 2:23 PM, Daniel Fischer
On Tuesday 02 November 2010 22:17:48, Michael Litchard wrote:
I have the following TagOpen [TagOpen "a" [("href","/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWeb Forwards.do")]]
I would like to get the attribute resourceId=4 from that. My understanding is that fromAttrib is the right thing to use. But I'm having difficulty understanding the type signature fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
I'm sure if I did, the way to use it would be clear. Could someone help me make sense of this?
In most uses, str will be one of
- String - ByteString - Text
substitute the one you're using for str.
I've no idea why there's a Show constraint (except perhaps it's needed for a failure message).

On 2 November 2010 22:40, Michael Litchard
fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
seems to say "fromAttrib takes two parameters (I know it doesn't literally take two), one str (with the constraints in parenthesis to the left) and one str of type Tag, giving back a str. Then I look at the above code sample and can't match the two up.
It's one string-like (String, ByteString, etc.) value `str`, and one *Tag* which contains, in its attributes and text contents, string-like values, also `str`. It extracts this `str` from the `Tag str` (tag of string-like `str`). The reason Tag is parametrized over the String type is so that you can use TagSoup with String (normal case), or ByteString for when you need speed, or Text, or whatever string type you need that implements the StringLike type.

Excerpts from Michael Litchard's message of Tue Nov 02 22:40:27 +0100 2010:
Daniel, Thank you for your reply. I'm still confused. When I see a code sample like this main = do posts <- liftM parseTags (readFile "posts.xml") print $ head $ map (fromAttrib "Id") $ filter (~== ("<row OwnerUserId=" ++ userid ++ ">")) posts
I have no idea how to match that up with what you said. The usage of fromAttrib here doesn't match up with what I htink the type signature is saying.
fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
seems to say "fromAttrib takes two parameters (I know it doesn't literally take two), one str (with the constraints in parenthesis to the left) and one str of type Tag, giving back a str. Then I look at the above code sample and can't match the two up.
In the code sample, the first argument is "Id", in which case the concrete type for the type variable str is String, and the second argument are the tags returned by the call to filter, which have type Tag String. The second parameter is not a str of type Tag, but Tag (which is a type constructor) applied to the same concrete type for str its first argument has. So in your case, calling
fromAttrib "href" (TagOpen ...)
Would give you "/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWebForwards.do". To get the resourceId you want, you'd have to dissect this string further.

Ah thank you. I can go ahead and figure out how to parse that string.
Using a regex is tempting but I have a feeling I can get something
more maintainable if I use another approach.
On Tue, Nov 2, 2010 at 2:52 PM, Daniel Schoepe
Excerpts from Michael Litchard's message of Tue Nov 02 22:40:27 +0100 2010:
Daniel, Thank you for your reply. I'm still confused. When I see a code sample like this main = do posts <- liftM parseTags (readFile "posts.xml") print $ head $ map (fromAttrib "Id") $ filter (~== ("<row OwnerUserId=" ++ userid ++ ">")) posts
I have no idea how to match that up with what you said. The usage of fromAttrib here doesn't match up with what I htink the type signature is saying.
fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
seems to say "fromAttrib takes two parameters (I know it doesn't literally take two), one str (with the constraints in parenthesis to the left) and one str of type Tag, giving back a str. Then I look at the above code sample and can't match the two up.
In the code sample, the first argument is "Id", in which case the concrete type for the type variable str is String, and the second argument are the tags returned by the call to filter, which have type Tag String.
The second parameter is not a str of type Tag, but Tag (which is a type constructor) applied to the same concrete type for str its first argument has.
So in your case, calling
fromAttrib "href" (TagOpen ...)
Would give you "/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWebForwards.do". To get the resourceId you want, you'd have to dissect this string further.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Michael,
If you just replace all 'str' with 'String' when reading the signature
then that will almost certainly be sufficient for your purposes. So
read it as:
fromAttrib :: String -> Tag String -> String
Where "Tag String" can be thought of as just "Tag". If you try to
parse HTML with regular expressions you'll probably find it much
harder than using tagsoup.
As one of the previous commenter's guessed, Show is indeed required
only for nice error messages. I think it's also probably redundant,
since StringLike has a toString style method on it anyway.
Thanks, Neil
On Tue, Nov 2, 2010 at 10:04 PM, Michael Litchard
Ah thank you. I can go ahead and figure out how to parse that string. Using a regex is tempting but I have a feeling I can get something more maintainable if I use another approach.
On Tue, Nov 2, 2010 at 2:52 PM, Daniel Schoepe
wrote: Excerpts from Michael Litchard's message of Tue Nov 02 22:40:27 +0100 2010:
Daniel, Thank you for your reply. I'm still confused. When I see a code sample like this main = do posts <- liftM parseTags (readFile "posts.xml") print $ head $ map (fromAttrib "Id") $ filter (~== ("<row OwnerUserId=" ++ userid ++ ">")) posts
I have no idea how to match that up with what you said. The usage of fromAttrib here doesn't match up with what I htink the type signature is saying.
fromAttrib :: (Show str, Eq str, StringLike str) => str -> Tag str -> str
seems to say "fromAttrib takes two parameters (I know it doesn't literally take two), one str (with the constraints in parenthesis to the left) and one str of type Tag, giving back a str. Then I look at the above code sample and can't match the two up.
In the code sample, the first argument is "Id", in which case the concrete type for the type variable str is String, and the second argument are the tags returned by the call to filter, which have type Tag String.
The second parameter is not a str of type Tag, but Tag (which is a type constructor) applied to the same concrete type for str its first argument has.
So in your case, calling
fromAttrib "href" (TagOpen ...)
Would give you "/launchWebForward.do?resourceId=4&policy=0&returnTo=%2FshowWebForwards.do". To get the resourceId you want, you'd have to dissect this string further.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (5)
-
Christopher Done
-
Daniel Fischer
-
Daniel Schoepe
-
Michael Litchard
-
Neil Mitchell