deriveXmlNewtype :: Options -> Name -> DecsQ
deriveXmlNewtype opts name = do
  wrappedConName <- conName <$> getWrappedTypeCon name
  wrappedTypeName <- getWrappedTypeName name
  let outerType = conT name
      innerType = conT wrappedTypeName
      vX = varE $ mkName "x"
      outerPattern = conP wrappedConName [varP $ mkName "x"]
      outerCon = conE wrappedConName
      restrictsDec = [d|
        instance Restricts $(outerType) $(innerType) where
          restricts $(outerPattern) = $(vX)
        |]
      schemaTypeDec = [d|
        instance SchemaType $(outerType) where
          parseSchemaType s = do
            e <- element [s]
            commit $ interior e $ parseSimpleType
          schemaTypeToXML s $(outerPattern) =
            toXMLElement s [] [toXMLText (simpleTypeText $(vX))]
        |]

      simpleTypeDec = [d|
        instance SimpleType $(outerType) where
          acceptingParser = fmap $(outerCon) acceptingParser
          simpleTypeText $(outerPattern) = simpleTypeText $(vX)
        |]
  let standardDecs = [restrictsDec, schemaTypeDec, simpleTypeDec]
      decs = case specialType $ ConT wrappedTypeName of
               SList -> deriveIsList name : standardDecs
               _ -> standardDecs
  concat <$> sequence decs
deriveXmlNewtype :: Options -> Name -> DecsQ
deriveXmlNewtype opts name = do
  wrappedConName <- conName <$> getWrappedTypeCon name
  wrappedTypeName <- getWrappedTypeName name
  let outerType = conT name
      innerType = conT wrappedTypeName
      vX = varE $ mkName "x"
      outerPattern = conP wrappedConName [varP $ mkName "x"]
      outerCon = conE wrappedConName
      restrictsDec = [d|
        instance Restricts $(outerType) $(innerType) where
          restricts $(outerPattern) = $(vX)
        |]
      schemaTypeDec = [d|
        instance SchemaType $(outerType) where
          parseSchemaType s = do
            e <- element [s]
            commit $ interior e $ parseSimpleType
          schemaTypeToXML s $(outerPattern) =
            toXMLElement s [] [toXMLText (simpleTypeText $(vX))]
        |]

      simpleTypeDec = [d|
        instance SimpleType $(outerType) where
          acceptingParser = fmap $(outerCon) acceptingParser
          simpleTypeText $(outerPattern) = simpleTypeText $(vX)
        |]
  let standardDecs = [restrictsDec, schemaTypeDec, simpleTypeDec]
      decs = case specialType $ ConT wrappedTypeName of
               SList -> deriveIsList name : standardDecs
               _ -> standardDecs
  concat <$> sequence decs
deriveXmlNewtype opts name = do
  wrappedConName <- conName <$> getWrappedTypeCon name
  wrappedTypeName <- getWrappedTypeName name
  let outerType = conT name
      innerType = conT wrappedTypeName
      vX = varE $ mkName "x"
      outerPattern = conP wrappedConName [varP $ mkName "x"]
      outerCon = conE wrappedConName
      restrictsDec = [d|
        instance Restricts $(outerType) $(innerType) where
          restricts $(outerPattern) = $(vX)
        |]
      schemaTypeDec = [d|
        instance SchemaType $(outerType) where
          parseSchemaType s = do
            e <- element [s]
            commit $ interior e $ parseSimpleType
          schemaTypeToXML s $(outerPattern) =
            toXMLElement s [] [toXMLText (simpleTypeText $(vX))]
        |]

      simpleTypeDec = [d|
        instance SimpleType $(outerType) where
          acceptingParser = fmap $(outerCon) acceptingParser
          simpleTypeText $(outerPattern) = simpleTypeText $(vX)
        |]
  let standardDecs = [restrictsDec, schemaTypeDec, simpleTypeDec]
      decs = case specialType $ ConT wrappedTypeName of
               SList -> deriveIsList name : standardDecs
               _ -> standardDecs
  concat <$> sequence decs